Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 1 | from django.db import models |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 2 | from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 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): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 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): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 29 | 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 Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 33 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 34 | return gateways |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 35 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 36 | def get_address_pool(self, name): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 37 | 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 Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 41 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 42 | def get_tenant(self, **kwargs): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 43 | address_pool_name = kwargs.pop("address_pool_name") |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 44 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 45 | ap = self.get_address_pool(address_pool_name) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 46 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 47 | ip = ap.get_address() |
| 48 | if not ip: |
| 49 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 50 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 51 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 56 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame^] | 57 | return t |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 58 | |
| 59 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 60 | class VRouterDevice (VRouterDevice_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 61 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 62 | class Meta: |
| 63 | proxy = True |
| 64 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 65 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 66 | class VRouterPort (VRouterPort_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 67 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 68 | class Meta: |
| 69 | proxy = True |
| 70 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 71 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 72 | class VRouterApp (VRouterApp_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 73 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 74 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 86 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 87 | class VRouterInterface (VRouterInterface_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 88 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 89 | class Meta: |
| 90 | proxy = True |
| 91 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 92 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 93 | class VRouterIp (VRouterIp_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 94 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 95 | class Meta: |
| 96 | proxy = True |
| 97 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 98 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 99 | class VRouterTenant (VRouterTenant_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 100 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 101 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 108 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 109 | @property |
| 110 | def gateway_mac(self): |
| 111 | if not self.address_pool: |
| 112 | return None |
| 113 | return self.address_pool.gateway_mac |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 114 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 115 | @property |
| 116 | def cidr(self): |
| 117 | if not self.address_pool: |
| 118 | return None |
| 119 | return self.address_pool.cidr |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 120 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 121 | @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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 129 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 130 | 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 Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 136 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 137 | def delete(self, *args, **kwargs): |
| 138 | self.cleanup_addresspool() |
| 139 | super(VRouterTenant, self).delete(*args, **kwargs) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 140 | |