Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 1 | # 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 | |
| 14 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 15 | |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 16 | from xosconfig import Config |
| 17 | from multistructlog import create_logger |
| 18 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 19 | log = create_logger(Config().get("logging")) |
| 20 | |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 21 | |
| 22 | class ORMWrapperServiceInstance(ORMWrapper): |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 23 | @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 Scandolo | 8a18e08 | 2018-06-11 13:28:47 -0700 | [diff] [blame] | 46 | links = self.subscribed_links.all() |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 47 | 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 74 | link = ServiceInstanceLink( |
| 75 | provider_service_instance=eastbound_si, subscriber_service_instance=si |
| 76 | ) |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 77 | link.save() |
| 78 | |
Scott Baker | 2f314d5 | 2018-08-24 08:31:19 -0700 | [diff] [blame] | 79 | 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 Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 83 | wi = self.westbound_service_instances |
| 84 | |
| 85 | if len(wi) == 0: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | 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 Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 94 | |
| 95 | for i in wi: |
| 96 | if hasattr(i, prop_name): |
| 97 | return getattr(i, prop_name) |
| 98 | else: |
Matteo Scandolo | 5d60728 | 2018-04-11 09:19:25 -0700 | [diff] [blame] | 99 | # cast to the ServiceInstance model |
| 100 | i = self.stub.ServiceInstance.objects.get(id=i.id) |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 101 | return i.get_westbound_service_instance_properties(prop_name) |
| 102 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 103 | |
| 104 | register_convenience_wrapper("ServiceInstance", ORMWrapperServiceInstance) |