blob: 3acc1984364224a831cd0fb1f886344f880671d3 [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
Sapan Bhatia5d255542017-06-16 17:49:56 -070026import traceback
27from xos.exceptions import *
28from models_decl import *
Scott Baker87eb7402016-06-20 17:21:50 -070029
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070030
Sapan Bhatia5d255542017-06-16 17:49:56 -070031class ConfigurationError(Exception):
32 pass
Scott Baker87eb7402016-06-20 17:21:50 -070033
34
Sapan Bhatia5d255542017-06-16 17:49:56 -070035class VRouterService (VRouterService_decl):
36 class Meta:
37 proxy = True
Scott Baker87eb7402016-06-20 17:21:50 -070038
Sapan Bhatia5d255542017-06-16 17:49:56 -070039 def ip_to_mac(self, ip):
Scott Baker7c241cd2017-07-18 10:54:14 -070040 (a, b, c, d) = ip.split('.')
41 return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070042
Sapan Bhatia5d255542017-06-16 17:49:56 -070043 def get_gateways(self):
Scott Baker7c241cd2017-07-18 10:54:14 -070044 gateways = []
45 aps = self.addresspools.all()
46 for ap in aps:
47 gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
Scott Baker87eb7402016-06-20 17:21:50 -070048
Scott Baker7c241cd2017-07-18 10:54:14 -070049 return gateways
Scott Baker87eb7402016-06-20 17:21:50 -070050
Sapan Bhatia5d255542017-06-16 17:49:56 -070051 def get_address_pool(self, name):
Scott Baker7c241cd2017-07-18 10:54:14 -070052 ap = AddressPool.objects.filter(name=name, service=self)
53 if not ap:
54 raise Exception("vRouter unable to find addresspool %s" % name)
55 return ap[0]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070056
Sapan Bhatia5d255542017-06-16 17:49:56 -070057 def get_tenant(self, **kwargs):
Scott Baker7c241cd2017-07-18 10:54:14 -070058 address_pool_name = kwargs.pop("address_pool_name")
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020059
Scott Baker7c241cd2017-07-18 10:54:14 -070060 ap = self.get_address_pool(address_pool_name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020061
Scott Baker7c241cd2017-07-18 10:54:14 -070062 ip = ap.get_address()
63 if not ip:
64 raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020065
Scott Baker7c241cd2017-07-18 10:54:14 -070066 t = VRouterTenant(owner=self, **kwargs)
67 t.public_ip = ip
68 t.public_mac = self.ip_to_mac(ip)
69 t.address_pool_id = ap.id
70 t.save()
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020071
Scott Baker7c241cd2017-07-18 10:54:14 -070072 return t
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020073
74
Sapan Bhatia5d255542017-06-16 17:49:56 -070075class VRouterDevice (VRouterDevice_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020076
Sapan Bhatia5d255542017-06-16 17:49:56 -070077 class Meta:
78 proxy = True
79 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020080
Sapan Bhatia5d255542017-06-16 17:49:56 -070081class VRouterPort (VRouterPort_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020082
Sapan Bhatia5d255542017-06-16 17:49:56 -070083 class Meta:
84 proxy = True
85 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020086
Sapan Bhatia5d255542017-06-16 17:49:56 -070087class VRouterApp (VRouterApp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020088
Sapan Bhatia5d255542017-06-16 17:49:56 -070089 class Meta:
90 proxy = True
91 def _get_interfaces(self):
92 app_interfaces = []
93 devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service)
94 for device in devices:
95 ports = VRouterPort.objects.filter(vrouter_device=device.id)
96 for port in ports:
97 interfaces = VRouterInterface.objects.filter(vrouter_port=port.id)
98 for iface in interfaces:
99 app_interfaces.append(iface.name)
100 return app_interfaces
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200101
Sapan Bhatia5d255542017-06-16 17:49:56 -0700102class VRouterInterface (VRouterInterface_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200103
Sapan Bhatia5d255542017-06-16 17:49:56 -0700104 class Meta:
105 proxy = True
106 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200107
Sapan Bhatia5d255542017-06-16 17:49:56 -0700108class VRouterIp (VRouterIp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200109
Sapan Bhatia5d255542017-06-16 17:49:56 -0700110 class Meta:
111 proxy = True
112 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200113
Sapan Bhatia5d255542017-06-16 17:49:56 -0700114class VRouterTenant (VRouterTenant_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200115
Sapan Bhatia5d255542017-06-16 17:49:56 -0700116 class Meta:
117 proxy = True
118 @property
119 def gateway_ip(self):
120 if not self.address_pool:
121 return None
122 return self.address_pool.gateway_ip
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200123
Sapan Bhatia5d255542017-06-16 17:49:56 -0700124 @property
125 def gateway_mac(self):
126 if not self.address_pool:
127 return None
128 return self.address_pool.gateway_mac
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200129
Sapan Bhatia5d255542017-06-16 17:49:56 -0700130 @property
131 def cidr(self):
132 if not self.address_pool:
133 return None
134 return self.address_pool.cidr
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200135
Sapan Bhatia5d255542017-06-16 17:49:56 -0700136 @property
137 def netbits(self):
138 # return number of bits in the network portion of the cidr
139 if self.cidr:
140 parts = self.cidr.split("/")
141 if len(parts) == 2:
142 return int(parts[1].strip())
143 return None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200144
Sapan Bhatia5d255542017-06-16 17:49:56 -0700145 def cleanup_addresspool(self):
146 if self.address_pool:
147 ap = self.address_pool
148 if ap:
149 ap.put_address(self.public_ip)
150 self.public_ip = None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200151
Sapan Bhatia5d255542017-06-16 17:49:56 -0700152 def delete(self, *args, **kwargs):
153 self.cleanup_addresspool()
154 super(VRouterTenant, self).delete(*args, **kwargs)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200155