1 ##############################################################################
2 # Copyright (c) 2018 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 ##############################################################################
10 from django.contrib.auth.models import User
11 from django.core.files.base import ContentFile
16 from dashboard.exceptions import (
17 InvalidHostnameException
19 from booking.models import Booking
20 from account.models import UserProfile, Lab, LabStatus, VlanManager, PublicNetwork
21 from resource_inventory.models import (
35 GenericResourceBundle,
43 from resource_inventory.resource_manager import ResourceManager
46 class BookingContextData(object):
47 def prepopulate(self, *args, **kwargs):
48 self.loginuser = instantiate_user(False, username=kwargs.get("login_username", "newtestuser"), password="testpassword")
49 instantiate_userprofile(self.loginuser)
51 lab_user = kwargs.get("lab_user", instantiate_user(True))
52 self.lab = instantiate_lab(lab_user)
54 self.host_profile = make_hostprofile_set(self.lab)
55 self.scenario = instantiate_scenario()
56 self.installer = instantiate_installer([self.scenario])
57 os = instantiate_os([self.installer])
58 self.image = instantiate_image(self.lab, 1, self.loginuser, os, self.host_profile)
59 self.host = instantiate_host(self.host_profile, self.lab)
60 self.role = instantiate_opnfvrole()
61 self.pubnet = instantiate_publicnet(10, self.lab)
65 Info for instantiate_booking() function:
66 [topology] argument structure:
67 the [topology] argument should describe the structure of the pod
68 the top level should be a dictionary, with each key being a hostname
69 each value in the top level should be a dictionary with two keys:
70 "type" should map to a host profile instance
71 "nets" should map to a list of interfaces each with a list of
72 dictionaries each defining a network in the format
73 { "name": "netname", "tagged": True|False, "public": True|False }
74 each network is defined if a matching name is not found
76 sample argument structure:
79 "type": instanceOf HostProfile,
80 "role": instanceOf OPNFVRole
81 "image": instanceOf Image
84 0: { "name": "public", "tagged": True, "public": True },
85 1: { "name": "private", "tagged": False, "public": False },
94 def instantiate_booking(owner,
98 lab=Lab.objects.first(),
99 purpose="purposetext",
100 project="projecttext",
105 (grb, host_set) = instantiate_grb(topology, owner, lab, booking_identifier)
106 cb = instantiate_cb(grb, owner, booking_identifier, topology, host_set, installer, scenario)
108 resource = ResourceManager.getInstance().convertResourceBundle(grb, lab, cb)
112 booking.resource = resource
114 raise Exception("Resource not created")
115 booking.config_bundle = cb
116 booking.start = start
118 booking.owner = owner
119 booking.purpose = purpose
120 booking.project = project
127 def instantiate_cb(grb,
136 cb.name = str(booking_identifier) + "_cb"
137 cb.description = "cb generated by instantiate_cb() method"
140 opnfvconfig = OPNFVConfig()
141 opnfvconfig.installer = installer
142 opnfvconfig.scenario = scenario
143 opnfvconfig.bundle = cb
146 # generate host configurations based on topology and host set
147 for hostname, host_info in topology.items():
148 hconf = HostConfiguration()
150 hconf.host = host_set[hostname]
151 hconf.image = host_info["image"]
152 hconf.opnfvRole = host_info["role"]
157 def instantiate_grb(topology,
162 grb = GenericResourceBundle(owner=owner, lab=lab)
163 grb.name = str(booking_identifier) + "_grb"
164 grb.description = "grb generated by instantiate_grb() method"
170 for hostname in topology.keys():
171 info = topology[hostname]
172 host_profile = info["type"]
174 # need to construct host from hostname and type
175 ghost = instantiate_ghost(grb, host_profile, hostname)
176 host_set[hostname] = ghost
182 for interface_index, interface_profile in enumerate(host_profile.interfaceprofile.all()):
183 generic_interface = GenericInterface()
184 generic_interface.host = ghost
185 generic_interface.profile = interface_profile
186 generic_interface.save()
188 netconfig = nets[interface_index]
189 for network_index, network_info in enumerate(netconfig):
190 network_name = network_info["name"]
192 if network_name in networks:
193 network = networks[network_name]
196 network.name = network_name
197 network.vlan_id = lab.vlan_manager.get_vlan()
199 networks[network_name] = network
200 if network_info["public"]:
201 public_net = lab.vlan_manager.get_public_vlan()
203 raise Exception("No more public networks available")
204 lab.vlan_manager.reserve_public_vlan(public_net.vlan)
205 network.vlan_id = public_net.vlan
207 private_net = lab.vlan_manager.get_vlan()
209 raise Exception("No more generic vlans are available")
210 lab.vlan_manager.reserve_vlans([private_net])
211 network.vlan_id = private_net
214 vlan.vlan_id = network.vlan_id
215 vlan.public = network_info["public"]
216 vlan.tagged = network_info["tagged"]
218 generic_interface.vlans.add(vlan)
220 return (grb, host_set)
223 def instantiate_ghost(grb, host_profile, hostname):
224 if not re.match(r"(?=^.{1,253}$)(^([A-Za-z0-9-_]{1,62}\.)*[A-Za-z0-9-_]{1,63})$", hostname):
225 raise InvalidHostnameException("Hostname must comply to RFC 952 and all extensions to it until this point")
226 gresource = GenericResource(bundle=grb, name=hostname)
229 ghost = GenericHost()
230 ghost.resource = gresource
231 ghost.profile = host_profile
237 def instantiate_user(is_superuser,
239 password="testpassword",
240 email="default_email@user.com"
242 user = User.objects.create_user(username=username, email=email, password=password)
243 user.is_superuser = is_superuser
250 def instantiate_userprofile(user, email_addr="email@email.com", company="company", full_name="John Doe", booking_privledge=True, ssh_file=None):
252 up.email_address = email_addr
254 up.full_name = full_name
255 up.booking_privledge = booking_privledge
258 up.ssh_public_key.save("user_ssh_key", ssh_file if ssh_file else ContentFile("public key content string"))
263 def instantiate_vlanmanager(vlans=None,
265 allow_overlapping=False,
268 vlanmanager = VlanManager()
271 for vlan in range(0, 4095):
272 vlans.append(vlan % 2)
273 vlanmanager.vlans = json.dumps(vlans)
274 if not reserved_vlans:
276 for vlan in range(0, 4095):
277 reserved_vlans.append(0)
278 vlanmanager.reserved_vlans = json.dumps(vlans)
279 vlanmanager.block_size = block_size
280 vlanmanager.allow_overlapping = allow_overlapping
287 def instantiate_lab(user=None,
288 name="Test Lab Instance",
293 vlan_manager = instantiate_vlanmanager()
296 user = instantiate_user(True, 'test_user', 'test_pass', 'test_user@test_site.org')
301 lab.contact_email = 'test_lab@test_site.org'
302 lab.contact_phone = '603 123 4567'
304 lab.vlan_manager = vlan_manager
305 lab.description = 'test lab instantiation'
306 lab.api_token = '12345678'
314 resource_inventory instantiation section for permenant resources
318 def make_hostprofile_set(lab, name="test_hostprofile"):
319 hostprof = instantiate_hostprofile(lab, name=name)
320 instantiate_diskprofile(hostprof, 500, name=name)
321 instantiate_cpuprofile(hostprof)
322 instantiate_interfaceprofile(hostprof, name=name)
323 instantiate_ramprofile(hostprof)
328 def instantiate_hostprofile(lab,
330 name="test hostprofile instance"
332 hostprof = HostProfile()
333 hostprof.host_type = host_type
335 hostprof.description = 'test hostprofile instance'
337 hostprof.labs.add(lab)
344 def instantiate_ramprofile(host,
347 ramprof = RamProfile()
349 ramprof.amount = amount
350 ramprof.channels = channels
356 def instantiate_diskprofile(hostprofile,
359 name="test diskprofile",
363 diskprof = DiskProfile()
366 diskprof.media_type = media_type
367 diskprof.host = hostprofile
368 diskprof.rotation = rotation
369 diskprof.interface = interface
376 def instantiate_cpuprofile(hostprofile,
378 architecture="x86_64",
381 cpuprof = CpuProfile()
382 cpuprof.cores = cores
383 cpuprof.architecture = architecture
385 cpuprof.host = hostprofile
393 def instantiate_interfaceprofile(hostprofile,
395 name="test interface profile",
398 intprof = InterfaceProfile()
399 intprof.host = hostprofile
401 intprof.speed = speed
402 intprof.nic_type = nic_type
409 def instantiate_image(lab,
415 name="default image",
416 description="default image"
420 image.lab_id = lab_id
422 image.host_type = host_profile
423 image.public = public
425 image.description = description
432 def instantiate_scenario(name="test scenario"):
433 scenario = Scenario()
439 def instantiate_installer(supported_scenarios,
440 name="test installer"
442 installer = Installer()
443 installer.name = name
445 for scenario in supported_scenarios:
446 installer.sup_scenarios.add(scenario)
452 def instantiate_os(supported_installers,
453 name="test operating system",
458 for installer in supported_installers:
459 os.sup_installers.add(installer)
464 def instantiate_host(host_profile,
477 host.profile = host_profile
480 host.working = working
482 host.template = template
492 def instantiate_opnfvrole(name="Jumphost",
493 description="test opnfvrole"):
496 role.description = description
502 def instantiate_publicnet(vlan,
507 pubnet = PublicNetwork()
511 pubnet.gateway = gateway