Lab as a Service 2.0
[laas.git] / src / api / serializers / booking_serializer.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
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 ##############################################################################
9
10
11 from rest_framework import serializers
12
13 from resource_inventory.models import *
14
15 class BookingField(serializers.Field):
16
17     def to_representation(self, booking):
18         """
19         Takes in a booking object.
20         Returns a dictionary of primitives representing that booking
21         """
22         ser = {}
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
31             #host is a Host model
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():
36                     #vlan is Vlan model
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
42         networking = []
43         for vlanid in networks:
44             network = {}
45             network['vlan_id'] = vlanid
46             network['hosts'] = networks[vlanid]
47
48         ser['networking'] = networking
49
50         #creates hosts object of correct form
51         hosts = []
52         for hostname in host_configs:
53             host = {"hostname": hostname}
54             host['deploy_image'] = True  # TODO?
55             image = host_configs[hostname].image
56             host['image'] = {
57                 "name": image.name,
58                 "lab_id": image.lab_id,
59                 "dashboard_id": image.id
60             }
61             hosts.append(host)
62
63         ser['hosts'] = hosts
64
65         return ser
66
67     def to_internal_value(self, data):
68         """
69         Takes in a dictionary of primitives
70         Returns a booking object
71
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
75         """
76         return None
77
78 class BookingSerializer(serializers.Serializer):
79
80     booking = BookingField()
81
82 #Host Type stuff, for inventory
83
84 class CPUSerializer(serializers.ModelSerializer):
85     class Meta:
86         model = CpuProfile
87         fields = ('cores', 'architecture', 'cpus')
88
89 class DiskSerializer(serializers.ModelSerializer):
90     class Meta:
91         model = DiskProfile
92         fields = ('size', 'media_type', 'name')
93
94 class InterfaceProfileSerializer(serializers.ModelSerializer):
95     class Meta:
96         model = InterfaceProfile
97         fields = ('speed', 'name')
98
99 class RamSerializer(serializers.ModelSerializer):
100     class Meta:
101         model = RamProfile
102         fields = ('amount', 'channels')
103
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()
111
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()
117
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)
123     class Meta:
124         model = Image
125
126 class InterfaceField(serializers.Field):
127     def to_representation(self, interface):
128         pass
129
130     def to_internal_value(self, data):
131         """
132         takes in a serialized interface and creates an Interface model
133         """
134         mac = data['mac']
135         bus_address = data['busaddr']
136         switch_name = data['switchport']['switch_name']
137         port_name = data['switchport']['port_name']
138         # TODO config??
139         return Interface.objects.create(
140             mac_address=mac,
141             bus_address=bus_address,
142             switch_name=switch_name,
143             port_name=port_name
144         )
145
146 class InventoryHostSerializer(serializers.Serializer):
147     hostname = serializers.CharField(max_length=100)
148     host_type = serializers.CharField(max_length=100)
149     interfaces = InterfaceField()
150
151
152 class InventorySerializer(serializers.Serializer):
153     hosts = InventoryHostSerializer()
154     networks = NetworkSerializer()
155     images = ImageSerializer()
156     host_types = HostTypeSerializer()