Scott Baker | beb128a | 2017-08-21 09:20:04 -0700 | [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 | from core.models import AddressPool |
| 17 | import os |
| 18 | from django.db.models import * |
| 19 | from xos.exceptions import * |
| 20 | from models_decl import * |
| 21 | |
| 22 | class AddressManagerService (AddressManagerService_decl): |
| 23 | class Meta: |
| 24 | proxy = True |
| 25 | |
| 26 | def ip_to_mac(self, ip): |
| 27 | (a, b, c, d) = ip.split('.') |
| 28 | return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d)) |
| 29 | |
| 30 | def get_gateways(self): |
| 31 | gateways = [] |
| 32 | aps = self.addresspools.all() |
| 33 | for ap in aps: |
| 34 | gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac}) |
| 35 | |
| 36 | return gateways |
| 37 | |
| 38 | def get_address_pool(self, name): |
| 39 | ap = AddressPool.objects.filter(name=name, service=self) |
| 40 | if not ap: |
| 41 | raise Exception("Address Manager unable to find addresspool %s" % name) |
| 42 | return ap[0] |
| 43 | |
| 44 | def get_service_instance(self, **kwargs): |
| 45 | address_pool_name = kwargs.pop("address_pool_name") |
| 46 | |
| 47 | ap = self.get_address_pool(address_pool_name) |
| 48 | |
| 49 | ip = ap.get_address() |
| 50 | if not ip: |
| 51 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
| 52 | |
| 53 | t = AddressManagerServiceInstance(owner=self, **kwargs) |
| 54 | t.public_ip = ip |
| 55 | t.public_mac = self.ip_to_mac(ip) |
| 56 | t.address_pool_id = ap.id |
| 57 | t.save() |
| 58 | |
| 59 | return t |
| 60 | |
| 61 | class AddressManagerServiceInstance (AddressManagerServiceInstance_decl): |
| 62 | |
| 63 | class Meta: |
| 64 | proxy = True |
| 65 | |
| 66 | @property |
| 67 | def gateway_ip(self): |
| 68 | if not self.address_pool: |
| 69 | return None |
| 70 | return self.address_pool.gateway_ip |
| 71 | |
| 72 | @property |
| 73 | def gateway_mac(self): |
| 74 | if not self.address_pool: |
| 75 | return None |
| 76 | return self.address_pool.gateway_mac |
| 77 | |
| 78 | @property |
| 79 | def cidr(self): |
| 80 | if not self.address_pool: |
| 81 | return None |
| 82 | return self.address_pool.cidr |
| 83 | |
| 84 | @property |
| 85 | def netbits(self): |
| 86 | # return number of bits in the network portion of the cidr |
| 87 | if self.cidr: |
| 88 | parts = self.cidr.split("/") |
| 89 | if len(parts) == 2: |
| 90 | return int(parts[1].strip()) |
| 91 | return None |
| 92 | |
| 93 | def cleanup_addresspool(self): |
| 94 | if self.address_pool: |
| 95 | ap = self.address_pool |
| 96 | if ap: |
| 97 | ap.put_address(self.public_ip) |
| 98 | self.public_ip = None |
| 99 | |
| 100 | def delete(self, *args, **kwargs): |
| 101 | self.cleanup_addresspool() |
| 102 | super(AddressManagerServiceInstance, self).delete(*args, **kwargs) |
| 103 | |