Matteo Scandolo | 5e293c9 | 2017-08-08 13:05:23 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 17 | def __init__(self, *args, **kwargs): |
Scott Baker | ea848b2 | 2017-05-25 09:13:47 -0700 | [diff] [blame] | 18 | volt_services = VOLTService.objects.all() |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 19 | if volt_services: |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 20 | self._meta.get_field("owner").default = volt_services[0].id |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 21 | super(VOLTTenant, self).__init__(*args, **kwargs) |
| 22 | self.cached_vcpe = None |
| 23 | |
| 24 | @property |
| 25 | def vcpe(self): |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 26 | # TODO: hardcoded service dependency |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 27 | from services.vsg.models import VSGTenant |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 28 | |
| 29 | vsg = None |
| 30 | for link in self.subscribed_links: |
| 31 | # cast from base class to derived class |
| 32 | vsgs = VSGTenant.objects.filter(serviceinstance_ptr=link.provider_service_instance) |
| 33 | if vsgs: |
| 34 | vsg = vsgs[0] |
| 35 | |
| 36 | if not vsg: |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 37 | return None |
| 38 | |
| 39 | # always return the same object when possible |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 40 | if (self.cached_vcpe) and (self.cached_vcpe.id == vsg.id): |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 41 | return self.cached_vcpe |
| 42 | |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 43 | vsg.caller = self.creator |
| 44 | self.cached_vcpe = vsg |
| 45 | return vsg |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 46 | |
| 47 | @vcpe.setter |
| 48 | def vcpe(self, value): |
| 49 | raise XOSConfigurationError("vOLT.vCPE cannot be set this way -- create a new vCPE object and set its subscriber_tenant instead") |
| 50 | |
| 51 | @property |
| 52 | def subscriber(self): |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 53 | for link in self.provided_links: |
| 54 | # cast from base class to derived class |
| 55 | roots = CordSubscriberRoot.objects.filter(serviceinstance_ptr=link.subscriber_service_instance) |
| 56 | if roots: |
| 57 | return roots[0] |
| 58 | return None |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 59 | |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 60 | def save(self, *args, **kwargs): |
Sapan Bhatia | 8fc7eb3 | 2017-05-05 20:43:43 +0200 | [diff] [blame] | 61 | if not self.creator: |
| 62 | if not getattr(self, "caller", None): |
| 63 | # caller must be set when creating a vCPE since it creates a slice |
| 64 | raise XOSProgrammingError("VOLTTenant's self.caller was not set") |
| 65 | self.creator = self.caller |
| 66 | if not self.creator: |
| 67 | raise XOSProgrammingError("VOLTTenant's self.creator was not set") |
| 68 | |
| 69 | super(VOLTTenant, self).save(*args, **kwargs) |