d174b2eb22f3d8cb3e2bce57d104dee50c48f60c
[plstackapi.git] / plstackapi / core / models / slice.py
1 import os
2 from django.db import models
3 from plstackapi.core.models import PlCoreBase
4 from plstackapi.core.models import Site
5 from plstackapi.core.models import PLUser
6 from plstackapi.core.models import Role
7 from plstackapi.core.models import DeploymentNetwork
8
9 # Create your models here.
10
11 class Slice(PlCoreBase):
12     tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
13     name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
14     enabled = models.BooleanField(default=True, help_text="Status for this Slice")
15     SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
16     instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
17     omf_friendly = models.BooleanField()
18     description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
19     slice_url = models.URLField(blank=True, max_length=512)
20     site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
21     network_id = models.CharField(max_length=256, help_text="Quantum network")
22     router_id = models.CharField(max_length=256, help_text="Quantum router id")
23
24     def __unicode__(self):  return u'%s' % (self.name)
25
26     def save(self, *args, **kwds):
27         if not self.tenant_id:
28             nova_fields = {'tenant_name': self.name,
29                    'description': self.description,
30                    'enabled': self.enabled}
31             tenant = self.driver.create_tenant(**nova_fields)
32             self.tenant_id = tenant.id
33
34             # create network
35             network = self.driver.create_network(self.name)
36             self.network_id = network['id']
37
38             # create router
39             router = self.driver.create_router(self.name)
40             self.router_id = router['id']
41
42         super(Slice, self).save(*args, **kwds)
43
44     def delete(self, *args, **kwds):
45         if self.tenant_id:
46             self.driver.delete_router(self.router_id)
47             self.driver.delete_network(self.network_id)
48             self.driver.delete_tenant(self.tenant_id)
49
50         super(Slice, self).delete(*args, **kwds)    
51
52 class SliceMembership(PlCoreBase):
53     user = models.ForeignKey('PLUser', related_name='slice_memberships')
54     slice = models.ForeignKey('Slice', related_name='slice_memberships')
55     role = models.ForeignKey('Role')
56
57     def __unicode__(self):  return u'%s %s %s' % (self.slice, self.user, self.role)
58