blob: 78c038f8071519e1ed2f7fd6446503bed31499ca [file] [log] [blame]
JianHao4a9550f2017-10-19 11:05:14 +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
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080017import time
JianHao4a9550f2017-10-19 11:05:14 +080018from django.db.models import Q, F
19from synchronizers.new_base.modelaccessor import *
20from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
21
22parentdir = os.path.join(os.path.dirname(__file__), "..")
23sys.path.insert(0, parentdir)
24
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050025
JianHao4a9550f2017-10-19 11:05:14 +080026class SyncVSPGWCTenant(SyncInstanceUsingAnsible):
JianHao4a9550f2017-10-19 11:05:14 +080027 observes = VSPGWCTenant
JianHao4a9550f2017-10-19 11:05:14 +080028 template_name = "vspgwctenant_playbook.yaml"
JianHao4a9550f2017-10-19 11:05:14 +080029 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
30
31 def __init__(self, *args, **kwargs):
32 super(SyncVSPGWCTenant, self).__init__(*args, **kwargs)
33
Woojoong Kimf000eda2017-10-20 15:00:43 -070034 def get_extra_attributes(self, o):
JianHao4a9550f2017-10-19 11:05:14 +080035
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050036 scenario = self.get_scenario(o)
JianHao4a9550f2017-10-19 11:05:14 +080037
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080038 if scenario == 'normal_scenario':
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050039 return self.get_values_for_normal_scenario(o)
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080040 elif scenario == 'normal_scenario_without_sdncontroller':
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050041 return self.get_values_for_normal_scenario_wo_sdncontroller(o)
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080042 elif scenario == 'emulator_scenario':
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050043 return self.get_values_for_emulator_scenario(o)
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080044 elif scenario == 'emulator_scenario_without_sdncontroller':
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050045 return self.get_values_for_emulator_scenario_wo_sdncontroller(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070046 else:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050047 return self.get_extra_attributes_for_manual(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070048
49 # fields for manual case
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050050 def get_extra_attributes_for_manual(self, o):
Woojoong Kimf000eda2017-10-20 15:00:43 -070051 fields = {}
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050052 fields['scenario'] = self.get_scenario(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070053 # for interface.cfg file
54 fields['zmq_sub_ip'] = "manual"
55 fields['zmq_pub_ip'] = "manual"
56 fields['dp_comm_ip'] = "manual"
57 fields['cp_comm_ip'] = "manual"
58 fields['fpc_ip'] = "manual"
59 fields['cp_nb_server_ip'] = "manual"
60
61 # for cp_config.cfg file
62 fields['s11_sgw_ip'] = "manual"
63 fields['s11_mme_ip'] = "manual"
64 fields['s1u_sgw_ip'] = "manual"
65
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080066 # for rules setup in ONOS
67 fields['sgi_as_ip'] = "manual"
68 fields['sgi_spgwu_ip'] = "manual"
69
Woojoong Kimf000eda2017-10-20 15:00:43 -070070 return fields
71
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050072 def get_values_for_normal_scenario(self, o):
Woojoong Kim1a1e3702017-10-27 13:26:34 -070073 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080074 fields['scenario'] = "normal_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -070075 # for interface.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050076 fields['zmq_sub_ip'] = self.get_ip_address_from_peer_service_instance(
77 'sbi_network', "SDNControllerServiceInstance", 'zmq_sub_ip')
78 fields['zmq_pub_ip'] = self.get_ip_address_from_peer_service_instance(
79 'sbi_network', "SDNControllerServiceInstance", 'zmq_pub_ip')
80 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
81 'sbi_network', "VSPGWUTenant", 'dp_comm_ip')
82 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
83 'nbi_network', o, 'cp_comm_ip')
84 fields['fpc_ip'] = self.get_ip_address_from_peer_service_instance(
85 'nbi_network', "SDNControllerServiceInstance", 'fpc_ip')
86 fields['cp_nb_server_ip'] = self.get_ip_address_from_peer_service_instance_instance(
87 'nbi_network', o, 'cp_nb_server_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -070088
89 # for cp_config.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050090 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
91 's11_network', o, 's11_sgw_ip')
92 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
93 's1u_network', "VSPGWUTenant", 's1u_sgw_ip')
94 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
95 's11_network', "VMMETenant", 's11_mme_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -070096
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080097 # for rules setup in ONOS
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050098 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
99 'sgi_network', "VENBServiceInstance", 'sgi_as_ip')
100 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
101 'sgi_network', "VSPGWUTenant", 'sgi_spgwu_ip')
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800102
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700103 return fields
104
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500105 def get_values_for_normal_scenario_wo_sdncontroller(self, o):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700106 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800107 fields['scenario'] = "normal_scenario_without_sdncontroller"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700108 # for interface.cfg file
109 fields['zmq_sub_ip'] = "127.0.0.1"
110 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500111 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
112 'spgw_network', "VSPGWUTenant", 'dp_comm_ip')
113 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
114 'spgw_network', o, 'cp_comm_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700115 fields['fpc_ip'] = "127.0.0.1"
116 fields['cp_nb_server_ip'] = "127.0.0.1"
117
118 # for cp_config.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500119 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
120 's11_network', o, 's11_sgw_ip')
121 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
122 's1u_network', "VSPGWUTenant", 's1u_sgw_ip')
123 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
124 's11_network', "VMMETenant", 's11_mme_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700125
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800126 # for rules setup in ONOS
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500127 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
128 'sgi_network', "VENBServiceInstance", 'sgi_as_ip')
129 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
130 'sgi_network', "VSPGWUTenant", 'sgi_spgwu_ip')
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800131
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700132 return fields
133
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500134 def get_values_for_emulator_scenario(self, o):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700135 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800136 fields['scenario'] = "emulator_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700137 # for interface.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500138 fields['zmq_sub_ip'] = self.get_ip_address_from_peer_service_instance(
139 'sbi_network', "SDNControllerServiceInstance", 'zmq_sub_ip')
140 fields['zmq_pub_ip'] = self.get_ip_address_from_peer_service_instance(
141 'sbi_network', "SDNControllerServiceInstance", 'zmq_pub_ip')
142 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
143 'sbi_network', "VSPGWUTenant", 'dp_comm_ip')
144 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
145 'nbi_network', o, 'cp_comm_ip')
146 fields['fpc_ip'] = self.get_ip_address_from_peer_service_instance(
147 'nbi_network', "SDNControllerServiceInstance", 'fpc_ip')
148 fields['cp_nb_server_ip'] = self.get_ip_address_from_peer_service_instance_instance(
149 'nbi_network', o, 'cp_nb_server_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700150
151 # for cp_config.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500152 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
153 's11_network', o, 's11_sgw_ip')
154 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
155 's1u_network', "VSPGWUTenant", 's1u_sgw_ip')
156 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
157 's11_network', "VENBServiceInstance", 's11_mme_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700158
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800159 # for rules setup in ONOS
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500160 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
161 'sgi_network', "VENBServiceInstance", 'sgi_as_ip')
162 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
163 'sgi_network', "VSPGWUTenant", 'sgi_spgwu_ip')
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800164
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700165 return fields
166
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500167 def get_values_for_emulator_scenario_wo_sdncontroller(self, o):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700168 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800169 fields['scenario'] = "emulator_scenario_without_sdncontroller"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700170 # for interface.cfg file
171 fields['zmq_sub_ip'] = "127.0.0.1"
172 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500173 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
174 'spgw_network', "VSPGWUTenant", 'dp_comm_ip')
175 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
176 'spgw_network', o, 'cp_comm_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700177 fields['fpc_ip'] = "127.0.0.1"
178 fields['cp_nb_server_ip'] = "127.0.0.1"
179
180 # for cp_config.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500181 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
182 's11_network', o, 's11_sgw_ip')
183 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
184 's1u_network', "VSPGWUTenant", 's1u_sgw_ip')
185 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
186 's11_network', "VENBServiceInstance", 's11_mme_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700187
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800188 # for rules setup in ONOS
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500189 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
190 'sgi_network', "VENBServiceInstance", 'sgi_as_ip')
191 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
192 'sgi_network', "VSPGWUTenant", 'sgi_spgwu_ip')
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800193
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700194 return fields
195
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500196 def get_scenario(self, o):
197 venb_flag = self.has_instance("VENBServiceInstance", o)
198 vmme_flag = self.has_instance("VMMETenant", o)
199 sdncontroller_flag = self.has_instance(
200 "SDNControllerServiceInstance", o)
201 vspgwu_flag = self.has_instance("VSPGWUTenant", o)
202 internetemulator_flag = self.has_instance(
203 "SDNControllerServiceInstance", o)
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800204
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700205 if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500206 return 'ng4t_with_sdncontroller'
Woojoong Kimf000eda2017-10-20 15:00:43 -0700207
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700208 if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500209 return 'ng4t_without_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700210
211 if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500212 return 'spirent_with_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700213
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500214 if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (
215 not internetemulator_flag):
216 return 'spirent_without_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700217
218 return 'manual'
219
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500220 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
221 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
222 return self.get_ip_address_for_peer_service_instance_instance(network_name, peer_si, parameter)
Woojoong Kimf000eda2017-10-20 15:00:43 -0700223
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500224 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, parameter=None):
225 try:
226 net_id = self.get_network_id(network_name)
227 ins_id = peer_si.instance_id
228 ip_address = Port.objects.get(
229 network_id=net_id, instance_id=ins_id).ip
230 except Exception:
231 self.log.error("Failed to fetch parameter",
232 parameter=parameter,
233 network_name=network_name)
234 self.defer_sync("Waiting for parameters to become available")
235
236 return ip_address
237
238 def get_peer_serviceinstance_of_type(self, sitype, o):
239 prov_link_set = ServiceInstanceLink.objects.filter(
240 subscriber_service_instance_id=o.id)
241
242 try:
243 peer_service = next(
244 p for p in prov_link_set if p.leaf_model_name == sitype)
245 except StopIteration:
246 sub_link_set = ServiceInstanceLink.objects.filter(
247 provider_service_instance_id=o.id)
248 try:
249 peer_service = next(
250 s for s in sub_link_set if s.leaf_model_name == sitype)
251 except StopIteration:
252 self.log.error(
253 'Could not find service type in service graph', service_type=sitype, object=o)
254 raise Exception(
255 "Synchronization failed due to incomplete service graph")
256
257 return peer_service
258
259 # To get IP address
260 def get_ip_address_from_peer_service_instance(self, network_name, service_instance, parameter):
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800261 condition = False
Woojoong Kimf000eda2017-10-20 15:00:43 -0700262
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800263 while (not condition):
264 try:
265 net_id = self.get_network_id(network_name)
266 ins_id = self.get_instance_id(service_instance)
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500267 ip_address = Port.objects.get(
268 network_id=net_id, instance_id=ins_id).ip
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800269 condition = True
270 except Exception:
271 ip_address = "error"
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500272 self.log.error('Could not fetch parameter',
273 parameter=parameter, network_name=network_name)
Sapan Bhatia4e1a4a72017-11-22 10:09:40 -0500274 self.defer_sync("Waiting for parameters to become available")
Woojoong Kimf000eda2017-10-20 15:00:43 -0700275
276 return ip_address
277
278 # To get each network id
279 def get_network_id(self, network_name):
280 return Network.objects.get(name=network_name).id
281
282 # To get service_instance (assumption: there is a single instance for each service)
283 def get_instance_id(self, serviceinstance):
284 instances = serviceinstance.objects.all()
285 instance_id = instances[0].instance_id
286 return instance_id
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500287
288 def has_instance(self, sitype, o):
289 i = self.get_peer_serviceinstance_of_type(sitype, o)
290 if not i:
291 self.log.info("Missing in ServiceInstance graph",
292 serviceinstance=sitype)
293 return False
294
295 return i.instance_id