1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from rest_framework import serializers
13 from resource_inventory.models import (
24 class BookingField(serializers.Field):
26 def to_representation(self, booking):
28 Take in a booking object.
30 Returns a dictionary of primitives representing that booking
33 ser['id'] = booking.id
34 # main loop to grab relevant info out of booking
35 host_configs = {} # mapping hostname -> config
36 networks = {} # mapping vlan id -> network_hosts
37 for host in booking.resource.hosts.all():
38 host_configs[host.name] = HostConfiguration.objects.get(host=host.template)
39 if "jumphost" not in ser and host_configs[host.name].opnfvRole.name.lower() == "jumphost":
40 ser['jumphost'] = host.name
41 # host is a Host model
42 for i in range(len(host.interfaces.all())):
43 interface = host.interfaces.all()[i]
44 # interface is an Interface model
45 for vlan in interface.config.all():
47 if vlan.id not in networks:
48 networks[vlan.id] = []
49 net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface": i}
50 networks[vlan.id].append(net_host)
51 # creates networking object of proper form
53 for vlanid in networks:
55 network['vlan_id'] = vlanid
56 network['hosts'] = networks[vlanid]
58 ser['networking'] = networking
60 # creates hosts object of correct form
62 for hostname in host_configs:
63 host = {"hostname": hostname}
64 host['deploy_image'] = True # TODO?
65 image = host_configs[hostname].image
68 "lab_id": image.lab_id,
69 "dashboard_id": image.id
77 def to_internal_value(self, data):
79 Take in a dictionary of primitives, and return a booking object.
81 This is not going to be implemented or allowed.
82 If someone needs to create a booking through the api,
83 they will send a different booking object
88 class BookingSerializer(serializers.Serializer):
90 booking = BookingField()
93 # Host Type stuff, for inventory
94 class CPUSerializer(serializers.ModelSerializer):
97 fields = ('cores', 'architecture', 'cpus')
100 class DiskSerializer(serializers.ModelSerializer):
103 fields = ('size', 'media_type', 'name')
106 class InterfaceProfileSerializer(serializers.ModelSerializer):
108 model = InterfaceProfile
109 fields = ('speed', 'name')
112 class RamSerializer(serializers.ModelSerializer):
115 fields = ('amount', 'channels')
118 class HostTypeSerializer(serializers.Serializer):
119 name = serializers.CharField(max_length=200)
120 ram = RamSerializer()
121 interface = InterfaceProfileSerializer()
122 description = serializers.CharField(max_length=1000)
123 disks = DiskSerializer()
124 cpu = CPUSerializer()
127 # the rest of the inventory stuff
128 class NetworkSerializer(serializers.Serializer):
129 cidr = serializers.CharField(max_length=200)
130 gateway = serializers.IPAddressField(max_length=200)
131 vlan = serializers.IntegerField()
134 class ImageSerializer(serializers.ModelSerializer):
135 lab_id = serializers.IntegerField()
136 id = serializers.IntegerField(source="dashboard_id")
137 name = serializers.CharField(max_length=50)
138 description = serializers.CharField(max_length=200)
144 class InterfaceField(serializers.Field):
145 def to_representation(self, interface):
148 def to_internal_value(self, data):
149 """Take in a serialized interface and creates an Interface model."""
151 bus_address = data['busaddr']
152 switch_name = data['switchport']['switch_name']
153 port_name = data['switchport']['port_name']
155 return Interface.objects.create(
157 bus_address=bus_address,
158 switch_name=switch_name,
163 class InventoryHostSerializer(serializers.Serializer):
164 hostname = serializers.CharField(max_length=100)
165 host_type = serializers.CharField(max_length=100)
166 interfaces = InterfaceField()
169 class InventorySerializer(serializers.Serializer):
170 hosts = InventoryHostSerializer()
171 networks = NetworkSerializer()
172 images = ImageSerializer()
173 host_types = HostTypeSerializer()