Matteo Scandolo | 11fc7e5 | 2018-04-24 12:55:14 +0200 | [diff] [blame^] | 1 | |
| 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 | |
| 17 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 18 | |
| 19 | class ORMWrapperVSGServiceInstance(ORMWrapper): |
| 20 | sync_attributes = ("wan_container_ip", "wan_container_mac", "wan_container_netbits", |
| 21 | "wan_container_gateway_ip", "wan_container_gateway_mac", |
| 22 | "wan_vm_ip", "wan_vm_mac") |
| 23 | |
| 24 | @property |
| 25 | def ingress_service_instance(self): |
| 26 | links = self.provided_links.all() |
| 27 | for link in links: |
| 28 | subscriber_service_instance = link.subscriber_service_instance.leaf_model |
| 29 | # Look for something that has an s_tag attribute |
| 30 | if (hasattr(subscriber_service_instance, "s_tag")): |
| 31 | return subscriber_service_instance |
| 32 | return None |
| 33 | |
| 34 | @property |
| 35 | def volt(self): |
| 36 | return self.ingress_service_instance |
| 37 | |
| 38 | def is_address_manager_service_instance(self, si): |
| 39 | # TODO: hardcoded dependency |
| 40 | # TODO: VRouterTenant is deprecated |
| 41 | return si.leaf_model_name in ["AddressManagerServiceInstance", "VRouterTenant"] |
| 42 | |
| 43 | # DEPRECATED |
| 44 | @property |
| 45 | def vrouter(self): |
| 46 | return self.address_service_instance |
| 47 | |
| 48 | @property |
| 49 | def address_service_instance(self): |
| 50 | links = self.subscribed_links.all() |
| 51 | for link in links: |
| 52 | if not self.is_address_manager_service_instance(link.provider_service_instance): |
| 53 | continue |
| 54 | # cast from ServiceInstance to AddressManagerServiceInstance or similar |
| 55 | return link.provider_service_instance.leaf_model |
| 56 | return None |
| 57 | |
| 58 | def get_address_service_instance_field(self, name, default=None): |
| 59 | if self.address_service_instance: |
| 60 | return getattr(self.address_service_instance, name, default) |
| 61 | else: |
| 62 | return default |
| 63 | |
| 64 | @property |
| 65 | def wan_container_ip(self): |
| 66 | return self.get_address_service_instance_field("public_ip", None) |
| 67 | |
| 68 | @property |
| 69 | def wan_container_mac(self): |
| 70 | return self.get_address_service_instance_field("public_mac", None) |
| 71 | |
| 72 | @property |
| 73 | def wan_container_netbits(self): |
| 74 | return self.get_address_service_instance_field("netbits", None) |
| 75 | |
| 76 | @property |
| 77 | def wan_container_gateway_ip(self): |
| 78 | return self.get_address_service_instance_field("gateway_ip", None) |
| 79 | |
| 80 | @property |
| 81 | def wan_container_gateway_mac(self): |
| 82 | return self.get_address_service_instance_field("gateway_mac", None) |
| 83 | |
| 84 | @property |
| 85 | def wan_vm_ip(self): |
| 86 | tags = self.stub.Tag.objects.filter(name="vm_vrouter_tenant", object_id=self.instance.id, content_type=self.instance.self_content_type_id) |
| 87 | if tags: |
| 88 | service_instances = self.stub.ServiceInstance.objects.filter(id=int(tags[0].value)) |
| 89 | if not service_instances: |
| 90 | raise Exception("ServiceInstance %d linked to vsg %s does not exist" % (int(tags[0].value), self)) |
| 91 | return service_instances[0].leaf_model.public_ip |
| 92 | else: |
| 93 | raise Exception("no vm_vrouter_tenant tag for instance %s" % self.instance) |
| 94 | |
| 95 | @property |
| 96 | def wan_vm_mac(self): |
| 97 | tags = self.stub.Tag.objects.filter(name="vm_vrouter_tenant", object_id=self.instance.id, content_type=self.instance.self_content_type_id) |
| 98 | if tags: |
| 99 | service_instances = self.stub.ServiceInstance.objects.filter(id=int(tags[0].value)) |
| 100 | if not service_instances: |
| 101 | raise Exception("ServiceInstance %d linked to vsg %s does not exist" % (int(tags[0].value), self)) |
| 102 | return service_instances[0].leaf_model.public_mac |
| 103 | else: |
| 104 | raise Exception("no vm_vrouter_tenant tag for instance %s" % self.instance) |
| 105 | |
| 106 | |
| 107 | register_convenience_wrapper("VSGTenant", ORMWrapperVSGServiceInstance) # DEPRECATED |
| 108 | register_convenience_wrapper("VSGServiceInstance", ORMWrapperVSGServiceInstance) |