blob: e9a101bbecdedec76168e8a1cfcd8f0ed1ef5533 [file] [log] [blame]
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +02001def __init__(self, *args, **kwargs):
2 volt_services = VOLTService.get_service_objects().all()
3 if volt_services:
4 self._meta.get_field("provider_service").default = volt_services[0].id
5 super(VOLTTenant, self).__init__(*args, **kwargs)
6 self.cached_vcpe = None
7
8@property
9def vcpe(self):
10 from services.vsg.models import VSGTenant
11 vcpe = self.get_newest_subscribed_tenant(VSGTenant)
12 if not vcpe:
13 return None
14
15 # always return the same object when possible
16 if (self.cached_vcpe) and (self.cached_vcpe.id == vcpe.id):
17 return self.cached_vcpe
18
19 vcpe.caller = self.creator
20 self.cached_vcpe = vcpe
21 return vcpe
22
23@vcpe.setter
24def vcpe(self, value):
25 raise XOSConfigurationError("vOLT.vCPE cannot be set this way -- create a new vCPE object and set its subscriber_tenant instead")
26
27@property
28def subscriber(self):
29 if not self.subscriber_root:
30 return None
31 subs = CordSubscriberRoot.objects.filter(id=self.subscriber_root.id)
32 if not subs:
33 return None
34 return subs[0]
35
36def manage_vcpe(self):
37 # Each VOLT object owns exactly one VCPE object
38
39 if self.deleted:
40 return
41
42 # Check to see if the wrong s-tag is set. This can only happen if the
43 # user changed the s-tag after the VoltTenant object was created.
44 if self.vcpe and self.vcpe.instance:
45 s_tags = Tag.select_by_content_object(self.vcpe.instance).filter(name="s_tag")
46 if s_tags and (s_tags[0].value != str(self.s_tag)):
47 self.vcpe.delete()
48
49 if self.vcpe is None:
50 from services.vsg.models import VSGService, VSGTenant
51 vsgServices = VSGService.get_service_objects().all()
52 if not vsgServices:
53 raise XOSConfigurationError("No VSG Services available")
54
55 vcpe = VSGTenant(provider_service = vsgServices[0],
56 subscriber_tenant = self)
57 vcpe.caller = self.creator
58 vcpe.save()
59
60def manage_subscriber(self):
61 if (self.subscriber_root is None):
62 # The vOLT is not connected to a Subscriber, so either find an
63 # existing subscriber with the same SSID, or autogenerate a new
64 # subscriber.
65 #
66 # TODO: This probably goes away when we rethink the ONOS-to-XOS
67 # vOLT API.
68
69 subs = CordSubscriberRoot.get_tenant_objects().filter(service_specific_id = self.service_specific_id)
70 if subs:
71 sub = subs[0]
72 else:
73 sub = CordSubscriberRoot(service_specific_id = self.service_specific_id,
74 name = "autogenerated-for-vOLT-%s" % self.id)
75 sub.save()
76 self.subscriber_root = sub
77 self.save()
78
79def cleanup_vcpe(self):
80 if self.vcpe:
81 # print "XXX cleanup vcpe", self.vcpe
82 self.vcpe.delete()
83
84def cleanup_orphans(self):
85 from services.vsg.models import VSGTenant
86 # ensure vOLT only has one vCPE
87 cur_vcpe = self.vcpe
88 for vcpe in list(self.get_subscribed_tenants(VSGTenant)):
89 if (not cur_vcpe) or (vcpe.id != cur_vcpe.id):
90 # print "XXX clean up orphaned vcpe", vcpe
91 vcpe.delete()
92
93def save(self, *args, **kwargs):
94 # VOLTTenant probably doesn't need a SSID anymore; that will be handled
95 # by CORDSubscriberRoot...
96 # self.validate_unique_service_specific_id()
97
98 if (self.subscriber_root is not None):
99 subs = self.subscriber_root.get_subscribed_tenants(VOLTTenant)
100 if (subs) and (self not in subs):
101 raise XOSDuplicateKey("Subscriber should only be linked to one vOLT")
102
103 if not self.creator:
104 if not getattr(self, "caller", None):
105 # caller must be set when creating a vCPE since it creates a slice
106 raise XOSProgrammingError("VOLTTenant's self.caller was not set")
107 self.creator = self.caller
108 if not self.creator:
109 raise XOSProgrammingError("VOLTTenant's self.creator was not set")
110
111 super(VOLTTenant, self).save(*args, **kwargs)
112 model_policy_volt(self.pk)
113
114def delete(self, *args, **kwargs):
115 self.cleanup_vcpe()
116 super(VOLTTenant, self).delete(*args, **kwargs)
117