Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 1 | from django.db import models |
| 2 | from core.models import Service, XOSBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 3 | from core.models.xosbase import StrippedCharField |
| 4 | import os |
| 5 | from django.db import models, transaction |
| 6 | from django.forms.models import model_to_dict |
| 7 | from django.db.models import * |
| 8 | from operator import itemgetter, attrgetter, methodcaller |
| 9 | from core.models import Tag |
| 10 | from core.models.service import LeastLoadedNodeScheduler |
| 11 | import traceback |
| 12 | from xos.exceptions import * |
| 13 | from models_decl import * |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 14 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 15 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 16 | class ConfigurationError(Exception): |
| 17 | pass |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 18 | |
| 19 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 20 | class VRouterService (VRouterService_decl): |
| 21 | class Meta: |
| 22 | proxy = True |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 23 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 24 | 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 Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 27 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 28 | def get_gateways(self): |
| 29 | gateways = [] |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 30 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 31 | aps = self.addresspools.all() |
| 32 | for ap in aps: |
| 33 | gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac}) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 34 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 35 | return gateways |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 36 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 37 | 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 Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 42 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 43 | def get_tenant(self, **kwargs): |
| 44 | address_pool_name = kwargs.pop("address_pool_name") |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 45 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 46 | ap = self.get_address_pool(address_pool_name) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 47 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 48 | ip = ap.get_address() |
| 49 | if not ip: |
| 50 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 51 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 52 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 57 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 58 | return t |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 59 | |
| 60 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 61 | class VRouterDevice (VRouterDevice_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 62 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 63 | class Meta: |
| 64 | proxy = True |
| 65 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 66 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 67 | class VRouterPort (VRouterPort_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 68 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 69 | class Meta: |
| 70 | proxy = True |
| 71 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 72 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 73 | class VRouterApp (VRouterApp_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 74 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 75 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 87 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 88 | class VRouterInterface (VRouterInterface_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 89 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 90 | class Meta: |
| 91 | proxy = True |
| 92 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 93 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 94 | class VRouterIp (VRouterIp_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 95 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 96 | class Meta: |
| 97 | proxy = True |
| 98 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 99 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 100 | class VRouterTenant (VRouterTenant_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 101 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 102 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 109 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 110 | @property |
| 111 | def gateway_mac(self): |
| 112 | if not self.address_pool: |
| 113 | return None |
| 114 | return self.address_pool.gateway_mac |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 115 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 116 | @property |
| 117 | def cidr(self): |
| 118 | if not self.address_pool: |
| 119 | return None |
| 120 | return self.address_pool.cidr |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 121 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 122 | @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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 130 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 131 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 137 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame^] | 138 | def delete(self, *args, **kwargs): |
| 139 | self.cleanup_addresspool() |
| 140 | super(VRouterTenant, self).delete(*args, **kwargs) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 141 | |