blob: f56988de8e9ec633cf78951d89ad1c1923971189 [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)
216 for link in node['links']:
217 flag = self.has_instance(link['name'], o)
218 if not flag:
219 self.defer_sync('%s does not have an instance. Deferring synchronization.'%link['name'])
220
221 def get_blueprint_and_check_dependencies(self, o):
222 blueprints = Config().get('blueprints')
223
224 adj_set = self.adj_set_of_service_graph(o)
225 blueprint_name = self.find_first_blueprint_subgraph(blueprints, adj_set)
226 if blueprint_name:
227 self.check_instance_dependencies(blueprints, blueprint_name, o)
228
229 ret_blueprint = 'manual' if not blueprint_name else blueprint_name
230 return ret_blueprint
Woojoong Kimf002aec2017-10-25 14:43:37 -0700231
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500232 def get_peer_serviceinstance_of_type(self, sitype, o):
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800233
234 global_list = self.get_all_instances_in_graph(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -0700235
236 try:
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800237 peer_service = next(p for p in global_list if p.leaf_model_name == sitype)
238
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500239 except StopIteration:
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800240 self.log.error(
241 'Could not find service type in service graph', service_type=sitype, object=o)
242 raise ServiceGraphException(
243 "Synchronization failed due to incomplete service graph")
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500244
245 return peer_service
246
Woojoong Kim4f3ea432018-01-22 17:35:52 -0800247 def has_instance_in_list(self, list, o):
248 for instance in list:
249 if instance.leaf_model_name == o.leaf_model_name:
250 return True
251
252 return False
253
254 def get_all_instances_in_graph(self, o):
255
256 to_search_list = self.get_one_hop_instances_in_graph(o)
257 result_list = []
258
259 while len(to_search_list) > 0:
260 tmp_obj = to_search_list[0]
261 to_search_list.remove(tmp_obj)
262 tmp_list = self.get_one_hop_instances_in_graph(tmp_obj)
263
264 for index_obj in tmp_list:
265 if (not self.has_instance_in_list(to_search_list, index_obj)) and (not self.has_instance_in_list(result_list, index_obj)):
266 to_search_list.append(index_obj)
267
268 result_list.append(tmp_obj)
269 return result_list
270
271 def get_one_hop_instances_in_graph(self, o):
272 instance_list = []
273
274 # 1 hop forward and backward
275 prov_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id=o.id)
276 subs_links = ServiceInstanceLink.objects.filter(provider_service_instance_id=o.id)
277
278 # add instances located in 1 hop into instance_list
279 for tmp_link1 in prov_links:
280 if not self.has_instance_in_list(instance_list, tmp_link1.provider_service_instance):
281 instance_list.append(tmp_link1.provider_service_instance)
282
283 for tmp_link1 in subs_links:
284 if not self.has_instance_in_list(instance_list, tmp_link1.subscriber_service_instance):
285 instance_list.append(tmp_link1.subscriber_service_instance)
286
287 return instance_list
288
289
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500290 # Maybe merge the two pairs of functions into one, with an address type "mac" or "ip" - SB
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500291 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
292 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500293 return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500294
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500295 def get_mac_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
296 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
297 return self.get_mac_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
298
299 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None):
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500300 try:
Woojoong Kimf002aec2017-10-25 14:43:37 -0700301 net_id = self.get_network_id(network_name)
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500302 ins_id = peer_si.leaf_model.instance_id
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500303 ip_address = Port.objects.get(
304 network_id=net_id, instance_id=ins_id).ip
Woojoong Kimf002aec2017-10-25 14:43:37 -0700305 except Exception:
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500306 self.log.error("Failed to fetch parameter",
307 parameter=parameter,
308 network_name=network_name)
309 self.defer_sync(o, "Waiting for parameters to become available")
Woojoong Kimf002aec2017-10-25 14:43:37 -0700310
311 return ip_address
312
Sapan Bhatia15721932017-12-08 15:36:33 -0500313 def get_mac_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter):
Woojoong Kim04d12162017-11-27 10:59:08 -0800314 try:
315 net_id = self.get_network_id(network_name)
Sapan Bhatia15721932017-12-08 15:36:33 -0500316 ins_id = peer_si.leaf_model.instance_id
317 mac_address = Port.objects.get(
318 network_id=net_id, instance_id=ins_id).mac
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500319
Woojoong Kim04d12162017-11-27 10:59:08 -0800320 except Exception:
Sapan Bhatia15721932017-12-08 15:36:33 -0500321 self.log.error("Failed to fetch parameter to get MAC",
322 parameter=parameter, network_name=network_name)
Sapan Bhatia2f27dbe2017-11-27 21:17:32 -0500323 self.defer_sync(o, "Waiting for parameters to become available")
Woojoong Kim04d12162017-11-27 10:59:08 -0800324
325 return mac_address
326
Woojoong Kimf002aec2017-10-25 14:43:37 -0700327 # To get each network id
328 def get_network_id(self, network_name):
329 return Network.objects.get(name=network_name).id
Sapan Bhatiaae309652018-01-23 23:40:40 -0500330