blob: 10adb1632188c12fa84aa6e1b1096f62e57e8afd [file] [log] [blame]
Matteo Scandolo78937592018-04-05 11:34:57 -07001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12
13
Zack Williams5c2ea232019-01-30 15:23:01 -070014from __future__ import absolute_import
Matteo Scandolo78937592018-04-05 11:34:57 -070015
Matteo Scandoloe3d2f262018-06-05 17:45:39 -070016from multistructlog import create_logger
Zack Williams5c2ea232019-01-30 15:23:01 -070017from xosapi.orm import ORMWrapper, register_convenience_wrapper
18from xosconfig import Config
Matteo Scandoloe3d2f262018-06-05 17:45:39 -070019
Zack Williams045b63d2019-01-22 16:30:57 -070020log = create_logger(Config().get("logging"))
21
Matteo Scandolo78937592018-04-05 11:34:57 -070022
23class ORMWrapperServiceInstance(ORMWrapper):
Matteo Scandolo78937592018-04-05 11:34:57 -070024 @property
25 def serviceinstanceattribute_dict(self):
26 attrs = {}
27 for attr in self.service_instance_attributes.all():
28 attrs[attr.name] = attr.value
29 return attrs
30
31 @property
Matteo Scandolo78937592018-04-05 11:34:57 -070032 def westbound_service_instances(self):
33 links = self.provided_links.all()
34 instances = []
35 for link in links:
36 instances.append(link.subscriber_service_instance.leaf_model)
37
38 return instances
39
40 @property
41 def eastbound_service_instances(self):
Matteo Scandolo8a18e082018-06-11 13:28:47 -070042 links = self.subscribed_links.all()
Matteo Scandolo78937592018-04-05 11:34:57 -070043 instances = []
44 for link in links:
45 instances.append(link.provider_service_instance.leaf_model)
46
47 return instances
48
Scott Baker2f314d52018-08-24 08:31:19 -070049 def get_westbound_service_instance_properties(self, prop_name, include_self=False):
50 if include_self and hasattr(self, prop_name):
51 return getattr(self, prop_name)
52
Matteo Scandolo78937592018-04-05 11:34:57 -070053 wi = self.westbound_service_instances
54
55 if len(wi) == 0:
Zack Williams045b63d2019-01-22 16:30:57 -070056 log.error(
57 "ServiceInstance with id %s has no westbound service instances, can't find property %s in the chain"
58 % (self.id, prop_name)
59 )
60 raise Exception(
61 "ServiceInstance with id %s has no westbound service instances"
62 % self.id
63 )
Matteo Scandolo78937592018-04-05 11:34:57 -070064
65 for i in wi:
66 if hasattr(i, prop_name):
67 return getattr(i, prop_name)
68 else:
Matteo Scandolo5d607282018-04-11 09:19:25 -070069 # cast to the ServiceInstance model
70 i = self.stub.ServiceInstance.objects.get(id=i.id)
Matteo Scandolo78937592018-04-05 11:34:57 -070071 return i.get_westbound_service_instance_properties(prop_name)
72
Zack Williams045b63d2019-01-22 16:30:57 -070073
74register_convenience_wrapper("ServiceInstance", ORMWrapperServiceInstance)