blob: 6de50686659148c9574e6c1659ec611ae11643b6 [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
25class SyncVSPGWCTenant(SyncInstanceUsingAnsible):
JianHao4a9550f2017-10-19 11:05:14 +080026 observes = VSPGWCTenant
JianHao4a9550f2017-10-19 11:05:14 +080027 template_name = "vspgwctenant_playbook.yaml"
JianHao4a9550f2017-10-19 11:05:14 +080028 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
29
30 def __init__(self, *args, **kwargs):
31 super(SyncVSPGWCTenant, self).__init__(*args, **kwargs)
32
Woojoong Kimf000eda2017-10-20 15:00:43 -070033 def get_extra_attributes(self, o):
JianHao4a9550f2017-10-19 11:05:14 +080034
Woojoong Kim1a1e3702017-10-27 13:26:34 -070035 scenario = self.get_scenario()
JianHao4a9550f2017-10-19 11:05:14 +080036
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080037 if scenario == 'normal_scenario':
38 return self.get_values_for_normal_scenario()
39 elif scenario == 'normal_scenario_without_sdncontroller':
40 return self.get_values_for_normal_scenario_wo_sdncontroller()
41 elif scenario == 'emulator_scenario':
42 return self.get_values_for_emulator_scenario()
43 elif scenario == 'emulator_scenario_without_sdncontroller':
44 return self.get_values_for_emulator_scenario_wo_sdncontroller()
Woojoong Kimf000eda2017-10-20 15:00:43 -070045 else:
Woojoong Kim1a1e3702017-10-27 13:26:34 -070046 return self.get_extra_attributes_for_manual()
Woojoong Kimf000eda2017-10-20 15:00:43 -070047
48 # fields for manual case
49 def get_extra_attributes_for_manual(self):
50 fields = {}
51 fields['scenario'] = self.get_scenario()
52 # 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 Kim4f09cbf2017-11-19 21:25:37 -080071 def get_values_for_normal_scenario(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -070072 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080073 fields['scenario'] = "normal_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -070074 # for interface.cfg file
75 fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
76 fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
77 fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
78 fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
79 fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
80 fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
81
82 # for cp_config.cfg file
83 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
84 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
85 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
86
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080087 # for rules setup in ONOS
88 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
89 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
90
Woojoong Kim1a1e3702017-10-27 13:26:34 -070091 return fields
92
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080093 def get_values_for_normal_scenario_wo_sdncontroller(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -070094 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080095 fields['scenario'] = "normal_scenario_without_sdncontroller"
Woojoong Kim1a1e3702017-10-27 13:26:34 -070096 # for interface.cfg file
97 fields['zmq_sub_ip'] = "127.0.0.1"
98 fields['zmq_pub_ip'] = "127.0.0.1"
99 fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
100 fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
101 fields['fpc_ip'] = "127.0.0.1"
102 fields['cp_nb_server_ip'] = "127.0.0.1"
103
104 # for cp_config.cfg file
105 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
106 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
107 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
108
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800109 # for rules setup in ONOS
110 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
111 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
112
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700113 return fields
114
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800115 def get_values_for_emulator_scenario(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700116 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800117 fields['scenario'] = "emulator_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700118 # for interface.cfg file
119 fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
120 fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
121 fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
122 fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
123 fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
124 fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
125
126 # for cp_config.cfg file
127 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
128 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
129 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
130
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800131 # for rules setup in ONOS
132 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
133 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
134
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700135 return fields
136
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800137 def get_values_for_emulator_scenario_wo_sdncontroller(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700138 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800139 fields['scenario'] = "emulator_scenario_without_sdncontroller"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700140 # for interface.cfg file
141 fields['zmq_sub_ip'] = "127.0.0.1"
142 fields['zmq_pub_ip'] = "127.0.0.1"
143 fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
144 fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
145 fields['fpc_ip'] = "127.0.0.1"
146 fields['cp_nb_server_ip'] = "127.0.0.1"
147
148 # for cp_config.cfg file
149 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
150 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
151 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
152
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800153 # for rules setup in ONOS
154 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
155 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
156
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700157 return fields
158
159
Woojoong Kimf000eda2017-10-20 15:00:43 -0700160 def has_venb(self):
161 # try get vMME instance
162 try:
163 instance_id = self.get_instance_id(VENBServiceInstance)
164 except Exception:
Sapan Bhatia7d2e7082017-11-22 10:08:11 -0500165 self.log.debug('VENBServiceInstance not found')
Woojoong Kimf000eda2017-10-20 15:00:43 -0700166 return False
167
168 return True
169
170 def has_vmme(self):
171 # try get vMME instance
172 try:
173 instance_id = self.get_instance_id(VMMETenant)
174 except Exception:
Sapan Bhatia7d2e7082017-11-22 10:08:11 -0500175 self.log.debug('VMMETenant not found')
Woojoong Kimf000eda2017-10-20 15:00:43 -0700176 return False
177
178 return True
179
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700180 def has_sdncontroller(self):
181 # try get vMME instance
182 try:
183 instance_id = self.get_instance_id(SDNControllerServiceInstance)
184 except Exception:
Sapan Bhatia7d2e7082017-11-22 10:08:11 -0500185 self.log.debug('SDNControllerServiceInstance not found')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700186 return False
187
188 return True
189
190 def has_vspgwu(self):
191 # try get vMME instance
192 try:
193 instance_id = self.get_instance_id(VSPGWUTenant)
194 except Exception:
Sapan Bhatia7d2e7082017-11-22 10:08:11 -0500195 self.log.debug('VSPGWUTenant instance not found')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700196 return False
197
198 return True
199
200 def has_internetemulator(self):
201 # try get vMME instance
202 try:
203 instance_id = self.get_instance_id(InternetEmulatorServiceInstance)
204 except Exception:
Sapan Bhatia7d2e7082017-11-22 10:08:11 -0500205 self.log.debug('InternetEmulatorServiceInstance not found')
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700206 return False
207
208 return True
209
Woojoong Kimf000eda2017-10-20 15:00:43 -0700210
Woojoong Kimf000eda2017-10-20 15:00:43 -0700211 def get_scenario(self):
212 # try get vENB instance: one of both Spirent and NG4T
213 venb_flag = self.has_venb()
214 vmme_flag = self.has_vmme()
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700215 sdncontroller_flag = self.has_sdncontroller()
216 vspgwu_flag = self.has_vspgwu()
217 internetemulator_flag = self.has_internetemulator()
Woojoong Kimf000eda2017-10-20 15:00:43 -0700218
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800219 # wait until vspgwu and env are comming up
220 while (not vspgwu_flag):
221 print "wait -- vSPGWU has not been comming up"
222 time.sleep(1)
223 vspgwu_flag = self.has_vspgwu()
224
225 while (not venb_flag):
226 print "wait -- vENB has not been comming up"
227 time.sleep(1)
228 venb_flag = self.has_venb()
229
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700230 if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800231 return 'normal_scenario'
Woojoong Kimf000eda2017-10-20 15:00:43 -0700232
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700233 if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag:
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800234 return 'normal_scenario_without_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700235
236 if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800237 return 'emulator_scenario'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700238
239 if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (not internetemulator_flag):
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800240 return 'emulator_scenario_without_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700241
242 return 'manual'
243
244 # To get IP address
Woojoong Kimf000eda2017-10-20 15:00:43 -0700245 def get_ip_address(self, network_name, service_instance, parameter):
246
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800247 condition = False
Woojoong Kimf000eda2017-10-20 15:00:43 -0700248
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800249 while (not condition):
250 try:
251 net_id = self.get_network_id(network_name)
252 ins_id = self.get_instance_id(service_instance)
253 ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
254 condition = True
255 except Exception:
256 ip_address = "error"
Sapan Bhatia7d2e7082017-11-22 10:08:11 -0500257 self.log.error('Could not fetch parameter', parameter = parameter, network_name = network_name)
Woojoong Kimf000eda2017-10-20 15:00:43 -0700258
259 return ip_address
260
261 # To get each network id
262 def get_network_id(self, network_name):
263 return Network.objects.get(name=network_name).id
264
265 # To get service_instance (assumption: there is a single instance for each service)
266 def get_instance_id(self, serviceinstance):
267 instances = serviceinstance.objects.all()
268 instance_id = instances[0].instance_id
269 return instance_id