Matteo Scandolo | d02b73b | 2017-08-08 13:05:26 -0700 | [diff] [blame^] | 1 | |
| 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 Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 17 | from django.db import models |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 18 | 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] | 19 | from core.models.xosbase import StrippedCharField |
| 20 | import os |
| 21 | from django.db import models, transaction |
| 22 | from django.forms.models import model_to_dict |
| 23 | from django.db.models import * |
| 24 | from operator import itemgetter, attrgetter, methodcaller |
| 25 | from core.models import Tag |
| 26 | from core.models.service import LeastLoadedNodeScheduler |
| 27 | import traceback |
| 28 | from xos.exceptions import * |
| 29 | from models_decl import * |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 30 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 31 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 32 | class ConfigurationError(Exception): |
| 33 | pass |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 34 | |
| 35 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 36 | class VRouterService (VRouterService_decl): |
| 37 | class Meta: |
| 38 | proxy = True |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 39 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 40 | def ip_to_mac(self, ip): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 41 | (a, b, c, d) = ip.split('.') |
| 42 | 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] | 43 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 44 | def get_gateways(self): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 45 | gateways = [] |
| 46 | aps = self.addresspools.all() |
| 47 | for ap in aps: |
| 48 | gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac}) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 49 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 50 | return gateways |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 51 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 52 | def get_address_pool(self, name): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 53 | ap = AddressPool.objects.filter(name=name, service=self) |
| 54 | if not ap: |
| 55 | raise Exception("vRouter unable to find addresspool %s" % name) |
| 56 | return ap[0] |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 57 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 58 | def get_tenant(self, **kwargs): |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 59 | address_pool_name = kwargs.pop("address_pool_name") |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 60 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 61 | ap = self.get_address_pool(address_pool_name) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 62 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 63 | ip = ap.get_address() |
| 64 | if not ip: |
| 65 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 66 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 67 | t = VRouterTenant(owner=self, **kwargs) |
| 68 | t.public_ip = ip |
| 69 | t.public_mac = self.ip_to_mac(ip) |
| 70 | t.address_pool_id = ap.id |
| 71 | t.save() |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 72 | |
Scott Baker | 7c241cd | 2017-07-18 10:54:14 -0700 | [diff] [blame] | 73 | return t |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 74 | |
| 75 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 76 | class VRouterDevice (VRouterDevice_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 77 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 78 | class Meta: |
| 79 | proxy = True |
| 80 | pass |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 81 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 82 | class VRouterPort (VRouterPort_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 83 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 84 | class Meta: |
| 85 | proxy = True |
| 86 | pass |
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 VRouterApp (VRouterApp_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 | def _get_interfaces(self): |
| 93 | app_interfaces = [] |
| 94 | devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service) |
| 95 | for device in devices: |
| 96 | ports = VRouterPort.objects.filter(vrouter_device=device.id) |
| 97 | for port in ports: |
| 98 | interfaces = VRouterInterface.objects.filter(vrouter_port=port.id) |
| 99 | for iface in interfaces: |
| 100 | app_interfaces.append(iface.name) |
| 101 | return app_interfaces |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 102 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 103 | class VRouterInterface (VRouterInterface_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 104 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 105 | class Meta: |
| 106 | proxy = True |
| 107 | pass |
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 | class VRouterIp (VRouterIp_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 110 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 111 | class Meta: |
| 112 | proxy = True |
| 113 | pass |
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 | class VRouterTenant (VRouterTenant_decl): |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 116 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 117 | class Meta: |
| 118 | proxy = True |
| 119 | @property |
| 120 | def gateway_ip(self): |
| 121 | if not self.address_pool: |
| 122 | return None |
| 123 | return self.address_pool.gateway_ip |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 124 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 125 | @property |
| 126 | def gateway_mac(self): |
| 127 | if not self.address_pool: |
| 128 | return None |
| 129 | return self.address_pool.gateway_mac |
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 | @property |
| 132 | def cidr(self): |
| 133 | if not self.address_pool: |
| 134 | return None |
| 135 | return self.address_pool.cidr |
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 | @property |
| 138 | def netbits(self): |
| 139 | # return number of bits in the network portion of the cidr |
| 140 | if self.cidr: |
| 141 | parts = self.cidr.split("/") |
| 142 | if len(parts) == 2: |
| 143 | return int(parts[1].strip()) |
| 144 | return None |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 145 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 146 | def cleanup_addresspool(self): |
| 147 | if self.address_pool: |
| 148 | ap = self.address_pool |
| 149 | if ap: |
| 150 | ap.put_address(self.public_ip) |
| 151 | self.public_ip = None |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 152 | |
Sapan Bhatia | 5d25554 | 2017-06-16 17:49:56 -0700 | [diff] [blame] | 153 | def delete(self, *args, **kwargs): |
| 154 | self.cleanup_addresspool() |
| 155 | super(VRouterTenant, self).delete(*args, **kwargs) |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 156 | |