blob: c7a3420875727c09c1db789ed8c3062a96829121 [file] [log] [blame]
Andy Bavier03df22b2017-08-30 14:46:02 -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
17def __init__(self, *args, **kwargs):
18 volt_services = VOLTService.objects.all()
19 if volt_services:
20 self._meta.get_field("owner").default = volt_services[0].id
21 super(VOLTTenant, self).__init__(*args, **kwargs)
22 self.cached_vcpe = None
23
24@property
25def vcpe(self):
26 # TODO: hardcoded service dependency
27 from services.vsg.models import VSGTenant
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:
37 return None
38
39 # always return the same object when possible
40 if (self.cached_vcpe) and (self.cached_vcpe.id == vsg.id):
41 return self.cached_vcpe
42
43 vsg.caller = self.creator
44 self.cached_vcpe = vsg
45 return vsg
46
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):
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
59
60def save(self, *args, **kwargs):
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)