blob: 0318ad68f853c8a994db359bf0e480d64f082d0e [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 Bhatia28b75602017-11-28 14:18:21 -050025class ServiceGraphException(Exception):
26 pass
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050027
JianHao4a9550f2017-10-19 11:05:14 +080028class SyncVSPGWCTenant(SyncInstanceUsingAnsible):
JianHao4a9550f2017-10-19 11:05:14 +080029 observes = VSPGWCTenant
JianHao4a9550f2017-10-19 11:05:14 +080030 template_name = "vspgwctenant_playbook.yaml"
JianHao4a9550f2017-10-19 11:05:14 +080031 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
32
33 def __init__(self, *args, **kwargs):
34 super(SyncVSPGWCTenant, self).__init__(*args, **kwargs)
35
Woojoong Kimf000eda2017-10-20 15:00:43 -070036 def get_extra_attributes(self, o):
JianHao4a9550f2017-10-19 11:05:14 +080037
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050038 scenario = self.get_scenario(o)
JianHao4a9550f2017-10-19 11:05:14 +080039
Woojoong Kim103fba02018-01-18 20:01:06 -080040 if scenario == 'cord_4_1_scenario':
41 return self.get_values_for_CORD_4_1(o)
42 elif scenario == 'cord_5_0_scenario':
43 return self.get_values_for_CORD_5_0(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070044 else:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050045 return self.get_extra_attributes_for_manual(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070046
47 # fields for manual case
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050048 def get_extra_attributes_for_manual(self, o):
Woojoong Kimf000eda2017-10-20 15:00:43 -070049 fields = {}
Woojoong Kim103fba02018-01-18 20:01:06 -080050 fields['scenario'] = "manual"
51 fields['cord_version'] = "manual"
Woojoong Kimf000eda2017-10-20 15:00:43 -070052 # for interface.cfg file
53 fields['zmq_sub_ip'] = "manual"
54 fields['zmq_pub_ip'] = "manual"
55 fields['dp_comm_ip'] = "manual"
56 fields['cp_comm_ip'] = "manual"
57 fields['fpc_ip'] = "manual"
58 fields['cp_nb_server_ip'] = "manual"
59
60 # for cp_config.cfg file
61 fields['s11_sgw_ip'] = "manual"
62 fields['s11_mme_ip'] = "manual"
63 fields['s1u_sgw_ip'] = "manual"
64
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080065 # for rules setup in ONOS
66 fields['sgi_as_ip'] = "manual"
67 fields['sgi_spgwu_ip'] = "manual"
68
Woojoong Kimf000eda2017-10-20 15:00:43 -070069 return fields
70
Woojoong Kim103fba02018-01-18 20:01:06 -080071 def get_values_for_CORD_4_1(self, o):
Woojoong Kim1a1e3702017-10-27 13:26:34 -070072 fields = {}
Woojoong Kim103fba02018-01-18 20:01:06 -080073 fields['cord_version'] = "4.1"
74 fields['scenario'] = "cord_4_1_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -070075 # for interface.cfg file
76 fields['zmq_sub_ip'] = "127.0.0.1"
77 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050078 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050079 'spgw_network', "VSPGWUTenant", o, 'dp_comm_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050080 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050081 'spgw_network', o, o, 'cp_comm_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -070082 fields['fpc_ip'] = "127.0.0.1"
83 fields['cp_nb_server_ip'] = "127.0.0.1"
84
85 # for cp_config.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050086 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050087 's11_network', o, o, 's11_sgw_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050088 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050089 's1u_network', "VSPGWUTenant", o, 's1u_sgw_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050090 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050091 's11_network', "VENBServiceInstance", o, 's11_mme_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -070092
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080093 # for rules setup in ONOS
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050094 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050095 'sgi_network', "VENBServiceInstance", o, 'sgi_as_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050096 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050097 'sgi_network', "VSPGWUTenant", o, 'sgi_spgwu_ip')
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080098
Woojoong Kim1a1e3702017-10-27 13:26:34 -070099 return fields
100
Woojoong Kim103fba02018-01-18 20:01:06 -0800101 def get_values_for_CORD_5_0(self, o):
Woojoong Kim313d35c2017-12-18 17:15:46 -0800102 fields = {}
Woojoong Kim103fba02018-01-18 20:01:06 -0800103 fields['cord_version'] = "5.0"
104 fields['scenario'] = "cord_5_0_scenario"
Woojoong Kim313d35c2017-12-18 17:15:46 -0800105
106 # for interface.cfg file
107 fields['zmq_sub_ip'] = "127.0.0.1"
108 fields['zmq_pub_ip'] = "127.0.0.1"
109 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
110 'spgw_network', "VSPGWUTenant", o, 'dp_comm_ip')
111 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
112 'spgw_network', o, o, 'cp_comm_ip')
113 fields['fpc_ip'] = "127.0.0.1"
114 fields['cp_nb_server_ip'] = "127.0.0.1"
115
116 # for cp_config.cfg file
117 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
118 's11_network', o, o, 's11_sgw_ip')
119 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
Woojoong Kimc96c24f2018-01-22 17:37:05 -0800120 'flat_network_s1u', "VSPGWUTenant", o, 's1u_sgw_ip')
Woojoong Kim313d35c2017-12-18 17:15:46 -0800121 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
122 's11_network', "VMMETenant", o, 's11_mme_ip')
123
124 # for rules setup in ONOS
Woojoong Kim103fba02018-01-18 20:01:06 -0800125 internetemulator_flag = self.has_instance("InternetEmulatorServiceInstance", o)
126 if (internetemulator_flag):
127 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
128 'sgi_network', "InternetEmulatorServiceInstance", o, 'sgi_as_ip')
129 else:
130 fields['sgi_as_ip'] = o.appserver_ip_addr
Woojoong Kim313d35c2017-12-18 17:15:46 -0800131 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
132 'sgi_network', "VSPGWUTenant", o, 'sgi_spgwu_ip')
133
134 return fields
135
136
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500137 def get_scenario(self, o):
138 venb_flag = self.has_instance("VENBServiceInstance", o)
139 vmme_flag = self.has_instance("VMMETenant", o)
140 sdncontroller_flag = self.has_instance(
141 "SDNControllerServiceInstance", o)
142 vspgwu_flag = self.has_instance("VSPGWUTenant", o)
143 internetemulator_flag = self.has_instance(
Woojoong Kim103fba02018-01-18 20:01:06 -0800144 "InternetEmulatorServiceInstance", o)
145 vhss_flag = self.has_instance("VHSSTenant", o)
146 hssdb_flag = self.has_instance("HSSDBServiceInstance", o)
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800147
Woojoong Kim103fba02018-01-18 20:01:06 -0800148 if (o.blueprint == "build") or (o.blueprint == "MCORD 4.1"):
149 if not venb_flag:
150 self.defer_sync(o, "Waiting for eNB image to become available")
151 if not vspgwu_flag:
152 self.defer_sync(o, "Waiting for SPGWU image to become available")
153 return 'cord_4_1_scenario'
Woojoong Kimf000eda2017-10-20 15:00:43 -0700154
Woojoong Kim103fba02018-01-18 20:01:06 -0800155 if (o.blueprint == "mcord_5") or (o.blueprint == "MCORD 5"):
156 if not hssdb_flag:
157 self.defer_sync(o, "Waiting for HSS_DB image to become available")
158 if not vhss_flag:
159 self.defer_sync(o, "Waiting for vHSS image to become available")
160 if not vmme_flag:
161 self.defer_sync(o, "Waiting for vMME image to become available")
162 if not vspgwu_flag:
163 self.defer_sync(o, "Waiting for vSPGWU image to become available")
164 return 'cord_5_0_scenario'
Woojoong Kim313d35c2017-12-18 17:15:46 -0800165
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700166 return 'manual'
167
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500168 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
169 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
Sapan Bhatia97536292017-11-27 21:35:34 -0500170 return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
Woojoong Kimf000eda2017-10-20 15:00:43 -0700171
Sapan Bhatia97536292017-11-27 21:35:34 -0500172 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None):
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500173 try:
174 net_id = self.get_network_id(network_name)
Sapan Bhatia97536292017-11-27 21:35:34 -0500175 ins_id = peer_si.leaf_model.instance_id
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500176 ip_address = Port.objects.get(
177 network_id=net_id, instance_id=ins_id).ip
178 except Exception:
179 self.log.error("Failed to fetch parameter",
180 parameter=parameter,
181 network_name=network_name)
Sapan Bhatia97536292017-11-27 21:35:34 -0500182 self.defer_sync(o, "Waiting for parameters to become available")
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500183
184 return ip_address
185
186 def get_peer_serviceinstance_of_type(self, sitype, o):
Woojoong Kimc96c24f2018-01-22 17:37:05 -0800187
188 global_list = self.get_all_instances_in_graph(o)
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500189
190 try:
Woojoong Kimc96c24f2018-01-22 17:37:05 -0800191 peer_service = next(p for p in global_list if p.leaf_model_name == sitype)
Sapan Bhatia97536292017-11-27 21:35:34 -0500192
Woojoong Kimc96c24f2018-01-22 17:37:05 -0800193 except StopIteration:
194 self.log.error(
195 'Could not find service type in service graph', service_type=sitype, object=o)
196 raise ServiceGraphException(
197 "Synchronization failed due to incomplete service graph")
198
199 return peer_service
200
201 def has_instance_in_list(self, list, o):
202 for instance in list:
203 if instance.leaf_model_name == o.leaf_model_name:
204 return True
205
206 return False
207
208 def get_all_instances_in_graph(self, o):
209
210 to_search_list = self.get_one_hop_instances_in_graph(o)
211 result_list = []
212
213 while len(to_search_list) > 0:
214 tmp_obj = to_search_list[0]
215 to_search_list.remove(tmp_obj)
216 tmp_list = self.get_one_hop_instances_in_graph(tmp_obj)
217
218 for index_obj in tmp_list:
219 if (not self.has_instance_in_list(to_search_list, index_obj)) and (
220 not self.has_instance_in_list(result_list, index_obj)):
221 to_search_list.append(index_obj)
222
223 result_list.append(tmp_obj)
224 return result_list
225
226 def get_one_hop_instances_in_graph(self, o):
227 instance_list = []
228
229 # 1 hop forward and backward
230 prov_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id=o.id)
231 subs_links = ServiceInstanceLink.objects.filter(provider_service_instance_id=o.id)
232
233 # add instances located in 1 hop into instance_list
234 for tmp_link1 in prov_links:
235 if not self.has_instance_in_list(instance_list, tmp_link1.provider_service_instance):
236 instance_list.append(tmp_link1.provider_service_instance)
237
238 for tmp_link1 in subs_links:
239 if not self.has_instance_in_list(instance_list, tmp_link1.subscriber_service_instance):
240 instance_list.append(tmp_link1.subscriber_service_instance)
241
242 return instance_list
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500243
Woojoong Kimf000eda2017-10-20 15:00:43 -0700244 # To get each network id
245 def get_network_id(self, network_name):
246 return Network.objects.get(name=network_name).id
247
248 # To get service_instance (assumption: there is a single instance for each service)
249 def get_instance_id(self, serviceinstance):
250 instances = serviceinstance.objects.all()
251 instance_id = instances[0].instance_id
252 return instance_id
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500253
254 def has_instance(self, sitype, o):
Sapan Bhatia28b75602017-11-28 14:18:21 -0500255 try:
256 i = self.get_peer_serviceinstance_of_type(sitype, o)
257 except ServiceGraphException:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500258 self.log.info("Missing in ServiceInstance graph",
259 serviceinstance=sitype)
260 return False
261
Woojoong Kim38d84882017-11-28 15:38:39 -0800262 return i.leaf_model.instance_id