blob: 906d125443ed2b5eb5f736e27f33621434ed8db6 [file] [log] [blame]
Sapan Bhatia5d255542017-06-16 17:49:56 -07001from django.db import models
2from core.models import Service, XOSBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
3from 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):
25 (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):
29 gateways = []
Scott Baker87eb7402016-06-20 17:21:50 -070030
Sapan Bhatia5d255542017-06-16 17:49:56 -070031 aps = self.addresspools.all()
32 for ap in aps:
33 gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
Scott Baker87eb7402016-06-20 17:21:50 -070034
Sapan Bhatia5d255542017-06-16 17:49:56 -070035 return gateways
Scott Baker87eb7402016-06-20 17:21:50 -070036
Sapan Bhatia5d255542017-06-16 17:49:56 -070037 def get_address_pool(self, name):
38 ap = AddressPool.objects.filter(name=name, service=self)
39 if not ap:
40 raise Exception("vRouter unable to find addresspool %s" % name)
41 return ap[0]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070042
Sapan Bhatia5d255542017-06-16 17:49:56 -070043 def get_tenant(self, **kwargs):
44 address_pool_name = kwargs.pop("address_pool_name")
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020045
Sapan Bhatia5d255542017-06-16 17:49:56 -070046 ap = self.get_address_pool(address_pool_name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020047
Sapan Bhatia5d255542017-06-16 17:49:56 -070048 ip = ap.get_address()
49 if not ip:
50 raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020051
Sapan Bhatia5d255542017-06-16 17:49:56 -070052 t = VRouterTenant(provider_service=self, **kwargs)
53 t.public_ip = ip
54 t.public_mac = self.ip_to_mac(ip)
55 t.address_pool_id = ap.id
56 t.save()
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020057
Sapan Bhatia5d255542017-06-16 17:49:56 -070058 return t
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020059
60
Sapan Bhatia5d255542017-06-16 17:49:56 -070061class VRouterDevice (VRouterDevice_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020062
Sapan Bhatia5d255542017-06-16 17:49:56 -070063 class Meta:
64 proxy = True
65 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020066
Sapan Bhatia5d255542017-06-16 17:49:56 -070067class VRouterPort (VRouterPort_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020068
Sapan Bhatia5d255542017-06-16 17:49:56 -070069 class Meta:
70 proxy = True
71 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020072
Sapan Bhatia5d255542017-06-16 17:49:56 -070073class VRouterApp (VRouterApp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020074
Sapan Bhatia5d255542017-06-16 17:49:56 -070075 class Meta:
76 proxy = True
77 def _get_interfaces(self):
78 app_interfaces = []
79 devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service)
80 for device in devices:
81 ports = VRouterPort.objects.filter(vrouter_device=device.id)
82 for port in ports:
83 interfaces = VRouterInterface.objects.filter(vrouter_port=port.id)
84 for iface in interfaces:
85 app_interfaces.append(iface.name)
86 return app_interfaces
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020087
Sapan Bhatia5d255542017-06-16 17:49:56 -070088class VRouterInterface (VRouterInterface_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020089
Sapan Bhatia5d255542017-06-16 17:49:56 -070090 class Meta:
91 proxy = True
92 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020093
Sapan Bhatia5d255542017-06-16 17:49:56 -070094class VRouterIp (VRouterIp_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020095
Sapan Bhatia5d255542017-06-16 17:49:56 -070096 class Meta:
97 proxy = True
98 pass
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +020099
Sapan Bhatia5d255542017-06-16 17:49:56 -0700100class VRouterTenant (VRouterTenant_decl):
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200101
Sapan Bhatia5d255542017-06-16 17:49:56 -0700102 class Meta:
103 proxy = True
104 @property
105 def gateway_ip(self):
106 if not self.address_pool:
107 return None
108 return self.address_pool.gateway_ip
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200109
Sapan Bhatia5d255542017-06-16 17:49:56 -0700110 @property
111 def gateway_mac(self):
112 if not self.address_pool:
113 return None
114 return self.address_pool.gateway_mac
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200115
Sapan Bhatia5d255542017-06-16 17:49:56 -0700116 @property
117 def cidr(self):
118 if not self.address_pool:
119 return None
120 return self.address_pool.cidr
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200121
Sapan Bhatia5d255542017-06-16 17:49:56 -0700122 @property
123 def netbits(self):
124 # return number of bits in the network portion of the cidr
125 if self.cidr:
126 parts = self.cidr.split("/")
127 if len(parts) == 2:
128 return int(parts[1].strip())
129 return None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200130
Sapan Bhatia5d255542017-06-16 17:49:56 -0700131 def cleanup_addresspool(self):
132 if self.address_pool:
133 ap = self.address_pool
134 if ap:
135 ap.put_address(self.public_ip)
136 self.public_ip = None
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200137
Sapan Bhatia5d255542017-06-16 17:49:56 -0700138 def delete(self, *args, **kwargs):
139 self.cleanup_addresspool()
140 super(VRouterTenant, self).delete(*args, **kwargs)
Sapan Bhatia0a56fdc2017-04-21 15:12:57 +0200141