Scott Baker | 3be4e74 | 2016-04-12 16:28:28 -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 | VROUTER_KIND = "vROUTER" |
| 19 | |
| 20 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 21 | |
| 22 | class VRouterService(Service): |
| 23 | KIND = VROUTER_KIND |
| 24 | |
| 25 | class Meta: |
| 26 | app_label = "cord" |
| 27 | verbose_name = "vRouter Service" |
| 28 | proxy = True |
| 29 | |
| 30 | def ip_to_mac(self, ip): |
| 31 | (a, b, c, d) = ip.split('.') |
| 32 | return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d)) |
| 33 | |
| 34 | def get_gateways(self): |
| 35 | gateways=[] |
| 36 | |
| 37 | aps = self.addresspools.all() |
| 38 | for ap in aps: |
| 39 | gateways.append( {"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac} ) |
| 40 | |
| 41 | return gateways |
| 42 | |
| 43 | def get_address_pool(self, name): |
| 44 | ap = AddressPool.objects.filter(name=name, service=self) |
| 45 | if not ap: |
| 46 | raise Exception("vRouter unable to find addresspool %s" % name) |
| 47 | return ap[0] |
| 48 | |
| 49 | def get_tenant(self, **kwargs): |
| 50 | address_pool_name = kwargs.pop("address_pool_name") |
| 51 | |
| 52 | ap = self.get_address_pool(address_pool_name) |
| 53 | |
| 54 | ip = ap.get_address() |
| 55 | if not ip: |
| 56 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
| 57 | |
| 58 | t = VRouterTenant(**kwargs) |
| 59 | t.public_ip = ip |
| 60 | t.public_mac = self.ip_to_mac(ip) |
| 61 | t.address_pool_name = ap.name |
| 62 | t.save() |
| 63 | |
| 64 | return t |
| 65 | |
| 66 | VRouterService.setup_simple_attributes() |
| 67 | |
| 68 | class VRouterTenant(Tenant): |
| 69 | class Meta: |
| 70 | proxy = True |
| 71 | |
| 72 | KIND = VROUTER_KIND |
| 73 | |
| 74 | simple_attributes = ( ("public_ip", None), |
| 75 | ("public_mac", None), |
| 76 | ("address_pool_name", None), |
| 77 | ) |
| 78 | |
| 79 | @property |
| 80 | def gateway_ip(self): |
| 81 | return self.address_pool.gateway_ip |
| 82 | |
| 83 | @property |
| 84 | def gateway_mac(self): |
| 85 | return self.address_pool.gateway_mac |
| 86 | |