blob: f79eddfed40807eb2b45899acbd84782d7cfc676 [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"
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000057 for net in tenant.provider_service.get_composable_networks():
58 if not net.controllernetworks.exists():
59 continue
Scott Baker23429032016-09-06 12:02:39 -070060
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000061 cn = net.controllernetworks.all()[0]
62 nets.append({"id": cn.net_id,
63 "name": net.name,
64 "bidirectional": bidirectional})
Scott Baker23429032016-09-06 12:02:39 -070065 return nets
66
67 @property
68 def subscriberNetworks(self):
69 slice = self.xos_network.owner
70 service = slice.service
71 if not service:
72 return []
73
74 nets=[]
75 for tenant in service.provided_tenants.all():
76 if tenant.subscriber_service:
77 bidirectional = tenant.connect_method!="private-unidirectional"
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000078 for net in tenant.subscriber_service.get_composable_networks():
79 if not net.controllernetworks.exists():
80 continue
Scott Baker23429032016-09-06 12:02:39 -070081
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000082 cn = net.controllernetworks.all()[0]
83 nets.append({"id": cn.net_id,
84 "name": net.name,
85 "bidirectional": bidirectional})
Scott Baker23429032016-09-06 12:02:39 -070086 return nets
87
88 @property
89 def ownerSliceName(self):
90 if self.xos_network.owner:
91 return self.xos_network.owner.name
92 return None
93
94 @property
95 def ownerServiceName(self):
96 if self.xos_network.owner and self.xos_network.owner.service:
97 return self.xos_network.owner.service.name
98 return None
99
Scott Bakerb3a80de2016-09-06 16:51:27 -0700100 def to_dict(self):
101 return {"id": self.id,
102 "name": self.name,
103 "subnet": self.subnet,
104 "gateway": self.gateway,
105 "segmentation_id": self.segmentation_id,
106 "type": self.type,
107 "providerNetworks": self.providerNetworks,
108 "subscriberNetworks": self.subscriberNetworks,
109 "ownerSliceName": self.ownerSliceName,
110 "ownerServiceName": self.ownerServiceName}
111
112 def __eq__(self, other):
113 return self.to_dict() == other.to_dict()
114
Scott Baker23429032016-09-06 12:02:39 -0700115class VTNPort(object):
Scott Baker900e5ac2017-01-24 17:09:13 -0800116 def __init__(self, xos_port=None):
117 self.xos_port = xos_port
118
119 def get_controller_network(self):
120 for cn in self.xos_port.network.controllernetworks.all():
121 # TODO: find the right one
122 return cn
123 return None
124
125 def get_vsg_tenants(self):
Scott Baker80befbb2017-02-21 08:48:23 -0800126 # If the VSG service isn't onboarded, then return an empty list.
127 try:
128 from services.vsg.models import VSGTenant
129 vsg_tenants=[]
130 for tenant in VSGTenant.get_tenant_objects().all():
131 if tenant.instance == self.xos_port.instance:
132 vsg_tenants.append(tenant)
133 return vsg_tenants
134 except ImportError:
135 # TODO: Set up logging for this library...
136 print "Failed to import VSG, returning no tenants"
137 return []
Scott Baker900e5ac2017-01-24 17:09:13 -0800138
139 @property
140 def vlan_id(self):
141 if not self.xos_port.instance:
142 return None
143 # Only some kinds of networks can have s-tags associated with them.
144 # Currently, only VSG access networks qualify.
145 if not self.xos_port.network.template.vtn_kind in ["VSG",]:
146 return None
147 tags = Tag.select_by_content_object(self.xos_port.instance).filter(name="s_tag")
148 if not tags:
149 return None
150 return tags[0].value
151
152 @property
153 def floating_address_pairs(self):
154 # Floating_address_pairs is the set of WAN addresses that should be
155 # applied to this port.
156
157 address_pairs = []
158
159 # only look apply the VSG addresses if the Network is of the VSG vtn_kind
160 if self.xos_port.network.template.vtn_kind in ["VSG", ]:
161 for vsg in self.get_vsg_tenants():
162 if vsg.wan_container_ip and vsg.wan_container_mac:
163 address_pairs.append({"ip_address": vsg.wan_container_ip,
164 "mac_address": vsg.wan_container_mac})
165
166 if vsg.wan_vm_ip and vsg.wan_vm_mac:
167 address_pairs.append({"ip_address": vsg.wan_vm_ip,
168 "mac_address": vsg.wan_vm_mac})
169
170 return address_pairs
171
172 @property
173 def id(self):
174 return self.xos_port.port_id
175
176 @property
177 def name(self):
178 return "port-%s" % self.xos_port.id
179
180 @property
181 def network_id(self):
182 cn = self.get_controller_network()
183 if not cn:
184 return None
185 return cn.net_id
186
187 @property
188 def network_name(self):
189 return self.xos_port.network.name
190
191 @property
192 def mac_address(self):
193 return self.xos_port.mac
194
195 @property
196 def ip_address(self):
Scott Baker23429032016-09-06 12:02:39 -0700197 return self.xos_port.ip
198
Scott Bakerb3a80de2016-09-06 16:51:27 -0700199 def to_dict(self):
200 return {"id": self.id,
201 "name": self.name,
202 "network_id": self.network_id,
203 "mac_address": self.mac_address,
204 "ip_address": self.ip_address,
205 "floating_address_pairs": self.floating_address_pairs,
206 "vlan_id": self.vlan_id}
207
208 def __eq__(self, other):
209 return self.to_dict() == other.to_dict()
210