blob: c7a3420875727c09c1db789ed8c3062a96829121 [file] [log] [blame]
Matteo Scandolo5e293c92017-08-08 13:05:23 -07001
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 Bhatia8fc7eb32017-05-05 20:43:43 +020017def __init__(self, *args, **kwargs):
Scott Bakerea848b22017-05-25 09:13:47 -070018 volt_services = VOLTService.objects.all()
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020019 if volt_services:
Scott Bakerd7590f72017-07-18 11:37:22 -070020 self._meta.get_field("owner").default = volt_services[0].id
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020021 super(VOLTTenant, self).__init__(*args, **kwargs)
22 self.cached_vcpe = None
23
24@property
25def vcpe(self):
Scott Bakerd7590f72017-07-18 11:37:22 -070026 # TODO: hardcoded service dependency
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020027 from services.vsg.models import VSGTenant
Scott Bakerd7590f72017-07-18 11:37:22 -070028
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 Bhatia8fc7eb32017-05-05 20:43:43 +020037 return None
38
39 # always return the same object when possible
Scott Bakerd7590f72017-07-18 11:37:22 -070040 if (self.cached_vcpe) and (self.cached_vcpe.id == vsg.id):
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020041 return self.cached_vcpe
42
Scott Bakerd7590f72017-07-18 11:37:22 -070043 vsg.caller = self.creator
44 self.cached_vcpe = vsg
45 return vsg
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020046
47@vcpe.setter
48def 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
52def subscriber(self):
Scott Bakerd7590f72017-07-18 11:37:22 -070053 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 Bhatia8fc7eb32017-05-05 20:43:43 +020059
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020060def save(self, *args, **kwargs):
Sapan Bhatia8fc7eb32017-05-05 20:43:43 +020061 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)