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 | |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 22 | def ip_to_mac(ip): |
| 23 | (a, b, c, d) = ip.split('.') |
| 24 | return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d)) |
| 25 | |
Scott Baker | beb128a | 2017-08-21 09:20:04 -0700 | [diff] [blame] | 26 | class AddressManagerService (AddressManagerService_decl): |
| 27 | class Meta: |
| 28 | proxy = True |
| 29 | |
Scott Baker | beb128a | 2017-08-21 09:20:04 -0700 | [diff] [blame] | 30 | |
| 31 | def get_gateways(self): |
| 32 | gateways = [] |
| 33 | aps = self.addresspools.all() |
| 34 | for ap in aps: |
| 35 | gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac}) |
| 36 | |
| 37 | return gateways |
| 38 | |
| 39 | def get_address_pool(self, name): |
| 40 | ap = AddressPool.objects.filter(name=name, service=self) |
| 41 | if not ap: |
| 42 | raise Exception("Address Manager unable to find addresspool %s" % name) |
| 43 | return ap[0] |
| 44 | |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 45 | # TODO remove me once the old TOSCA engine is gone |
Scott Baker | beb128a | 2017-08-21 09:20:04 -0700 | [diff] [blame] | 46 | def get_service_instance(self, **kwargs): |
| 47 | address_pool_name = kwargs.pop("address_pool_name") |
| 48 | |
| 49 | ap = self.get_address_pool(address_pool_name) |
| 50 | |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 51 | # ip = ap.get_address() |
| 52 | # if not ip: |
| 53 | # raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
Scott Baker | beb128a | 2017-08-21 09:20:04 -0700 | [diff] [blame] | 54 | |
| 55 | t = AddressManagerServiceInstance(owner=self, **kwargs) |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 56 | # NOTE this will be added updated on save |
| 57 | # t.public_ip = ip |
| 58 | # t.public_mac = ip_to_mac(ip) |
Scott Baker | beb128a | 2017-08-21 09:20:04 -0700 | [diff] [blame] | 59 | t.address_pool_id = ap.id |
| 60 | t.save() |
| 61 | |
| 62 | return t |
| 63 | |
| 64 | class AddressManagerServiceInstance (AddressManagerServiceInstance_decl): |
| 65 | |
| 66 | class Meta: |
| 67 | proxy = True |
| 68 | |
| 69 | @property |
| 70 | def gateway_ip(self): |
| 71 | if not self.address_pool: |
| 72 | return None |
| 73 | return self.address_pool.gateway_ip |
| 74 | |
| 75 | @property |
| 76 | def gateway_mac(self): |
| 77 | if not self.address_pool: |
| 78 | return None |
| 79 | return self.address_pool.gateway_mac |
| 80 | |
| 81 | @property |
| 82 | def cidr(self): |
| 83 | if not self.address_pool: |
| 84 | return None |
| 85 | return self.address_pool.cidr |
| 86 | |
| 87 | @property |
| 88 | def netbits(self): |
| 89 | # return number of bits in the network portion of the cidr |
| 90 | if self.cidr: |
| 91 | parts = self.cidr.split("/") |
| 92 | if len(parts) == 2: |
| 93 | return int(parts[1].strip()) |
| 94 | return None |
| 95 | |
| 96 | def cleanup_addresspool(self): |
| 97 | if self.address_pool: |
| 98 | ap = self.address_pool |
| 99 | if ap: |
| 100 | ap.put_address(self.public_ip) |
| 101 | self.public_ip = None |
| 102 | |
| 103 | def delete(self, *args, **kwargs): |
| 104 | self.cleanup_addresspool() |
| 105 | super(AddressManagerServiceInstance, self).delete(*args, **kwargs) |
| 106 | |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 107 | def save(self, *args, **kwds): |
| 108 | """ |
| 109 | We need to get an ip from addresspool when we create this model |
| 110 | """ |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 111 | if not self.id and not self.public_ip: |
| 112 | self.public_ip = self.address_pool.get_address() |
| 113 | self.public_mac = ip_to_mac(self.public_ip) |
Matteo Scandolo | 249076b | 2017-12-18 13:41:41 -0800 | [diff] [blame] | 114 | super(AddressManagerServiceInstance, self).save(*args, **kwds) |