Fixes the idf and pdf templates so that we can deploy opnfv
[pharos-tools.git] / dashboard / 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 (
16     OPNFVConfig,
17     Vlan
18 )
19
20
21 class IDFTemplater:
22     """
23     Utility class to create a full IDF yaml file
24     """
25     def __init__(self):
26         self.net_names = ["admin", "mgmt", "private", "public"]
27         self.networks = {}
28         for i, name in enumerate(self.net_names):
29             self.networks[name] = {
30                 "name": name,
31                 "vlan": -1,
32                 "interface": i,
33                 "ip": "10.250." + str(i) + ".0",
34                 "netmask": 24
35             }
36
37     def makeIDF(self, booking):
38         """
39         fills the installer descriptor file template with info about the resource
40         """
41         template = "dashboard/idf.yaml"
42         info = {}
43         info['version'] = "0.1"
44         info['net_config'] = self.get_net_config(booking)
45         info['fuel'] = self.get_fuel_config(booking)
46
47         return render_to_string(template, context=info)
48
49     def get_net_config(self, booking):
50         net_config = {}
51         try:
52             net_config['oob'] = self.get_oob_net(booking)
53         except Exception:
54             net_config['oob'] = {}
55         try:
56             net_config['public'] = self.get_public_net(booking)
57         except Exception:
58             net_config['public'] = {}
59
60         for net in [net for net in self.net_names if net != "public"]:
61             try:
62                 net_config[net] = self.get_single_net_config(booking, net)
63             except Exception:
64                 net_config[net] = {}
65
66         return net_config
67
68     def get_public_net(self, booking):
69         public = {}
70         config = OPNFVConfig.objects.get(bundle=booking.config_bundle)
71         public_role = config.networks.get(name="public")
72         public_vlan = Vlan.objects.filter(network=public_role.network).first()
73         public_network = PublicNetwork.objects.get(vlan=public_vlan.vlan_id, lab=booking.lab)
74         self.networks['public']['vlan'] = public_vlan.vlan_id
75         public['interface'] = self.networks['public']['interface']
76         public['vlan'] = public_network.vlan  # untagged??
77         public['network'] = public_network.cidr.split("/")[0]
78         public['mask'] = public_network.cidr.split("/")[1]
79         # public['ip_range'] = 4  # necesary?
80         public['gateway'] = public_network.gateway
81         public['dns'] = ["1.1.1.1", "8.8.8.8"]
82
83         return public
84
85     def get_oob_net(self, booking):
86         net = {}
87         hosts = booking.resource.hosts.all()
88         addrs = [host.remote_management.address for host in hosts]
89         net['ip_range'] = ",".join(addrs)
90         net['vlan'] = "native"
91         return net
92
93     def get_single_net_config(self, booking, net_name):
94         config = OPNFVConfig.objects.get(bundle=booking.config_bundle)
95         role = config.networks.get(name=net_name)
96         vlan = Vlan.objects.filter(network=role.network).first()
97         self.networks[net_name]['vlan'] = vlan.vlan_id
98         net = {}
99         net['interface'] = self.networks[net_name]['interface']
100         net['vlan'] = vlan.vlan_id
101         net['network'] = self.networks[net_name]['ip']
102         net['mask'] = self.networks[net_name]['netmask']
103
104         return net
105
106     def get_fuel_config(self, booking):
107         fuel = {}
108         fuel['jumphost'] = {}
109         try:
110             fuel['jumphost']['bridges'] = self.get_fuel_bridges()
111         except Exception:
112             fuel['jumphost']['bridges'] = {}
113
114         fuel['network'] = {}
115         try:
116             fuel['network']['nodes'] = self.get_fuel_nodes(booking)
117         except Exception:
118             fuel['network']['nodes'] = {}
119
120         return fuel
121
122     def get_fuel_bridges(self):
123         bridges = {}
124         for net in self.net_names:
125             bridges[net] = "br-" + net
126
127         return bridges
128
129     def get_fuel_nodes(self, booking):
130         hosts = booking.resource.hosts.exclude(config__opnfvRole__name="jumphost")
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