Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 1 | from django.db import models |
| 2 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 3 | from core.models.plcorebase 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 Q |
| 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 xos.config import Config |
| 14 | |
| 15 | class ConfigurationError(Exception): |
| 16 | pass |
| 17 | |
| 18 | |
| 19 | VROUTER_KIND = "vROUTER" |
| 20 | |
| 21 | # NOTE: don't change VROUTER_KIND unless you also change the reference to it |
| 22 | # in tosca/resources/network.py |
| 23 | |
| 24 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 25 | |
| 26 | class VRouterService(Service): |
| 27 | KIND = VROUTER_KIND |
| 28 | |
| 29 | class Meta: |
| 30 | app_label = "vrouter" |
| 31 | verbose_name = "vRouter Service" |
| 32 | proxy = True |
| 33 | |
| 34 | def ip_to_mac(self, ip): |
| 35 | (a, b, c, d) = ip.split('.') |
| 36 | return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d)) |
| 37 | |
| 38 | def get_gateways(self): |
| 39 | gateways=[] |
| 40 | |
| 41 | aps = self.addresspools.all() |
| 42 | for ap in aps: |
| 43 | gateways.append( {"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac} ) |
| 44 | |
| 45 | return gateways |
| 46 | |
| 47 | def get_address_pool(self, name): |
| 48 | ap = AddressPool.objects.filter(name=name, service=self) |
| 49 | if not ap: |
| 50 | raise Exception("vRouter unable to find addresspool %s" % name) |
| 51 | return ap[0] |
| 52 | |
| 53 | def get_tenant(self, **kwargs): |
| 54 | address_pool_name = kwargs.pop("address_pool_name") |
| 55 | |
| 56 | ap = self.get_address_pool(address_pool_name) |
| 57 | |
| 58 | ip = ap.get_address() |
| 59 | if not ip: |
| 60 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
| 61 | |
| 62 | t = VRouterTenant(provider_service=self, **kwargs) |
| 63 | t.public_ip = ip |
| 64 | t.public_mac = self.ip_to_mac(ip) |
| 65 | t.address_pool_id = ap.id |
| 66 | t.save() |
| 67 | |
| 68 | return t |
| 69 | |
| 70 | #VRouterService.setup_simple_attributes() |
| 71 | |
| 72 | class VRouterTenant(Tenant): |
| 73 | class Meta: |
| 74 | proxy = True |
| 75 | |
| 76 | KIND = VROUTER_KIND |
| 77 | |
| 78 | simple_attributes = ( ("public_ip", None), |
| 79 | ("public_mac", None), |
| 80 | ("address_pool_id", None), |
| 81 | ) |
| 82 | |
| 83 | @property |
| 84 | def gateway_ip(self): |
| 85 | if not self.address_pool: |
| 86 | return None |
| 87 | return self.address_pool.gateway_ip |
| 88 | |
| 89 | @property |
| 90 | def gateway_mac(self): |
| 91 | if not self.address_pool: |
| 92 | return None |
| 93 | return self.address_pool.gateway_mac |
| 94 | |
| 95 | @property |
| 96 | def cidr(self): |
| 97 | if not self.address_pool: |
| 98 | return None |
| 99 | return self.address_pool.cidr |
| 100 | |
| 101 | @property |
| 102 | def netbits(self): |
| 103 | # return number of bits in the network portion of the cidr |
| 104 | if self.cidr: |
| 105 | parts = self.cidr.split("/") |
| 106 | if len(parts)==2: |
| 107 | return int(parts[1].strip()) |
| 108 | return None |
| 109 | |
| 110 | @property |
| 111 | def address_pool(self): |
| 112 | if getattr(self, "cached_address_pool", None): |
| 113 | return self.cached_address_pool |
| 114 | if not self.address_pool_id: |
| 115 | return None |
| 116 | aps=AddressPool.objects.filter(id=self.address_pool_id) |
| 117 | if not aps: |
| 118 | return None |
| 119 | ap=aps[0] |
| 120 | self.cached_address_pool = ap |
| 121 | return ap |
| 122 | |
| 123 | @address_pool.setter |
| 124 | def address_pool(self, value): |
| 125 | if value: |
| 126 | value = value.id |
| 127 | if (value != self.get_attribute("address_pool_id", None)): |
| 128 | self.cached_address_pool=None |
| 129 | self.set_attribute("address_pool_id", value) |
| 130 | |
| 131 | def cleanup_addresspool(self): |
| 132 | if self.address_pool_id: |
| 133 | ap = AddressPool.objects.filter(id=self.address_pool_id) |
| 134 | if ap: |
| 135 | ap[0].put_address(self.public_ip) |
| 136 | self.public_ip = None |
| 137 | |
| 138 | def delete(self, *args, **kwargs): |
| 139 | self.cleanup_addresspool() |
| 140 | super(VRouterTenant, self).delete(*args, **kwargs) |
| 141 | |
| 142 | VRouterTenant.setup_simple_attributes() |
| 143 | |