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 Takes in a booking object.
29 Returns a dictionary of primitives representing that booking
32 ser['id'] = booking.id
33 # main loop to grab relevant info out of booking
34 host_configs = {} # mapping hostname -> config
35 networks = {} # mapping vlan id -> network_hosts
36 for host in booking.resource.hosts.all():
37 host_configs[host.name] = HostConfiguration.objects.get(host=host.template)
38 if "jumphost" not in ser and host_configs[host.name].opnfvRole.name.lower() == "jumphost":
39 ser['jumphost'] = host.name
40 # host is a Host model
41 for i in range(len(host.interfaces.all())):
42 interface = host.interfaces.all()[i]
43 # interface is an Interface model
44 for vlan in interface.config.all():
46 if vlan.id not in networks:
47 networks[vlan.id] = []
48 net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface": i}
49 networks[vlan.id].append(net_host)
50 # creates networking object of proper form
52 for vlanid in networks:
54 network['vlan_id'] = vlanid
55 network['hosts'] = networks[vlanid]
57 ser['networking'] = networking
59 # creates hosts object of correct form
61 for hostname in host_configs:
62 host = {"hostname": hostname}
63 host['deploy_image'] = True # TODO?
64 image = host_configs[hostname].image
67 "lab_id": image.lab_id,
68 "dashboard_id": image.id
76 def to_internal_value(self, data):
78 Takes in a dictionary of primitives
79 Returns 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):
150 takes in a serialized interface and creates an Interface model
153 bus_address = data['busaddr']
154 switch_name = data['switchport']['switch_name']
155 port_name = data['switchport']['port_name']
157 return Interface.objects.create(
159 bus_address=bus_address,
160 switch_name=switch_name,
165 class InventoryHostSerializer(serializers.Serializer):
166 hostname = serializers.CharField(max_length=100)
167 host_type = serializers.CharField(max_length=100)
168 interfaces = InterfaceField()
171 class InventorySerializer(serializers.Serializer):
172 hosts = InventoryHostSerializer()
173 networks = NetworkSerializer()
174 images = ImageSerializer()
175 host_types = HostTypeSerializer()