blob: be092c05de01ab96fe37106930dbcf52b00b5111 [file] [log] [blame]
Scott Baker23429032016-09-06 12:02:39 -07001from core.models import *
2from services.vtn.models import VTNService
3
4VTN_SERVCOMP_KINDS=["PRIVATE","VSG"]
5
6class VTNNetwork(object):
7 def __init__(self, xos_network=None):
8 self.xos_network = xos_network
9
10 def get_controller_network(self):
11 for cn in self.xos_network.controllernetworks.all():
12 # TODO: find the right one
13 return cn
14 return None
15
16 def get_cn_field(self, fieldname):
17 cn=self.get_controller_network()
18 if not cn:
19 return None
20 return getattr(cn, fieldname)
21
22 @property
23 def id(self):
24 return self.get_cn_field("net_id")
25
26 @property
27 def name(self):
28 return self.xos_network.name
29
30 @property
31 def subnet(self):
32 return self.get_cn_field("subnet")
33
34 @property
35 def gateway(self):
36 return self.get_cn_field("gateway")
37
38 @property
39 def segmentation_id(self):
40 return self.get_cn_field("segmentation_id")
41
42 @property
43 def type(self):
44 return self.xos_network.template.vtn_kind
45
46 @property
47 def providerNetworks(self):
48 slice = self.xos_network.owner
49 service = slice.service
50 if not service:
51 return []
52
53 nets=[]
54 for tenant in service.subscribed_tenants.all():
55 if tenant.provider_service:
56 bidirectional = tenant.connect_method!="private-unidirectional"
57 for slice in tenant.provider_service.slices.all():
58 for net in slice.networks.all():
59 if net.template.vtn_kind not in VTN_SERVCOMP_KINDS:
60 continue
61
62 if not net.controllernetworks.exists():
63 continue
64
65 cn = net.controllernetworks.all()[0]
66 nets.append({"id": cn.net_id,
67 "name": net.name,
68 "bidirectional": bidirectional})
69 return nets
70
71 @property
72 def subscriberNetworks(self):
73 slice = self.xos_network.owner
74 service = slice.service
75 if not service:
76 return []
77
78 nets=[]
79 for tenant in service.provided_tenants.all():
80 if tenant.subscriber_service:
81 bidirectional = tenant.connect_method!="private-unidirectional"
82 for slice in tenant.subscriber_service.slices.all():
83 for net in slice.networks.all():
84 if net.template.vtn_kind not in VTN_SERVCOMP_KINDS:
85 continue
86
87 if not net.controllernetworks.exists():
88 continue
89
90 cn = net.controllernetworks.all()[0]
91 nets.append({"id": cn.net_id,
92 "name": net.name,
93 "bidirectional": bidirectional})
94 return nets
95
96 @property
97 def ownerSliceName(self):
98 if self.xos_network.owner:
99 return self.xos_network.owner.name
100 return None
101
102 @property
103 def ownerServiceName(self):
104 if self.xos_network.owner and self.xos_network.owner.service:
105 return self.xos_network.owner.service.name
106 return None
107
108class VTNPort(object):
109 def __init__(self, xos_port=None):
110 self.xos_port = xos_port
111
112 def get_controller_network(self):
113 for cn in self.xos_port.network.controllernetworks.all():
114 # TODO: find the right one
115 return cn
116 return None
117
118 def get_vsg_tenant(self):
119 from services.vsg.models import VSGTenant
120 for tenant in VSGTenant.get_tenant_objects().all():
121 if tenant.instance == self.xos_port.instance:
122 return tenant
123 return None
124
125 @property
126 def vlan_id(self):
127 if not self.xos_port.instance:
128 return None
129 tags = Tag.select_by_content_object(self.xos_port.instance).filter(name="s_tag")
130 if not tags:
131 return None
132 return tags[0].value
133
134 @property
135 def floating_address_pairs(self):
136 address_pairs = []
137 vsg = self.get_vsg_tenant()
138 if vsg:
139 if vsg.wan_container_ip and vsg.wan_container_mac:
140 address_pairs.append({"ip_address": vsg.wan_container_ip,
141 "mac_address": vsg.wan_container_mac})
142
143 if vsg.wan_vm_ip and vsg.wan_vm_mac:
144 address_pairs.append({"ip_address": vsg.wan_vm_ip,
145 "mac_address": vsg.wan_vm_mac})
146
147 return address_pairs
148
149 @property
150 def id(self):
151 return self.xos_port.port_id
152
153 @property
154 def name(self):
155 return "port-%s" % self.xos_port.id
156
157 @property
158 def network_id(self):
159 cn = self.get_controller_network()
160 if not cn:
161 return None
162 return cn.net_id
163
164 @property
165 def mac_address(self):
166 return self.xos_port.mac
167
168 @property
169 def ip_address(self):
170 return self.xos_port.ip
171