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 Vlan
19 """Utility class to create a full Installer Descriptor File (IDF) yaml file."""
21 net_names = ["admin", "mgmt", "private", "public"]
25 "private": "br-private",
31 for i, name in enumerate(self.net_names):
32 self.networks[name] = {
36 "ip": "10.250." + str(i) + ".0",
40 def makeIDF(self, booking):
41 """Fill the IDF template with info about the resource."""
42 template = "dashboard/idf.yaml"
44 info['version'] = "0.1"
45 info['net_config'] = self.get_net_config(booking)
46 info['fuel'] = self.get_fuel_config(booking)
48 return render_to_string(template, context=info)
50 def get_net_config(self, booking):
53 net_config['oob'] = self.get_oob_net(booking)
55 net_config['oob'] = {}
57 net_config['public'] = self.get_public_net(booking)
59 net_config['public'] = {}
61 for net in [net for net in self.net_names if net != "public"]:
63 net_config[net] = self.get_single_net_config(booking, net)
69 def get_public_net(self, booking):
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"]
86 def get_oob_net(self, booking):
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"
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
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']
107 def get_fuel_config(self, booking):
109 fuel['jumphost'] = {}
111 fuel['jumphost']['bridges'] = self.get_fuel_bridges()
113 fuel['jumphost']['bridges'] = {}
117 fuel['network']['nodes'] = self.get_fuel_nodes(booking)
119 fuel['network']['nodes'] = {}
123 def get_fuel_bridges(self):
124 return self.bridge_names
126 def get_fuel_nodes(self, booking):
127 jumphost = booking.opnfv_config.host_opnfv_config.get(
128 role__name__iexact="jumphost"
130 hosts = booking.resource.hosts.exclude(pk=jumphost.pk)
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})