blob: f557e45f808ff716f600f7192dc720c9ee258b28 [file] [log] [blame]
Wei-Yu Chenba043592017-10-18 17:08:51 +08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import sys
17from django.db.models import Q, F
Wei-Yu Chenba043592017-10-18 17:08:51 +080018from synchronizers.new_base.modelaccessor import *
19from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
20
Sapan Bhatiaae309652018-01-23 23:40:40 -050021from collections import defaultdict
22
Wei-Yu Chenba043592017-10-18 17:08:51 +080023parentdir = os.path.join(os.path.dirname(__file__), "..")
24sys.path.insert(0, parentdir)
25
Sapan Bhatiafc9f6b32017-11-28 14:13:31 -050026class ServiceGraphException(Exception):
27 pass
Sapan Bhatia968f28f2017-11-22 12:00:01 -050028
Wei-Yu Chenba043592017-10-18 17:08:51 +080029class SyncVSPGWUTenant(SyncInstanceUsingAnsible):
Wei-Yu Chenba043592017-10-18 17:08:51 +080030 observes = VSPGWUTenant
Wei-Yu Chenba043592017-10-18 17:08:51 +080031 template_name = "vspgwutenant_playbook.yaml"
Wei-Yu Chenba043592017-10-18 17:08:51 +080032 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
33
Sapan Bhatiaae309652018-01-23 23:40:40 -050034 """ Convert ServiceInstance graph into an adjacency set"""
35 def adj_set_of_service_graph(self, o, visited = None, adj_set = None):
36 def key(o):
37 return o.leaf_model_name
38
39 if not adj_set:
40 adj_set = defaultdict(set)
41
42 if not o:
43 return adj_set
44 else:
45 if not visited:
46 visited = set()
47
48 ko = key(o)
49 visited.add(ko)
50
51 provider_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id = o.id)
52 for l in provider_links:
53 n = l.provider_service_instance
54 kn = key(n)
55 adj_set[ko].add(kn)
56 adj_set[kn].update([])
57 if kn not in visited:
58 adj_set = self.adj_set_of_service_graph(n, visited, adj_set)
59
60 subscriber_links = ServiceInstanceLink.objects.filter(provider_service_instance_id = o.id)
61 for l in subscriber_links:
62 n = l.subscriber_service_instance
63 sn = key(n)
64 adj_set[sn].add(ko)
65 adj_set[ko].update([])
66 if sn not in visited:
67 adj_set = self.adj_set_of_service_graph(n, visited, adj_set)
68
69 return adj_set
70
Wei-Yu Chenba043592017-10-18 17:08:51 +080071 def __init__(self, *args, **kwargs):
72 super(SyncVSPGWUTenant, self).__init__(*args, **kwargs)
73
Woojoong Kimf002aec2017-10-25 14:43:37 -070074 def get_extra_attributes(self, o):
Sapan Bhatiaae309652018-01-23 23:40:40 -050075 blueprint = self.get_blueprint_and_check_dependencies(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -070076
Sapan Bhatiaae309652018-01-23 23:40:40 -050077 if blueprint == 'cord_4_1_blueprint':
Woojoong Kim2f03e442018-01-18 20:02:22 -080078 return self.get_values_for_CORD_4_1(o)
Sapan Bhatiaae309652018-01-23 23:40:40 -050079 elif blueprint == 'cord_5_0_blueprint':
Woojoong Kim2f03e442018-01-18 20:02:22 -080080 return self.get_values_for_CORD_5_0(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -070081 else:
Sapan Bhatia968f28f2017-11-22 12:00:01 -050082 return self.get_extra_attributes_for_manual(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -070083
84 # fields for manual case
Sapan Bhatia968f28f2017-11-22 12:00:01 -050085 def get_extra_attributes_for_manual(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -070086 fields = {}
Sapan Bhatiaae309652018-01-23 23:40:40 -050087 fields['blueprint'] = "manual"
Woojoong Kim2f03e442018-01-18 20:02:22 -080088 fields['cord_version'] = "manual"
Woojoong Kimf002aec2017-10-25 14:43:37 -070089 # for interface.cfg file
90 fields['zmq_sub_ip'] = "manual"
91 fields['zmq_pub_ip'] = "manual"
92 fields['dp_comm_ip'] = "manual"
93 fields['cp_comm_ip'] = "manual"
94 fields['fpc_ip'] = "manual"
95 fields['cp_nb_server_ip'] = "manual"
96
97 # for dp_config.cfg file
98 fields['s1u_ip'] = "manual"
99 fields['sgi_ip'] = "manual"
100
Woojoong Kim04d12162017-11-27 10:59:08 -0800101 # for static_arp.cfg file
102 fields['as_sgi_ip'] = "manual"
103 fields['as_sgi_mac'] = "manual"
104 fields['enb_s1u_ip'] = "manual"
105 fields['enb_s1u_mac'] = "manual"
106
Woojoong Kimf002aec2017-10-25 14:43:37 -0700107 return fields
108
Woojoong Kim2f03e442018-01-18 20:02:22 -0800109 def get_values_for_CORD_4_1(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -0700110 fields = {}
Woojoong Kim2f03e442018-01-18 20:02:22 -0800111 fields['cord_version'] = "4.1"
Sapan Bhatiaae309652018-01-23 23:40:40 -0500112 fields['blueprint'] = "cord_4_1_blueprint"
Woojoong Kimf002aec2017-10-25 14:43:37 -0700113 # for interface.cfg file
114 fields['zmq_sub_ip'] = "127.0.0.1"
115 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500116 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500117 'spgw_network', o, o, 'dp_comm_ip')
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500118 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500119 'spgw_network', "VSPGWCTenant", o, 'cp_comm_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700120 fields['fpc_ip'] = "127.0.0.1"
121 fields['cp_nb_server_ip'] = "127.0.0.1"
122
123 # for cp_config.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500124 fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500125 's1u_network', o, o, 's1u_ip')
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500126 fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500127 'sgi_network', o, o, 'sgi_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700128
Woojoong Kim04d12162017-11-27 10:59:08 -0800129 # for static_arp.cfg file
Sapan Bhatia15721932017-12-08 15:36:33 -0500130 fields['as_sgi_ip'] = self.get_ip_address_from_peer_service_instance(
131 'sgi_network', "VENBServiceInstance", o, 'as_sgi_ip')
132 fields['as_sgi_mac'] = self.get_mac_address_from_peer_service_instance(
133 'sgi_network', "VENBServiceInstance", o, 'as_sgi_mac')
134 fields['enb_s1u_ip'] = self.get_ip_address_from_peer_service_instance(
135 's1u_network', "VENBServiceInstance", o, 'enb_s1u_ip')
136 fields['enb_s1u_mac'] = self.get_mac_address_from_peer_service_instance(
137 's1u_network', "VENBServiceInstance", o, 'enb_s1u_mac')
Woojoong Kim04d12162017-11-27 10:59:08 -0800138
Woojoong Kimf002aec2017-10-25 14:43:37 -0700139 return fields
140
Woojoong Kim2f03e442018-01-18 20:02:22 -0800141 def get_values_for_CORD_5_0(self, o):
Woojoong Kim4a15d412017-12-18 17:17:40 -0800142 fields = {}
Woojoong Kim2f03e442018-01-18 20:02:22 -0800143 fields['cord_version'] = "5.0"
Sapan Bhatiaae309652018-01-23 23:40:40 -0500144 fields['blueprint'] = "cord_5_0_blueprint"
Woojoong Kim4a15d412017-12-18 17:17:40 -0800145
Woojoong Kim4a15d412017-12-18 17:17:40 -0800146 # for interface.cfg file
147 fields['zmq_sub_ip'] = "127.0.0.1"
148 fields['zmq_pub_ip'] = "127.0.0.1"
149 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
150 'spgw_network', o, o, 'dp_comm_ip')
151 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
152 'spgw_network', "VSPGWCTenant", o, 'cp_comm_ip')
153 fields['fpc_ip'] = "127.0.0.1"
154 fields['cp_nb_server_ip'] = "127.0.0.1"
155
Woojoong Kim2f03e442018-01-18 20:02:22 -0800156 # for cp_config.cfg file
Woojoong Kim4a15d412017-12-18 17:17:40 -0800157 fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800158 'flat_network_s1u', o, o, 's1u_ip')
Woojoong Kim4a15d412017-12-18 17:17:40 -0800159 fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance(
160 'sgi_network', o, o, 'sgi_ip')
161
162 # for static_arp.cfg file
Woojoong Kim2f03e442018-01-18 20:02:22 -0800163 internetemulator_flag = self.has_instance("InternetEmulatorServiceInstance", o)
164 if (internetemulator_flag):
165 fields['as_sgi_ip'] = self.get_ip_address_from_peer_service_instance('sgi_network', "InternetEmulatorServiceInstance", o, 'as_sgi_ip')
166 fields['as_sgi_mac'] = self.get_mac_address_from_peer_service_instance('sgi_network', "InternetEmulatorServiceInstance", o, 'as_sgi_mac')
167 else:
168 fields['as_sgi_ip'] = o.appserver_ip_addr
169 fields['as_sgi_mac'] = o.appserver_mac_addr
170 fields['enb_s1u_ip'] = o.enodeb_ip_addr # write down eNB IP address manually (S1U)
171 fields['enb_s1u_mac'] = o.enodeb_mac_addr # write down eNB MAC address manually (S1U)
Woojoong Kim4a15d412017-12-18 17:17:40 -0800172
173 return fields
174
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500175 def has_instance(self, sitype, o):
Sapan Bhatiafc9f6b32017-11-28 14:13:31 -0500176 try:
177 i = self.get_peer_serviceinstance_of_type(sitype, o)
178 except ServiceGraphException:
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500179 self.log.info("Missing in ServiceInstance graph",
180 serviceinstance=sitype)
Woojoong Kimf002aec2017-10-25 14:43:37 -0700181 return False
182
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500183 return i.leaf_model.instance_id
Woojoong Kimf002aec2017-10-25 14:43:37 -0700184
Sapan Bhatiaae309652018-01-23 23:40:40 -0500185 def find_first_blueprint_subgraph(self, blueprints, adj_set):
186 found_blueprint = None
187 for blueprint in blueprints:
188 found = True
Woojoong Kimf002aec2017-10-25 14:43:37 -0700189
Sapan Bhatiaae309652018-01-23 23:40:40 -0500190 for node in blueprint['graph']:
191 if node['name'] not in adj_set:
192 found = False
193 break
194 try:
195 links = node['links']
196 except KeyError:
197 links = []
Woojoong Kimf002aec2017-10-25 14:43:37 -0700198
Sapan Bhatiaae309652018-01-23 23:40:40 -0500199 for link in links:
200 if link['name'] not in adj_set[node['name']]:
201 found = False
202 break
203 if not found:
204 break
Woojoong Kim4a15d412017-12-18 17:17:40 -0800205
Sapan Bhatiaae309652018-01-23 23:40:40 -0500206 if found:
207 found_blueprint = blueprint['name']
208 break
209
210 return found_blueprint
211
212 def check_instance_dependencies(self, blueprints, blueprint_name, o):
213 blueprint = next(
214 b for b in blueprints if b['name'] == blueprint_name)
215 node = next(n for n in blueprint['graph'] if n['name'] == o.leaf_model_name)
Andy Bavier2a0f9f32018-01-31 16:01:08 -0700216
217 try:
218 links = node['links']
219 except KeyError:
220 links = []
221
222 for link in links:
Sapan Bhatiaae309652018-01-23 23:40:40 -0500223 flag = self.has_instance(link['name'], o)
224 if not flag:
225 self.defer_sync('%s does not have an instance. Deferring synchronization.'%link['name'])
Andy Bavier2a0f9f32018-01-31 16:01:08 -0700226
Sapan Bhatiaae309652018-01-23 23:40:40 -0500227 def get_blueprint_and_check_dependencies(self, o):
228 blueprints = Config().get('blueprints')
229
230 adj_set = self.adj_set_of_service_graph(o)
231 blueprint_name = self.find_first_blueprint_subgraph(blueprints, adj_set)
232 if blueprint_name:
233 self.check_instance_dependencies(blueprints, blueprint_name, o)
234
235 ret_blueprint = 'manual' if not blueprint_name else blueprint_name
236 return ret_blueprint
Woojoong Kimf002aec2017-10-25 14:43:37 -0700237
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500238 def get_peer_serviceinstance_of_type(self, sitype, o):
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800239
240 global_list = self.get_all_instances_in_graph(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -0700241
242 try:
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800243 peer_service = next(p for p in global_list if p.leaf_model_name == sitype)
244
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500245 except StopIteration:
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800246 self.log.error(
247 'Could not find service type in service graph', service_type=sitype, object=o)
248 raise ServiceGraphException(
249 "Synchronization failed due to incomplete service graph")
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500250
251 return peer_service
252
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800253 def has_instance_in_list(self, list, o):
254 for instance in list:
255 if instance.leaf_model_name == o.leaf_model_name:
256 return True
257
258 return False
259
260 def get_all_instances_in_graph(self, o):
261
262 to_search_list = self.get_one_hop_instances_in_graph(o)
263 result_list = []
264
265 while len(to_search_list) > 0:
266 tmp_obj = to_search_list[0]
267 to_search_list.remove(tmp_obj)
268 tmp_list = self.get_one_hop_instances_in_graph(tmp_obj)
269
270 for index_obj in tmp_list:
271 if (not self.has_instance_in_list(to_search_list, index_obj)) and (not self.has_instance_in_list(result_list, index_obj)):
272 to_search_list.append(index_obj)
273
274 result_list.append(tmp_obj)
275 return result_list
276
277 def get_one_hop_instances_in_graph(self, o):
278 instance_list = []
279
280 # 1 hop forward and backward
281 prov_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id=o.id)
282 subs_links = ServiceInstanceLink.objects.filter(provider_service_instance_id=o.id)
283
284 # add instances located in 1 hop into instance_list
285 for tmp_link1 in prov_links:
286 if not self.has_instance_in_list(instance_list, tmp_link1.provider_service_instance):
287 instance_list.append(tmp_link1.provider_service_instance)
288
289 for tmp_link1 in subs_links:
290 if not self.has_instance_in_list(instance_list, tmp_link1.subscriber_service_instance):
291 instance_list.append(tmp_link1.subscriber_service_instance)
292
293 return instance_list
294
295
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500296 # Maybe merge the two pairs of functions into one, with an address type "mac" or "ip" - SB
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500297 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
298 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500299 return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500300
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500301 def get_mac_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
302 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
303 return self.get_mac_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
304
305 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None):
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500306 try:
Woojoong Kimf002aec2017-10-25 14:43:37 -0700307 net_id = self.get_network_id(network_name)
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500308 ins_id = peer_si.leaf_model.instance_id
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500309 ip_address = Port.objects.get(
310 network_id=net_id, instance_id=ins_id).ip
Woojoong Kimf002aec2017-10-25 14:43:37 -0700311 except Exception:
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500312 self.log.error("Failed to fetch parameter",
313 parameter=parameter,
314 network_name=network_name)
315 self.defer_sync(o, "Waiting for parameters to become available")
Woojoong Kimf002aec2017-10-25 14:43:37 -0700316
317 return ip_address
318
Sapan Bhatia15721932017-12-08 15:36:33 -0500319 def get_mac_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter):
Woojoong Kim04d12162017-11-27 10:59:08 -0800320 try:
321 net_id = self.get_network_id(network_name)
Sapan Bhatia15721932017-12-08 15:36:33 -0500322 ins_id = peer_si.leaf_model.instance_id
323 mac_address = Port.objects.get(
324 network_id=net_id, instance_id=ins_id).mac
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500325
Woojoong Kim04d12162017-11-27 10:59:08 -0800326 except Exception:
Sapan Bhatia15721932017-12-08 15:36:33 -0500327 self.log.error("Failed to fetch parameter to get MAC",
328 parameter=parameter, network_name=network_name)
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500329 self.defer_sync(o, "Waiting for parameters to become available")
Woojoong Kim04d12162017-11-27 10:59:08 -0800330
331 return mac_address
332
Woojoong Kimf002aec2017-10-25 14:43:37 -0700333 # To get each network id
334 def get_network_id(self, network_name):
335 return Network.objects.get(name=network_name).id
Sapan Bhatiaae309652018-01-23 23:40:40 -0500336