blob: 2e85e90bb4aa4c4c0e3613310d9b677267c887b4 [file] [log] [blame]
Scott Bakerbeb128a2017-08-21 09:20:04 -07001
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
16from core.models import AddressPool
17import os
18from django.db.models import *
19from xos.exceptions import *
20from models_decl import *
21
Matteo Scandolo249076b2017-12-18 13:41:41 -080022def 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 Bakerbeb128a2017-08-21 09:20:04 -070026class AddressManagerService (AddressManagerService_decl):
27 class Meta:
28 proxy = True
29
Scott Bakerbeb128a2017-08-21 09:20:04 -070030
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 Scandolo249076b2017-12-18 13:41:41 -080045 # TODO remove me once the old TOSCA engine is gone
Scott Bakerbeb128a2017-08-21 09:20:04 -070046 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 Scandolo249076b2017-12-18 13:41:41 -080051 # ip = ap.get_address()
52 # if not ip:
53 # raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
Scott Bakerbeb128a2017-08-21 09:20:04 -070054
55 t = AddressManagerServiceInstance(owner=self, **kwargs)
Matteo Scandolo249076b2017-12-18 13:41:41 -080056 # NOTE this will be added updated on save
57 # t.public_ip = ip
58 # t.public_mac = ip_to_mac(ip)
Scott Bakerbeb128a2017-08-21 09:20:04 -070059 t.address_pool_id = ap.id
60 t.save()
61
62 return t
63
64class 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 Scandolo249076b2017-12-18 13:41:41 -0800107 def save(self, *args, **kwds):
108 """
109 We need to get an ip from addresspool when we create this model
110 """
Matteo Scandolo249076b2017-12-18 13:41:41 -0800111 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 Scandolo249076b2017-12-18 13:41:41 -0800114 super(AddressManagerServiceInstance, self).save(*args, **kwds)