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 *
15 class BookingField(serializers.Field):
17 def to_representation(self, booking):
19 Takes in a booking object.
20 Returns a dictionary of primitives representing that booking
23 ser['id'] = booking.id
24 # main loop to grab relevant info out of booking
25 host_configs = {} # mapping hostname -> config
26 networks = {} # mapping vlan id -> network_hosts
27 for host in booking.resource.hosts.all():
28 host_configs[host.name] = HostConfiguration.objects.get(host=host.template)
29 if "jumphost" not in ser and host_configs[host.name].opnfvRole.name.lower() == "jumphost":
30 ser['jumphost'] = host.name
32 for i in range(len(host.interfaces.all())):
33 interface = host.interfaces.all()[i]
34 #interface is an Interface model
35 for vlan in interface.config.all():
37 if vlan.id not in networks:
38 networks[vlan.id] = []
39 net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface":i}
40 networks[vlan.id].append(net_host)
41 #creates networking object of proper form
43 for vlanid in networks:
45 network['vlan_id'] = vlanid
46 network['hosts'] = networks[vlanid]
48 ser['networking'] = networking
50 #creates hosts object of correct form
52 for hostname in host_configs:
53 host = {"hostname": hostname}
54 host['deploy_image'] = True # TODO?
55 image = host_configs[hostname].image
58 "lab_id": image.lab_id,
59 "dashboard_id": image.id
67 def to_internal_value(self, data):
69 Takes in a dictionary of primitives
70 Returns a booking object
72 This is not going to be implemented or allowed.
73 If someone needs to create a booking through the api,
74 they will send a different booking object
78 class BookingSerializer(serializers.Serializer):
80 booking = BookingField()
82 #Host Type stuff, for inventory
84 class CPUSerializer(serializers.ModelSerializer):
87 fields = ('cores', 'architecture', 'cpus')
89 class DiskSerializer(serializers.ModelSerializer):
92 fields = ('size', 'media_type', 'name')
94 class InterfaceProfileSerializer(serializers.ModelSerializer):
96 model = InterfaceProfile
97 fields = ('speed', 'name')
99 class RamSerializer(serializers.ModelSerializer):
102 fields = ('amount', 'channels')
104 class HostTypeSerializer(serializers.Serializer):
105 name = serializers.CharField(max_length=200)
106 ram = RamSerializer()
107 interface = InterfaceProfileSerializer()
108 description = serializers.CharField(max_length=1000)
109 disks = DiskSerializer()
110 cpu = CPUSerializer()
112 #the rest of the inventory stuff
113 class NetworkSerializer(serializers.Serializer):
114 cidr = serializers.CharField(max_length=200)
115 gateway = serializers.IPAddressField(max_length=200)
116 vlan = serializers.IntegerField()
118 class ImageSerializer(serializers.ModelSerializer):
119 lab_id = serializers.IntegerField()
120 id = serializers.IntegerField(source="dashboard_id")
121 name = serializers.CharField(max_length=50)
122 description = serializers.CharField(max_length=200)
126 class InterfaceField(serializers.Field):
127 def to_representation(self, interface):
130 def to_internal_value(self, data):
132 takes in a serialized interface and creates an Interface model
135 bus_address = data['busaddr']
136 switch_name = data['switchport']['switch_name']
137 port_name = data['switchport']['port_name']
139 return Interface.objects.create(
141 bus_address=bus_address,
142 switch_name=switch_name,
146 class InventoryHostSerializer(serializers.Serializer):
147 hostname = serializers.CharField(max_length=100)
148 host_type = serializers.CharField(max_length=100)
149 interfaces = InterfaceField()
152 class InventorySerializer(serializers.Serializer):
153 hosts = InventoryHostSerializer()
154 networks = NetworkSerializer()
155 images = ImageSerializer()
156 host_types = HostTypeSerializer()