blob: 0ca473c43a2ae6bc0211765bc7fd101895891aa9 [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):
26 provides = [VSPGWCTenant]
27
28 observes = VSPGWCTenant
29
30 requested_interval = 0
31
32 template_name = "vspgwctenant_playbook.yaml"
33
34 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
35
36 def __init__(self, *args, **kwargs):
37 super(SyncVSPGWCTenant, self).__init__(*args, **kwargs)
38
Woojoong Kimf000eda2017-10-20 15:00:43 -070039 def get_extra_attributes(self, o):
JianHao4a9550f2017-10-19 11:05:14 +080040
Woojoong Kim1a1e3702017-10-27 13:26:34 -070041 scenario = self.get_scenario()
JianHao4a9550f2017-10-19 11:05:14 +080042
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080043 if scenario == 'normal_scenario':
44 return self.get_values_for_normal_scenario()
45 elif scenario == 'normal_scenario_without_sdncontroller':
46 return self.get_values_for_normal_scenario_wo_sdncontroller()
47 elif scenario == 'emulator_scenario':
48 return self.get_values_for_emulator_scenario()
49 elif scenario == 'emulator_scenario_without_sdncontroller':
50 return self.get_values_for_emulator_scenario_wo_sdncontroller()
Woojoong Kimf000eda2017-10-20 15:00:43 -070051 else:
Woojoong Kim1a1e3702017-10-27 13:26:34 -070052 return self.get_extra_attributes_for_manual()
Woojoong Kimf000eda2017-10-20 15:00:43 -070053
54 # fields for manual case
55 def get_extra_attributes_for_manual(self):
56 fields = {}
57 fields['scenario'] = self.get_scenario()
58 # for interface.cfg file
59 fields['zmq_sub_ip'] = "manual"
60 fields['zmq_pub_ip'] = "manual"
61 fields['dp_comm_ip'] = "manual"
62 fields['cp_comm_ip'] = "manual"
63 fields['fpc_ip'] = "manual"
64 fields['cp_nb_server_ip'] = "manual"
65
66 # for cp_config.cfg file
67 fields['s11_sgw_ip'] = "manual"
68 fields['s11_mme_ip'] = "manual"
69 fields['s1u_sgw_ip'] = "manual"
70
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080071 # for rules setup in ONOS
72 fields['sgi_as_ip'] = "manual"
73 fields['sgi_spgwu_ip'] = "manual"
74
Woojoong Kimf000eda2017-10-20 15:00:43 -070075 return fields
76
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080077 def get_values_for_normal_scenario(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -070078 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080079 fields['scenario'] = "normal_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -070080 # for interface.cfg file
81 fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
82 fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
83 fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
84 fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
85 fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
86 fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
87
88 # for cp_config.cfg file
89 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
90 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
91 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
92
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080093 # for rules setup in ONOS
94 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
95 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
96
Woojoong Kim1a1e3702017-10-27 13:26:34 -070097 return fields
98
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080099 def get_values_for_normal_scenario_wo_sdncontroller(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700100 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800101 fields['scenario'] = "normal_scenario_without_sdncontroller"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700102 # for interface.cfg file
103 fields['zmq_sub_ip'] = "127.0.0.1"
104 fields['zmq_pub_ip'] = "127.0.0.1"
105 fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
106 fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
107 fields['fpc_ip'] = "127.0.0.1"
108 fields['cp_nb_server_ip'] = "127.0.0.1"
109
110 # for cp_config.cfg file
111 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
112 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
113 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
114
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800115 # for rules setup in ONOS
116 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
117 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
118
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700119 return fields
120
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800121 def get_values_for_emulator_scenario(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700122 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800123 fields['scenario'] = "emulator_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700124 # for interface.cfg file
125 fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
126 fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
127 fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
128 fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
129 fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
130 fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
131
132 # for cp_config.cfg file
133 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
134 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
135 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
136
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800137 # for rules setup in ONOS
138 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
139 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
140
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700141 return fields
142
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800143 def get_values_for_emulator_scenario_wo_sdncontroller(self):
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700144 fields = {}
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800145 fields['scenario'] = "emulator_scenario_without_sdncontroller"
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700146 # 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('spgw_network', VSPGWUTenant, 'dp_comm_ip')
150 fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
151 fields['fpc_ip'] = "127.0.0.1"
152 fields['cp_nb_server_ip'] = "127.0.0.1"
153
154 # for cp_config.cfg file
155 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
156 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
157 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
158
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800159 # for rules setup in ONOS
160 fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
161 fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
162
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700163 return fields
164
165
Woojoong Kimf000eda2017-10-20 15:00:43 -0700166 def has_venb(self):
167 # try get vMME instance
168 try:
169 instance_id = self.get_instance_id(VENBServiceInstance)
170 except Exception:
171 print 'cannot get VENBServiceInstance'
172 return False
173
174 return True
175
176 def has_vmme(self):
177 # try get vMME instance
178 try:
179 instance_id = self.get_instance_id(VMMETenant)
180 except Exception:
181 print 'cannot get VMMETenant'
182 return False
183
184 return True
185
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700186 def has_sdncontroller(self):
187 # try get vMME instance
188 try:
189 instance_id = self.get_instance_id(SDNControllerServiceInstance)
190 except Exception:
191 print 'cannot get SDNControllerServiceInstance'
192 return False
193
194 return True
195
196 def has_vspgwu(self):
197 # try get vMME instance
198 try:
199 instance_id = self.get_instance_id(VSPGWUTenant)
200 except Exception:
201 print 'cannot get VSPGWUTenant'
202 return False
203
204 return True
205
206 def has_internetemulator(self):
207 # try get vMME instance
208 try:
209 instance_id = self.get_instance_id(InternetEmulatorServiceInstance)
210 except Exception:
211 print 'cannot get InternetEmulatorServiceInstance'
212 return False
213
214 return True
215
Woojoong Kimf000eda2017-10-20 15:00:43 -0700216
Woojoong Kimf000eda2017-10-20 15:00:43 -0700217 def get_scenario(self):
218 # try get vENB instance: one of both Spirent and NG4T
219 venb_flag = self.has_venb()
220 vmme_flag = self.has_vmme()
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700221 sdncontroller_flag = self.has_sdncontroller()
222 vspgwu_flag = self.has_vspgwu()
223 internetemulator_flag = self.has_internetemulator()
Woojoong Kimf000eda2017-10-20 15:00:43 -0700224
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800225 # wait until vspgwu and env are comming up
226 while (not vspgwu_flag):
227 print "wait -- vSPGWU has not been comming up"
228 time.sleep(1)
229 vspgwu_flag = self.has_vspgwu()
230
231 while (not venb_flag):
232 print "wait -- vENB has not been comming up"
233 time.sleep(1)
234 venb_flag = self.has_venb()
235
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700236 if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800237 return 'normal_scenario'
Woojoong Kimf000eda2017-10-20 15:00:43 -0700238
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700239 if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag:
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800240 return 'normal_scenario_without_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700241
242 if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800243 return 'emulator_scenario'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700244
245 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 -0800246 return 'emulator_scenario_without_sdncontroller'
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700247
248 return 'manual'
249
250 # To get IP address
Woojoong Kimf000eda2017-10-20 15:00:43 -0700251 def get_ip_address(self, network_name, service_instance, parameter):
252
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800253 condition = False
Woojoong Kimf000eda2017-10-20 15:00:43 -0700254
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800255 while (not condition):
256 try:
257 net_id = self.get_network_id(network_name)
258 ins_id = self.get_instance_id(service_instance)
259 ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
260 condition = True
261 except Exception:
262 ip_address = "error"
263 print "get failed -- %s" % (parameter)
264 time.sleep(1)
Woojoong Kimf000eda2017-10-20 15:00:43 -0700265
266 return ip_address
267
268 # To get each network id
269 def get_network_id(self, network_name):
270 return Network.objects.get(name=network_name).id
271
272 # To get service_instance (assumption: there is a single instance for each service)
273 def get_instance_id(self, serviceinstance):
274 instances = serviceinstance.objects.all()
275 instance_id = instances[0].instance_id
276 return instance_id