blob: 3f8fac02aef263df08680a6b812a56460ee79ac6 [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]
Scott Baker43da8a12017-01-18 08:28:49 -080062
63 if not cn.net_id:
64 continue
65
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000066 nets.append({"id": cn.net_id,
67 "name": net.name,
68 "bidirectional": bidirectional})
Scott Baker23429032016-09-06 12:02:39 -070069 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"
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000082 for net in tenant.subscriber_service.get_composable_networks():
83 if not net.controllernetworks.exists():
84 continue
Scott Baker23429032016-09-06 12:02:39 -070085
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000086 cn = net.controllernetworks.all()[0]
Scott Baker43da8a12017-01-18 08:28:49 -080087
88 if not cn.net_id:
89 continue
90
Srikanth Vavilapalli9c1c66f2016-12-03 23:52:16 +000091 nets.append({"id": cn.net_id,
92 "name": net.name,
93 "bidirectional": bidirectional})
Scott Baker23429032016-09-06 12:02:39 -070094 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
Scott Bakerb3a80de2016-09-06 16:51:27 -0700108 def to_dict(self):
109 return {"id": self.id,
110 "name": self.name,
111 "subnet": self.subnet,
112 "gateway": self.gateway,
113 "segmentation_id": self.segmentation_id,
114 "type": self.type,
115 "providerNetworks": self.providerNetworks,
116 "subscriberNetworks": self.subscriberNetworks,
117 "ownerSliceName": self.ownerSliceName,
118 "ownerServiceName": self.ownerServiceName}
119
120 def __eq__(self, other):
121 return self.to_dict() == other.to_dict()
122
Scott Baker23429032016-09-06 12:02:39 -0700123class VTNPort(object):
124 def __init__(self, xos_port=None):
125 self.xos_port = xos_port
126
127 def get_controller_network(self):
128 for cn in self.xos_port.network.controllernetworks.all():
129 # TODO: find the right one
130 return cn
131 return None
132
133 def get_vsg_tenant(self):
134 from services.vsg.models import VSGTenant
135 for tenant in VSGTenant.get_tenant_objects().all():
136 if tenant.instance == self.xos_port.instance:
137 return tenant
138 return None
139
140 @property
141 def vlan_id(self):
142 if not self.xos_port.instance:
143 return None
Scott Baker9837fb82016-09-26 13:43:57 -0700144 # Only some kinds of networks can have s-tags associated with them.
145 # Currently, only VSG access networks qualify.
146 if not self.xos_port.network.template.vtn_kind in ["VSG",]:
147 return None
Scott Baker23429032016-09-06 12:02:39 -0700148 tags = Tag.select_by_content_object(self.xos_port.instance).filter(name="s_tag")
149 if not tags:
150 return None
151 return tags[0].value
152
153 @property
154 def floating_address_pairs(self):
Scott Baker9837fb82016-09-26 13:43:57 -0700155 # Floating_address_pairs is the set of WAN addresses that should be
156 # applied to this port.
Scott Baker23429032016-09-06 12:02:39 -0700157
Scott Baker9837fb82016-09-26 13:43:57 -0700158 address_pairs = []
159
160 # only look apply the VSG addresses if the Network is of the VSG vtn_kind
161 if self.xos_port.network.template.vtn_kind in ["VSG", ]:
162 vsg = self.get_vsg_tenant()
163 if vsg:
164 if vsg.wan_container_ip and vsg.wan_container_mac:
165 address_pairs.append({"ip_address": vsg.wan_container_ip,
166 "mac_address": vsg.wan_container_mac})
167
168 if vsg.wan_vm_ip and vsg.wan_vm_mac:
169 address_pairs.append({"ip_address": vsg.wan_vm_ip,
170 "mac_address": vsg.wan_vm_mac})
Scott Baker23429032016-09-06 12:02:39 -0700171
172 return address_pairs
173
174 @property
175 def id(self):
176 return self.xos_port.port_id
177
178 @property
179 def name(self):
180 return "port-%s" % self.xos_port.id
181
182 @property
183 def network_id(self):
184 cn = self.get_controller_network()
185 if not cn:
186 return None
187 return cn.net_id
188
189 @property
Scott Baker9837fb82016-09-26 13:43:57 -0700190 def network_name(self):
191 return self.xos_port.network.name
192
193 @property
Scott Baker23429032016-09-06 12:02:39 -0700194 def mac_address(self):
195 return self.xos_port.mac
196
197 @property
198 def ip_address(self):
199 return self.xos_port.ip
200
Scott Bakerb3a80de2016-09-06 16:51:27 -0700201 def to_dict(self):
202 return {"id": self.id,
203 "name": self.name,
204 "network_id": self.network_id,
205 "mac_address": self.mac_address,
206 "ip_address": self.ip_address,
207 "floating_address_pairs": self.floating_address_pairs,
208 "vlan_id": self.vlan_id}
209
210 def __eq__(self, other):
211 return self.to_dict() == other.to_dict()
212