Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 1 | def __init__(self, *args, **kwargs): |
Scott Baker | ea848b2 | 2017-05-25 09:13:47 -0700 | [diff] [blame] | 2 | volt_services = VOLTService.objects.all() |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 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 |
| 9 | def 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 |
| 24 | def 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 |
| 28 | def 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 | |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 36 | def save(self, *args, **kwargs): |
| 37 | # VOLTTenant probably doesn't need a SSID anymore; that will be handled |
| 38 | # by CORDSubscriberRoot... |
| 39 | # self.validate_unique_service_specific_id() |
| 40 | |
| 41 | if (self.subscriber_root is not None): |
| 42 | subs = self.subscriber_root.get_subscribed_tenants(VOLTTenant) |
| 43 | if (subs) and (self not in subs): |
| 44 | raise XOSDuplicateKey("Subscriber should only be linked to one vOLT") |
| 45 | |
| 46 | if not self.creator: |
| 47 | if not getattr(self, "caller", None): |
| 48 | # caller must be set when creating a vCPE since it creates a slice |
| 49 | raise XOSProgrammingError("VOLTTenant's self.caller was not set") |
| 50 | self.creator = self.caller |
| 51 | if not self.creator: |
| 52 | raise XOSProgrammingError("VOLTTenant's self.creator was not set") |
| 53 | |
| 54 | super(VOLTTenant, self).save(*args, **kwargs) |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 55 | |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 56 | |