aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/models.py')
-rw-r--r--src/api/models.py151
1 files changed, 120 insertions, 31 deletions
diff --git a/src/api/models.py b/src/api/models.py
index f8b8f89..1f708ae 100644
--- a/src/api/models.py
+++ b/src/api/models.py
@@ -12,6 +12,7 @@ from django.contrib.auth.models import User
from django.db import models
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404
+from django.urls import reverse
import json
import uuid
@@ -23,8 +24,12 @@ from resource_inventory.models import (
Host,
Image,
Interface,
- RemoteInfo
+ HostOPNFVConfig,
+ RemoteInfo,
+ OPNFVConfig
)
+from resource_inventory.idf_templater import IDFTemplater
+from resource_inventory.pdf_templater import PDFTemplater
class JobStatus(object):
@@ -86,8 +91,23 @@ class LabManager(object):
remote_info.save()
host.remote_management = remote_info
host.save()
+ booking = Booking.objects.get(resource=host.bundle)
+ self.update_xdf(booking)
return {"status": "success"}
+ def update_xdf(self, booking):
+ booking.pdf = PDFTemplater.makePDF(booking)
+ booking.idf = IDFTemplater().makeIDF(booking)
+ booking.save()
+
+ def get_pdf(self, booking_id):
+ booking = get_object_or_404(Booking, pk=booking_id, lab=self.lab)
+ return booking.pdf
+
+ def get_idf(self, booking_id):
+ booking = get_object_or_404(Booking, pk=booking_id, lab=self.lab)
+ return booking.idf
+
def get_profile(self):
prof = {}
prof['name'] = self.lab.name
@@ -341,25 +361,71 @@ class TaskConfig(models.Model):
self.delta = '{}'
+class BridgeConfig(models.Model):
+ """
+ Displays mapping between jumphost interfaces and
+ bridges
+ """
+ interfaces = models.ManyToManyField(Interface)
+ opnfv_config = models.ForeignKey(OPNFVConfig, on_delete=models.CASCADE)
+
+ def to_dict(self):
+ d = {}
+ hid = self.interfaces.first().host.labid
+ d[hid] = {}
+ for interface in self.interfaces.all():
+ d[hid][interface.mac_address] = []
+ for vlan in interface.config.all():
+ network_role = self.opnfv_model.networks().filter(network=vlan.network)
+ bridge = IDFTemplater.bridge_names[network_role.name]
+ br_config = {
+ "vlan_id": vlan.vlan_id,
+ "tagged": vlan.tagged,
+ "bridge": bridge
+ }
+ d[hid][interface.mac_address].append(br_config)
+ return d
+
+ def to_json(self):
+ return json.dumps(self.to_dict())
+
+
class OpnfvApiConfig(models.Model):
installer = models.CharField(max_length=200)
scenario = models.CharField(max_length=300)
roles = models.ManyToManyField(Host)
+ # pdf and idf are url endpoints, not the actual file
+ pdf = models.CharField(max_length=100)
+ idf = models.CharField(max_length=100)
+ bridge_config = models.OneToOneField(BridgeConfig, on_delete=models.CASCADE, null=True)
delta = models.TextField()
+ opnfv_config = models.ForeignKey(OPNFVConfig, null=True, on_delete=models.SET_NULL)
def to_dict(self):
d = {}
+ if not self.opnfv_config:
+ return d
if self.installer:
d['installer'] = self.installer
if self.scenario:
d['scenario'] = self.scenario
+ if self.pdf:
+ d['pdf'] = self.pdf
+ if self.idf:
+ d['idf'] = self.idf
+ if self.bridge_config:
+ d['bridged_interfaces'] = self.bridge_config.to_dict()
hosts = self.roles.all()
if hosts.exists():
d['roles'] = []
- for host in self.roles.all():
- d['roles'].append({host.labid: host.config.opnfvRole.name})
+ for host in hosts:
+ d['roles'].append({
+ host.labid: self.opnfv_config.host_opnfv_config.get(
+ host_config__pk=host.config.pk
+ ).role.name
+ })
return d
@@ -378,6 +444,16 @@ class OpnfvApiConfig(models.Model):
d['scenario'] = scenario
self.delta = json.dumps(d)
+ def set_xdf(self, booking, update_delta=True):
+ kwargs = {'lab_name': booking.lab.name, 'booking_id': booking.id}
+ self.pdf = reverse('get-pdf', kwargs=kwargs)
+ self.idf = reverse('get-idf', kwargs=kwargs)
+ if update_delta:
+ d = json.loads(self.delta)
+ d['pdf'] = self.pdf
+ d['idf'] = self.idf
+ self.delta = json.dumps(d)
+
def add_role(self, host):
self.roles.add(host)
d = json.loads(self.delta)
@@ -615,6 +691,7 @@ class SnapshotConfig(TaskConfig):
if not self.delta:
self.delta = self.to_json()
self.save()
+
d = json.loads(self.delta)
return d
@@ -765,14 +842,12 @@ class JobFactory(object):
net_relation.status = JobStatus.NEW
# re-apply ssh access after host is reset
- ssh_relation = AccessRelation.objects.get(job=job, config__access_type="ssh")
- ssh_relation.status = JobStatus.NEW
+ for relation in AccessRelation.objects.filter(job=job, config__access_type="ssh"):
+ relation.status = JobStatus.NEW
+ relation.save()
- # save them all at once to reduce the chance
- # of a lab polling and only seeing partial change
hardware_relation.save()
net_relation.save()
- ssh_relation.save()
@classmethod
def makeSnapshotTask(cls, image, booking, host):
@@ -808,7 +883,7 @@ class JobFactory(object):
job=job
)
cls.makeSoftware(
- hosts=hosts,
+ booking=booking,
job=job
)
all_users = list(booking.collaborators.all())
@@ -899,28 +974,42 @@ class JobFactory(object):
network_config.save()
@classmethod
- def makeSoftware(cls, hosts=[], job=Job()):
- def init_config(host):
- opnfv_config = OpnfvApiConfig()
- if host is not None:
- opnfv = host.config.bundle.opnfv_config.first()
- opnfv_config.installer = opnfv.installer.name
- opnfv_config.scenario = opnfv.scenario.name
- opnfv_config.save()
- return opnfv_config
-
+ def make_bridge_config(cls, booking):
+ if booking.resource.hosts.count() < 2:
+ return None
try:
- host = None
- if len(hosts) > 0:
- host = hosts[0]
- opnfv_config = init_config(host)
-
- for host in hosts:
- opnfv_config.roles.add(host)
- software_config = SoftwareConfig.objects.create(opnfv=opnfv_config)
- software_config.save()
- software_relation = SoftwareRelation.objects.create(job=job, config=software_config)
- software_relation.save()
- return software_relation
+ jumphost_config = HostOPNFVConfig.objects.filter(
+ role__name__iexact="jumphost"
+ )
+ jumphost = Host.objects.get(
+ bundle=booking.resource,
+ config=jumphost_config.host_config
+ )
except Exception:
return None
+ br_config = BridgeConfig.objects.create(opnfv_config=booking.opnfv_config)
+ for iface in jumphost.interfaces.all():
+ br_config.interfaces.add(iface)
+ return br_config
+
+ @classmethod
+ def makeSoftware(cls, booking=None, job=Job()):
+
+ if not booking.opnfv_config:
+ return None
+
+ opnfv_api_config = OpnfvApiConfig.objects.create(
+ opnfv_config=booking.opnfv_config,
+ installer=booking.opnfv_config.installer.name,
+ scenario=booking.opnfv_config.scenario.name,
+ bridge_config=cls.make_bridge_config(booking)
+ )
+
+ opnfv_api_config.set_xdf(booking, False)
+ opnfv_api_config.save()
+
+ for host in booking.resource.hosts.all():
+ opnfv_api_config.roles.add(host)
+ software_config = SoftwareConfig.objects.create(opnfv=opnfv_api_config)
+ software_relation = SoftwareRelation.objects.create(job=job, config=software_config)
+ return software_relation