blob: 3b1035b4632960b9bf283ab5e52a44c03d5cc919 [file] [log] [blame]
Matteo Scandolod02b73b2017-08-08 13:05:26 -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
16
Sapan Bhatia5d255542017-06-16 17:49:56 -070017from django.db import models
Scott Baker7c241cd2017-07-18 10:54:14 -070018from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
Sapan Bhatia5d255542017-06-16 17:49:56 -070019from core.models.xosbase import StrippedCharField
20import os
21from django.db import models, transaction
22from django.forms.models import model_to_dict
23from django.db.models import *
24from operator import itemgetter, attrgetter, methodcaller
25from core.models import Tag
26from core.models.service import LeastLoadedNodeScheduler
27import traceback
28from xos.exceptions import *
29from models_decl import *
Scott Baker87eb7402016-06-20 17:21:50 -070030
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070031
Sapan Bhatia5d255542017-06-16 17:49:56 -070032class ConfigurationError(Exception):
33 pass
Scott Baker87eb7402016-06-20 17:21:50 -070034
35
Sapan Bhatia5d255542017-06-16 17:49:56 -070036class VRouterService (VRouterService_decl):
37 class Meta:
38 proxy = True
Scott Baker87eb7402016-06-20 17:21:50 -070039
Sapan Bhatia5d255542017-06-16 17:49:56 -070040 def ip_to_mac(self, ip):
Scott Baker7c241cd2017-07-18 10:54:14 -070041 (a, b, c, d) = ip.split('.')
42 return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070043
Sapan Bhatia5d255542017-06-16 17:49:56 -070044 def get_gateways(self):
Scott Baker7c241cd2017-07-18 10:54:14 -070045 gateways = []
46 aps = self.addresspools.all()
47 for ap in aps:
48 gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
Scott Baker87eb7402016-06-20 17:21:50 -070049
Scott Baker7c241cd2017-07-18 10:54:14 -070050 return gateways
Scott Baker87eb7402016-06-20 17:21:50 -070051
Sapan Bhatia5d255542017-06-16 17:49:56 -070052 def get_address_pool(self, name):
Scott Baker7c241cd2017-07-18 10:54:14 -070053 ap = AddressPool.objects.filter(name=name, service=self)
54 if not ap:
55 raise Exception("vRouter unable to find addresspool %s" % name)
56 return ap[0]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070057
Sapan Bhatia5d255542017-06-16 17:49:56 -070058 def get_tenant(self, **kwargs):
Scott Baker7c241cd2017-07-18 10:54:14 -070059 address_pool_name = kwargs.pop("address_pool_name")
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020060
Scott Baker7c241cd2017-07-18 10:54:14 -070061 ap = self.get_address_pool(address_pool_name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020062
Scott Baker7c241cd2017-07-18 10:54:14 -070063 ip = ap.get_address()
64 if not ip:
65 raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020066
Scott Baker7c241cd2017-07-18 10:54:14 -070067 t = VRouterTenant(owner=self, **kwargs)
68 t.public_ip = ip
69 t.public_mac = self.ip_to_mac(ip)
70 t.address_pool_id = ap.id
71 t.save()
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020072
Scott Baker7c241cd2017-07-18 10:54:14 -070073 return t
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020074
75
Sapan Bhatia5d255542017-06-16 17:49:56 -070076class VRouterDevice (VRouterDevice_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020077
Sapan Bhatia5d255542017-06-16 17:49:56 -070078 class Meta:
79 proxy = True
80 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020081
Sapan Bhatia5d255542017-06-16 17:49:56 -070082class VRouterPort (VRouterPort_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020083
Sapan Bhatia5d255542017-06-16 17:49:56 -070084 class Meta:
85 proxy = True
86 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020087
Sapan Bhatia5d255542017-06-16 17:49:56 -070088class VRouterApp (VRouterApp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020089
Sapan Bhatia5d255542017-06-16 17:49:56 -070090 class Meta:
91 proxy = True
92 def _get_interfaces(self):
93 app_interfaces = []
94 devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service)
95 for device in devices:
96 ports = VRouterPort.objects.filter(vrouter_device=device.id)
97 for port in ports:
98 interfaces = VRouterInterface.objects.filter(vrouter_port=port.id)
99 for iface in interfaces:
100 app_interfaces.append(iface.name)
101 return app_interfaces
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200102
Sapan Bhatia5d255542017-06-16 17:49:56 -0700103class VRouterInterface (VRouterInterface_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200104
Sapan Bhatia5d255542017-06-16 17:49:56 -0700105 class Meta:
106 proxy = True
107 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200108
Sapan Bhatia5d255542017-06-16 17:49:56 -0700109class VRouterIp (VRouterIp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200110
Sapan Bhatia5d255542017-06-16 17:49:56 -0700111 class Meta:
112 proxy = True
113 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200114
Sapan Bhatia5d255542017-06-16 17:49:56 -0700115class VRouterTenant (VRouterTenant_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200116
Sapan Bhatia5d255542017-06-16 17:49:56 -0700117 class Meta:
118 proxy = True
119 @property
120 def gateway_ip(self):
121 if not self.address_pool:
122 return None
123 return self.address_pool.gateway_ip
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200124
Sapan Bhatia5d255542017-06-16 17:49:56 -0700125 @property
126 def gateway_mac(self):
127 if not self.address_pool:
128 return None
129 return self.address_pool.gateway_mac
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200130
Sapan Bhatia5d255542017-06-16 17:49:56 -0700131 @property
132 def cidr(self):
133 if not self.address_pool:
134 return None
135 return self.address_pool.cidr
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200136
Sapan Bhatia5d255542017-06-16 17:49:56 -0700137 @property
138 def netbits(self):
139 # return number of bits in the network portion of the cidr
140 if self.cidr:
141 parts = self.cidr.split("/")
142 if len(parts) == 2:
143 return int(parts[1].strip())
144 return None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200145
Sapan Bhatia5d255542017-06-16 17:49:56 -0700146 def cleanup_addresspool(self):
147 if self.address_pool:
148 ap = self.address_pool
149 if ap:
150 ap.put_address(self.public_ip)
151 self.public_ip = None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200152
Sapan Bhatia5d255542017-06-16 17:49:56 -0700153 def delete(self, *args, **kwargs):
154 self.cleanup_addresspool()
155 super(VRouterTenant, self).delete(*args, **kwargs)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200156