Matteo Scandolo | 4d925a5 | 2018-04-24 12:53:30 +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 | import json |
| 18 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 19 | |
| 20 | class ORMWrapperVRouterTenant(ORMWrapper): |
| 21 | @property |
| 22 | def gateway_ip(self): |
| 23 | if not self.address_pool: |
| 24 | return None |
| 25 | return self.address_pool.gateway_ip |
| 26 | |
| 27 | @property |
| 28 | def gateway_mac(self): |
| 29 | if not self.address_pool: |
| 30 | return None |
| 31 | return self.address_pool.gateway_mac |
| 32 | |
| 33 | @property |
| 34 | def cidr(self): |
| 35 | if not self.address_pool: |
| 36 | return None |
| 37 | return self.address_pool.cidr |
| 38 | |
| 39 | @property |
| 40 | def netbits(self): |
| 41 | # return number of bits in the network portion of the cidr |
| 42 | if self.cidr: |
| 43 | parts = self.cidr.split("/") |
| 44 | if len(parts) == 2: |
| 45 | return int(parts[1].strip()) |
| 46 | return None |
| 47 | |
| 48 | # Use for tenant_for_instance_id |
| 49 | # TODO: These should be reimplemented using real database models |
| 50 | |
| 51 | def get_attribute(self, name, default=None): |
| 52 | if self.service_specific_attribute: |
| 53 | attributes = json.loads(self.service_specific_attribute) |
| 54 | else: |
| 55 | attributes = {} |
| 56 | return attributes.get(name, default) |
| 57 | |
| 58 | def set_attribute(self, name, value): |
| 59 | if self.service_specific_attribute: |
| 60 | attributes = json.loads(self.service_specific_attribute) |
| 61 | else: |
| 62 | attributes = {} |
| 63 | attributes[name] = value |
| 64 | self.service_specific_attribute = json.dumps(attributes) |
| 65 | |
| 66 | |
| 67 | register_convenience_wrapper("VRouterTenant", ORMWrapperVRouterTenant) |