1 ##############################################################################
2 # Copyright (c) 2019 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 django.template.loader import render_to_string
13 from account.models import PublicNetwork
15 from resource_inventory.models import (
23 Utility class to create a full IDF yaml file
26 self.net_names = ["admin", "mgmt", "private", "public"]
28 for i, name in enumerate(self.net_names):
29 self.networks[name] = {
33 "ip": "10.250." + str(i) + ".0",
37 def makeIDF(self, booking):
39 fills the installer descriptor file template with info about the resource
41 template = "dashboard/idf.yaml"
43 info['version'] = "0.1"
44 info['net_config'] = self.get_net_config(booking)
45 info['fuel'] = self.get_fuel_config(booking)
47 return render_to_string(template, context=info)
49 def get_net_config(self, booking):
52 net_config['oob'] = self.get_oob_net(booking)
54 net_config['oob'] = {}
56 net_config['public'] = self.get_public_net(booking)
58 net_config['public'] = {}
60 for net in [net for net in self.net_names if net != "public"]:
62 net_config[net] = self.get_single_net_config(booking, net)
68 def get_public_net(self, booking):
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"]
85 def get_oob_net(self, booking):
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"
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
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']
106 def get_fuel_config(self, booking):
108 fuel['jumphost'] = {}
110 fuel['jumphost']['bridges'] = self.get_fuel_bridges()
112 fuel['jumphost']['bridges'] = {}
116 fuel['network']['nodes'] = self.get_fuel_nodes(booking)
118 fuel['network']['nodes'] = {}
122 def get_fuel_bridges(self):
124 for net in self.net_names:
125 bridges[net] = "br-" + net
129 def get_fuel_nodes(self, booking):
130 hosts = booking.resource.hosts.exclude(config__opnfvRole__name="jumphost")
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]
141 def get_node_interfaces(self, node):
142 # TODO: this should sync with pdf ordering
145 for iface in node.interfaces.all():
146 interfaces.append({"name": iface.name, "bus": iface.bus_address})