blob: d7d7387adb215f0be1a48a73f6db835d3b9bdcd5 [file] [log] [blame]
Scott Baker3be4e742016-04-12 16:28:28 -07001from django.db import models
2from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber, NetworkParameter, NetworkParameterType, Port, AddressPool
3from core.models.plcorebase import StrippedCharField
4import os
5from django.db import models, transaction
6from django.forms.models import model_to_dict
7from django.db.models import Q
8from operator import itemgetter, attrgetter, methodcaller
9from core.models import Tag
10from core.models.service import LeastLoadedNodeScheduler
11import traceback
12from xos.exceptions import *
13from xos.config import Config
14
15class ConfigurationError(Exception):
16 pass
17
18VROUTER_KIND = "vROUTER"
19
20CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
21
22class 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
66VRouterService.setup_simple_attributes()
67
68class 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