blob: 32dd4f2456fccf5e3ad5a50b3ec523d48ddc2b64 [file] [log] [blame]
Sapan Bhatia5d255542017-06-16 17:49:56 -07001from django.db import models
Scott Baker7c241cd2017-07-18 10:54:14 -07002from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
Sapan Bhatia5d255542017-06-16 17:49:56 -07003from core.models.xosbase import StrippedCharField
4import os
5from django.db import models, transaction
6from django.forms.models import model_to_dict
7from django.db.models import *
8from operator import itemgetter, attrgetter, methodcaller
9from core.models import Tag
10from core.models.service import LeastLoadedNodeScheduler
11import traceback
12from xos.exceptions import *
13from models_decl import *
Scott Baker87eb7402016-06-20 17:21:50 -070014
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070015
Sapan Bhatia5d255542017-06-16 17:49:56 -070016class ConfigurationError(Exception):
17 pass
Scott Baker87eb7402016-06-20 17:21:50 -070018
19
Sapan Bhatia5d255542017-06-16 17:49:56 -070020class VRouterService (VRouterService_decl):
21 class Meta:
22 proxy = True
Scott Baker87eb7402016-06-20 17:21:50 -070023
Sapan Bhatia5d255542017-06-16 17:49:56 -070024 def ip_to_mac(self, ip):
Scott Baker7c241cd2017-07-18 10:54:14 -070025 (a, b, c, d) = ip.split('.')
26 return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070027
Sapan Bhatia5d255542017-06-16 17:49:56 -070028 def get_gateways(self):
Scott Baker7c241cd2017-07-18 10:54:14 -070029 gateways = []
30 aps = self.addresspools.all()
31 for ap in aps:
32 gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
Scott Baker87eb7402016-06-20 17:21:50 -070033
Scott Baker7c241cd2017-07-18 10:54:14 -070034 return gateways
Scott Baker87eb7402016-06-20 17:21:50 -070035
Sapan Bhatia5d255542017-06-16 17:49:56 -070036 def get_address_pool(self, name):
Scott Baker7c241cd2017-07-18 10:54:14 -070037 ap = AddressPool.objects.filter(name=name, service=self)
38 if not ap:
39 raise Exception("vRouter unable to find addresspool %s" % name)
40 return ap[0]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070041
Sapan Bhatia5d255542017-06-16 17:49:56 -070042 def get_tenant(self, **kwargs):
Scott Baker7c241cd2017-07-18 10:54:14 -070043 address_pool_name = kwargs.pop("address_pool_name")
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020044
Scott Baker7c241cd2017-07-18 10:54:14 -070045 ap = self.get_address_pool(address_pool_name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020046
Scott Baker7c241cd2017-07-18 10:54:14 -070047 ip = ap.get_address()
48 if not ip:
49 raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020050
Scott Baker7c241cd2017-07-18 10:54:14 -070051 t = VRouterTenant(owner=self, **kwargs)
52 t.public_ip = ip
53 t.public_mac = self.ip_to_mac(ip)
54 t.address_pool_id = ap.id
55 t.save()
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020056
Scott Baker7c241cd2017-07-18 10:54:14 -070057 return t
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020058
59
Sapan Bhatia5d255542017-06-16 17:49:56 -070060class VRouterDevice (VRouterDevice_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020061
Sapan Bhatia5d255542017-06-16 17:49:56 -070062 class Meta:
63 proxy = True
64 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020065
Sapan Bhatia5d255542017-06-16 17:49:56 -070066class VRouterPort (VRouterPort_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020067
Sapan Bhatia5d255542017-06-16 17:49:56 -070068 class Meta:
69 proxy = True
70 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020071
Sapan Bhatia5d255542017-06-16 17:49:56 -070072class VRouterApp (VRouterApp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020073
Sapan Bhatia5d255542017-06-16 17:49:56 -070074 class Meta:
75 proxy = True
76 def _get_interfaces(self):
77 app_interfaces = []
78 devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service)
79 for device in devices:
80 ports = VRouterPort.objects.filter(vrouter_device=device.id)
81 for port in ports:
82 interfaces = VRouterInterface.objects.filter(vrouter_port=port.id)
83 for iface in interfaces:
84 app_interfaces.append(iface.name)
85 return app_interfaces
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020086
Sapan Bhatia5d255542017-06-16 17:49:56 -070087class VRouterInterface (VRouterInterface_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020088
Sapan Bhatia5d255542017-06-16 17:49:56 -070089 class Meta:
90 proxy = True
91 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020092
Sapan Bhatia5d255542017-06-16 17:49:56 -070093class VRouterIp (VRouterIp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020094
Sapan Bhatia5d255542017-06-16 17:49:56 -070095 class Meta:
96 proxy = True
97 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020098
Sapan Bhatia5d255542017-06-16 17:49:56 -070099class VRouterTenant (VRouterTenant_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200100
Sapan Bhatia5d255542017-06-16 17:49:56 -0700101 class Meta:
102 proxy = True
103 @property
104 def gateway_ip(self):
105 if not self.address_pool:
106 return None
107 return self.address_pool.gateway_ip
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200108
Sapan Bhatia5d255542017-06-16 17:49:56 -0700109 @property
110 def gateway_mac(self):
111 if not self.address_pool:
112 return None
113 return self.address_pool.gateway_mac
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200114
Sapan Bhatia5d255542017-06-16 17:49:56 -0700115 @property
116 def cidr(self):
117 if not self.address_pool:
118 return None
119 return self.address_pool.cidr
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200120
Sapan Bhatia5d255542017-06-16 17:49:56 -0700121 @property
122 def netbits(self):
123 # return number of bits in the network portion of the cidr
124 if self.cidr:
125 parts = self.cidr.split("/")
126 if len(parts) == 2:
127 return int(parts[1].strip())
128 return None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200129
Sapan Bhatia5d255542017-06-16 17:49:56 -0700130 def cleanup_addresspool(self):
131 if self.address_pool:
132 ap = self.address_pool
133 if ap:
134 ap.put_address(self.public_ip)
135 self.public_ip = None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200136
Sapan Bhatia5d255542017-06-16 17:49:56 -0700137 def delete(self, *args, **kwargs):
138 self.cleanup_addresspool()
139 super(VRouterTenant, self).delete(*args, **kwargs)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200140