add nick
[laas.git] / src / resource_inventory / idf_templater.py
1 ##############################################################################
2 # Copyright (c) 2019 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 django.template.loader import render_to_string
12
13 from account.models import PublicNetwork
14
15 from resource_inventory.models import Vlan
16
17
18 class IDFTemplater:
19     """Utility class to create a full Installer Descriptor File (IDF) yaml file."""
20
21     net_names = ["admin", "mgmt", "private", "public"]
22     bridge_names = {
23         "admin": "br-admin",
24         "mgmt": "br-mgmt",
25         "private": "br-private",
26         "public": "br-public"
27     }
28
29     def __init__(self):
30         self.networks = {}
31         for i, name in enumerate(self.net_names):
32             self.networks[name] = {
33                 "name": name,
34                 "vlan": -1,
35                 "interface": i,
36                 "ip": "10.250." + str(i) + ".0",
37                 "netmask": 24
38             }
39
40     def makeIDF(self, booking):
41         """Fill the IDF template with info about the resource."""
42         template = "dashboard/idf.yaml"
43         info = {}
44         info['version'] = "0.1"
45         info['net_config'] = self.get_net_config(booking)
46         info['fuel'] = self.get_fuel_config(booking)
47
48         return render_to_string(template, context=info)
49
50     def get_net_config(self, booking):
51         net_config = {}
52         try:
53             net_config['oob'] = self.get_oob_net(booking)
54         except Exception:
55             net_config['oob'] = {}
56         try:
57             net_config['public'] = self.get_public_net(booking)
58         except Exception:
59             net_config['public'] = {}
60
61         for net in [net for net in self.net_names if net != "public"]:
62             try:
63                 net_config[net] = self.get_single_net_config(booking, net)
64             except Exception:
65                 net_config[net] = {}
66
67         return net_config
68
69     def get_public_net(self, booking):
70         public = {}
71         config = booking.opnfv_config
72         public_role = config.networks.get(name="public")
73         public_vlan = Vlan.objects.filter(network=public_role.network).first()
74         public_network = PublicNetwork.objects.get(vlan=public_vlan.vlan_id, lab=booking.lab)
75         self.networks['public']['vlan'] = public_vlan.vlan_id
76         public['interface'] = self.networks['public']['interface']
77         public['vlan'] = public_network.vlan  # untagged??
78         public['network'] = public_network.cidr.split("/")[0]
79         public['mask'] = public_network.cidr.split("/")[1]
80         # public['ip_range'] = 4  # necesary?
81         public['gateway'] = public_network.gateway
82         public['dns'] = ["1.1.1.1", "8.8.8.8"]
83
84         return public
85
86     def get_oob_net(self, booking):
87         net = {}
88         hosts = booking.resource.hosts.all()
89         addrs = [host.remote_management.address for host in hosts]
90         net['ip_range'] = ",".join(addrs)
91         net['vlan'] = "native"
92         return net
93
94     def get_single_net_config(self, booking, net_name):
95         config = booking.opnfv_config
96         role = config.networks.get(name=net_name)
97         vlan = Vlan.objects.filter(network=role.network).first()
98         self.networks[net_name]['vlan'] = vlan.vlan_id
99         net = {}
100         net['interface'] = self.networks[net_name]['interface']
101         net['vlan'] = vlan.vlan_id
102         net['network'] = self.networks[net_name]['ip']
103         net['mask'] = self.networks[net_name]['netmask']
104
105         return net
106
107     def get_fuel_config(self, booking):
108         fuel = {}
109         fuel['jumphost'] = {}
110         try:
111             fuel['jumphost']['bridges'] = self.get_fuel_bridges()
112         except Exception:
113             fuel['jumphost']['bridges'] = {}
114
115         fuel['network'] = {}
116         try:
117             fuel['network']['nodes'] = self.get_fuel_nodes(booking)
118         except Exception:
119             fuel['network']['nodes'] = {}
120
121         return fuel
122
123     def get_fuel_bridges(self):
124         return self.bridge_names
125
126     def get_fuel_nodes(self, booking):
127         jumphost = booking.opnfv_config.host_opnfv_config.get(
128             role__name__iexact="jumphost"
129         )
130         hosts = booking.resource.hosts.exclude(pk=jumphost.pk)
131         nodes = []
132         for host in hosts:
133             node = {}
134             ordered_interfaces = self.get_node_interfaces(host)
135             node['interfaces'] = [iface['name'] for iface in ordered_interfaces]
136             node['bus_addrs'] = [iface['bus'] for iface in ordered_interfaces]
137             nodes.append(node)
138
139         return nodes
140
141     def get_node_interfaces(self, node):
142         # TODO: this should sync with pdf ordering
143         interfaces = []
144
145         for iface in node.interfaces.all():
146             interfaces.append({"name": iface.name, "bus": iface.bus_address})
147
148         return interfaces