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 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 14 | from __future__ import absolute_import |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 15 | |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 16 | from multistructlog import create_logger |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 17 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 18 | from xosconfig import Config |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 19 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 20 | log = create_logger(Config().get("logging")) |
| 21 | |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 22 | |
| 23 | class ORMWrapperServiceInstance(ORMWrapper): |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 24 | @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 Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 32 | 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 Scandolo | 8a18e08 | 2018-06-11 13:28:47 -0700 | [diff] [blame] | 42 | links = self.subscribed_links.all() |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 43 | instances = [] |
| 44 | for link in links: |
| 45 | instances.append(link.provider_service_instance.leaf_model) |
| 46 | |
| 47 | return instances |
| 48 | |
Scott Baker | 2f314d5 | 2018-08-24 08:31:19 -0700 | [diff] [blame] | 49 | 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 Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 53 | wi = self.westbound_service_instances |
| 54 | |
| 55 | if len(wi) == 0: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | 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 Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 64 | |
| 65 | for i in wi: |
| 66 | if hasattr(i, prop_name): |
| 67 | return getattr(i, prop_name) |
| 68 | else: |
Matteo Scandolo | 5d60728 | 2018-04-11 09:19:25 -0700 | [diff] [blame] | 69 | # cast to the ServiceInstance model |
| 70 | i = self.stub.ServiceInstance.objects.get(id=i.id) |
Matteo Scandolo | 7893759 | 2018-04-05 11:34:57 -0700 | [diff] [blame] | 71 | return i.get_westbound_service_instance_properties(prop_name) |
| 72 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 73 | |
| 74 | register_convenience_wrapper("ServiceInstance", ORMWrapperServiceInstance) |