blob: 33ab553b914bf3f38282e07e4ba64395384bcc13 [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
14from xosapi.orm import ORMWrapper, register_convenience_wrapper
15
Matteo Scandoloe3d2f262018-06-05 17:45:39 -070016from xosconfig import Config
17from multistructlog import create_logger
18
Zack Williams045b63d2019-01-22 16:30:57 -070019log = create_logger(Config().get("logging"))
20
Matteo Scandolo78937592018-04-05 11:34:57 -070021
22class ORMWrapperServiceInstance(ORMWrapper):
Matteo Scandolo78937592018-04-05 11:34:57 -070023 @property
24 def serviceinstanceattribute_dict(self):
25 attrs = {}
26 for attr in self.service_instance_attributes.all():
27 attrs[attr.name] = attr.value
28 return attrs
29
30 @property
31 def tenantattribute_dict(self):
32 log.warn("tenantattribute_dict method is deprecated")
33 return self.serviceinstanceattribute_dict
34
35 @property
36 def westbound_service_instances(self):
37 links = self.provided_links.all()
38 instances = []
39 for link in links:
40 instances.append(link.subscriber_service_instance.leaf_model)
41
42 return instances
43
44 @property
45 def eastbound_service_instances(self):
Matteo Scandolo8a18e082018-06-11 13:28:47 -070046 links = self.subscribed_links.all()
Matteo Scandolo78937592018-04-05 11:34:57 -070047 instances = []
48 for link in links:
49 instances.append(link.provider_service_instance.leaf_model)
50
51 return instances
52
53 def create_eastbound_instance(self):
54
55 # Already has a chain
56 if len(self.eastbound_service_instances) > 0 and not self.is_new:
57 log.debug("MODEL_POLICY: Subscriber %s is already part of a chain" % si.id)
58 return
59
60 # if it does not have a chain,
61 # Find links to the next element in the service chain
62 # and create one
63
64 links = self.owner.subscribed_dependencies.all()
65
66 for link in links:
67 si_class = link.provider_service.get_service_instance_class_name()
68 log.info(" %s creating %s" % (self.model_name, si_class))
69
70 eastbound_si_class = model_accessor.get_model_class(si_class)
71 eastbound_si = eastbound_si_class()
72 eastbound_si.owner_id = link.provider_service_id
73 eastbound_si.save()
Zack Williams045b63d2019-01-22 16:30:57 -070074 link = ServiceInstanceLink(
75 provider_service_instance=eastbound_si, subscriber_service_instance=si
76 )
Matteo Scandolo78937592018-04-05 11:34:57 -070077 link.save()
78
Scott Baker2f314d52018-08-24 08:31:19 -070079 def get_westbound_service_instance_properties(self, prop_name, include_self=False):
80 if include_self and hasattr(self, prop_name):
81 return getattr(self, prop_name)
82
Matteo Scandolo78937592018-04-05 11:34:57 -070083 wi = self.westbound_service_instances
84
85 if len(wi) == 0:
Zack Williams045b63d2019-01-22 16:30:57 -070086 log.error(
87 "ServiceInstance with id %s has no westbound service instances, can't find property %s in the chain"
88 % (self.id, prop_name)
89 )
90 raise Exception(
91 "ServiceInstance with id %s has no westbound service instances"
92 % self.id
93 )
Matteo Scandolo78937592018-04-05 11:34:57 -070094
95 for i in wi:
96 if hasattr(i, prop_name):
97 return getattr(i, prop_name)
98 else:
Matteo Scandolo5d607282018-04-11 09:19:25 -070099 # cast to the ServiceInstance model
100 i = self.stub.ServiceInstance.objects.get(id=i.id)
Matteo Scandolo78937592018-04-05 11:34:57 -0700101 return i.get_westbound_service_instance_properties(prop_name)
102
Zack Williams045b63d2019-01-22 16:30:57 -0700103
104register_convenience_wrapper("ServiceInstance", ORMWrapperServiceInstance)