summaryrefslogtreecommitdiffstats
path: root/LICENSE.rst
blob: 0e9e7710408abe90cd77f871164552259efd7d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
Copyright (c) 2015 Ericsson AB and others.

All rights reserved. This program and the accompanying materials
are made available under the terms of the Apache License, Version 2.0
which accompanies this distribution, and is available at
http://www.apache.org/licenses/LICENSE-2.0


This license is related to the following files included in this directory:

- ez_setup-py
- setup.py
#333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
##############################################################################
# Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from rest_framework import serializers

from resource_inventory.models import (
    ResourceConfiguration,
    CpuProfile,
    DiskProfile,
    InterfaceProfile,
    RamProfile,
    Image,
    Interface
)


class BookingField(serializers.Field):

    def to_representation(self, booking):
        """
        Take in a booking object.

        Returns a dictionary of primitives representing that booking
        """
        ser = {}
        ser['id'] = booking.id
        # main loop to grab relevant info out of booking
        host_configs = {}  # mapping hostname -> config
        networks = {}  # mapping vlan id -> network_hosts
        for host in booking.resource.hosts.all():
            host_configs[host.name] = ResourceConfiguration.objects.get(host=host.template)
            if "jumphost" not in ser and host_configs[host.name].opnfvRole.name.lower() == "jumphost":
                ser['jumphost'] = host.name
            # host is a Host model
            for i in range(len(host.interfaces.all())):
                interface = host.interfaces.all()[i]
                # interface is an Interface model
                for vlan in interface.config.all():
                    # vlan is Vlan model
                    if vlan.id not in networks:
                        networks[vlan.id] = []
                    net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface": i}
                    networks[vlan.id].append(net_host)
        # creates networking object of proper form
        networking = []
        for vlanid in networks:
            network = {}
            network['vlan_id'] = vlanid
            network['hosts'] = networks[vlanid]

        ser['networking'] = networking

        # creates hosts object of correct form
        hosts = []
        for hostname in host_configs:
            host = {"hostname": hostname}
            host['deploy_image'] = True  # TODO?
            image = host_configs[hostname].image
            host['image'] = {
                "name": image.name,
                "lab_id": image.lab_id,
                "dashboard_id": image.id
            }
            hosts.append(host)

        ser['hosts'] = hosts

        return ser

    def to_internal_value(self, data):
        """
        Take in a dictionary of primitives, and return a booking object.

        This is not going to be implemented or allowed.
        If someone needs to create a booking through the api,
        they will send a different booking object
        """
        return None


class BookingSerializer(serializers.Serializer):

    booking = BookingField()


# Host Type stuff, for inventory
class CPUSerializer(serializers.ModelSerializer):
    class Meta:
        model = CpuProfile
        fields = ('cores', 'architecture', 'cpus')


class DiskSerializer(serializers.ModelSerializer):
    class Meta:
        model = DiskProfile
        fields = ('size', 'media_type', 'name')


class InterfaceProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = InterfaceProfile
        fields = ('speed', 'name')


class RamSerializer(serializers.ModelSerializer):
    class Meta:
        model = RamProfile
        fields = ('amount', 'channels')


class HostTypeSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=200)
    ram = RamSerializer()
    interface = InterfaceProfileSerializer()
    description = serializers.CharField(max_length=1000)
    disks = DiskSerializer()
    cpu = CPUSerializer()


# the rest of the inventory stuff
class NetworkSerializer(serializers.Serializer):
    cidr = serializers.CharField(max_length=200)
    gateway = serializers.IPAddressField(max_length=200)
    vlan = serializers.IntegerField()


class ImageSerializer(serializers.ModelSerializer):
    lab_id = serializers.IntegerField()
    id = serializers.IntegerField(source="dashboard_id")
    name = serializers.CharField(max_length=50)
    description = serializers.CharField(max_length=200)

    class Meta:
        model = Image


class InterfaceField(serializers.Field):
    def to_representation(self, interface):
        pass

    def to_internal_value(self, data):
        """Take in a serialized interface and creates an Interface model."""
        mac = data['mac']
        bus_address = data['busaddr']
        switch_name = data['switchport']['switch_name']
        port_name = data['switchport']['port_name']
        # TODO config??
        return Interface.objects.create(
            mac_address=mac,
            bus_address=bus_address,
            switch_name=switch_name,
            port_name=port_name
        )


class InventoryHostSerializer(serializers.Serializer):
    hostname = serializers.CharField(max_length=100)
    host_type = serializers.CharField(max_length=100)
    interfaces = InterfaceField()


class InventorySerializer(serializers.Serializer):
    hosts = InventoryHostSerializer()
    networks = NetworkSerializer()
    images = ImageSerializer()
    host_types = HostTypeSerializer()