blob: 39b8bb27cf971fbdf730c3b751840959ec0d90cc [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
21parentdir = os.path.join(os.path.dirname(__file__), "..")
22sys.path.insert(0, parentdir)
23
Sapan Bhatia968f28f2017-11-22 12:00:01 -050024
Wei-Yu Chenba043592017-10-18 17:08:51 +080025class SyncVSPGWUTenant(SyncInstanceUsingAnsible):
Wei-Yu Chenba043592017-10-18 17:08:51 +080026 observes = VSPGWUTenant
Wei-Yu Chenba043592017-10-18 17:08:51 +080027 template_name = "vspgwutenant_playbook.yaml"
Wei-Yu Chenba043592017-10-18 17:08:51 +080028 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
29
30 def __init__(self, *args, **kwargs):
31 super(SyncVSPGWUTenant, self).__init__(*args, **kwargs)
32
Woojoong Kimf002aec2017-10-25 14:43:37 -070033 def get_extra_attributes(self, o):
34
Sapan Bhatia968f28f2017-11-22 12:00:01 -050035 scenario = self.get_scenario(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -070036
Woojoong Kim04d12162017-11-27 10:59:08 -080037 if scenario == 'normal_scenario':
38 return self.get_values_for_normal_scenario(o)
39 elif scenario == 'normal_scenario_without_sdncontroller':
40 return self.get_values_for_normal_scenario_wo_sdncontroller(o)
41 elif scenario == 'emulator_scenario':
42 return self.get_values_for_emulator_scenario(o)
43 elif scenario == 'emulator_scenario_without_sdncontroller':
44 return self.get_values_for_emulator_scenario_wo_sdncontroller(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -070045 else:
Sapan Bhatia968f28f2017-11-22 12:00:01 -050046 return self.get_extra_attributes_for_manual(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -070047
48 # fields for manual case
Sapan Bhatia968f28f2017-11-22 12:00:01 -050049 def get_extra_attributes_for_manual(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -070050 fields = {}
Sapan Bhatia968f28f2017-11-22 12:00:01 -050051 fields['scenario'] = self.get_scenario(o)
Woojoong Kimf002aec2017-10-25 14:43:37 -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 dp_config.cfg file
61 fields['s1u_ip'] = "manual"
62 fields['sgi_ip'] = "manual"
63
Woojoong Kim04d12162017-11-27 10:59:08 -080064 # for static_arp.cfg file
65 fields['as_sgi_ip'] = "manual"
66 fields['as_sgi_mac'] = "manual"
67 fields['enb_s1u_ip'] = "manual"
68 fields['enb_s1u_mac'] = "manual"
69
Woojoong Kimf002aec2017-10-25 14:43:37 -070070 return fields
71
Woojoong Kim04d12162017-11-27 10:59:08 -080072 def get_values_for_normal_scenario(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -070073 fields = {}
Woojoong Kim04d12162017-11-27 10:59:08 -080074 fields['scenario'] = "normal_scenario"
Woojoong Kimf002aec2017-10-25 14:43:37 -070075 # for interface.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -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_instance(
81 'sbi_network', o, 'dp_comm_ip')
82 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
83 'nbi_network', "VSPGWCTenant", '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(
87 'nbi_network', "VSPGWCTenant", 'cp_nb_server_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -070088
89 # for dp_config.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -050090 fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance(
91 's1u_network', o, 's1u_ip')
92 fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance(
93 'sgi_network', "VSPGWUTenant", 'sgi_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -070094
Woojoong Kim04d12162017-11-27 10:59:08 -080095 # for static_arp.cfg file
96 fields['as_sgi_ip'] = self.get_ip_address('sgi_network', InternetEmulatorServiceInstance, 'as_sgi_ip')
97 fields['as_sgi_mac'] = self.get_mac_address('sgi_network', InternetEmulatorServiceInstance, 'as_sgi_mac')
98 fields['enb_s1u_ip'] = self.get_ip_address('s1u_network', VENBServiceInstance, 'enb_s1u_ip')
99 fields['enb_s1u_mac'] = self.get_mac_address('s1u_network', VENBServiceInstance, 'enb_s1u_mac')
100
Woojoong Kimf002aec2017-10-25 14:43:37 -0700101 return fields
102
Woojoong Kim04d12162017-11-27 10:59:08 -0800103 def get_values_for_normal_scenario_wo_sdncontroller(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -0700104 fields = {}
Woojoong Kim04d12162017-11-27 10:59:08 -0800105 fields['scenario'] = "normal_scenario_without_sdncontroller"
Woojoong Kimf002aec2017-10-25 14:43:37 -0700106 # for interface.cfg file
107 fields['zmq_sub_ip'] = "127.0.0.1"
108 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500109 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
110 'spgw_network', o, 'dp_comm_ip')
111 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
112 'spgw_network', "VSPGWCTenant", 'cp_comm_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700113 fields['fpc_ip'] = "127.0.0.1"
114 fields['cp_nb_server_ip'] = "127.0.0.1"
115
116 # for cp_config.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500117 fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance(
118 's1u_network', o, 's1u_ip')
119 fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance(
120 'sgi_network', o, 'sgi_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700121
Woojoong Kim04d12162017-11-27 10:59:08 -0800122 # for static_arp.cfg file
123 fields['as_sgi_ip'] = self.get_ip_address('sgi_network', InternetEmulatorServiceInstance, 'as_sgi_ip')
124 fields['as_sgi_mac'] = self.get_mac_address('sgi_network', InternetEmulatorServiceInstance, 'as_sgi_mac')
125 fields['enb_s1u_ip'] = self.get_ip_address('s1u_network', VENBServiceInstance, 'enb_s1u_ip')
126 fields['enb_s1u_mac'] = self.get_mac_address('s1u_network', VENBServiceInstance, 'enb_s1u_mac')
127
Woojoong Kimf002aec2017-10-25 14:43:37 -0700128 return fields
129
Woojoong Kim04d12162017-11-27 10:59:08 -0800130 def get_values_for_emulator_scenario(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -0700131 fields = {}
Woojoong Kim04d12162017-11-27 10:59:08 -0800132 fields['scenario'] = "emulator_scenario"
Woojoong Kimf002aec2017-10-25 14:43:37 -0700133 # for interface.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500134 fields['zmq_sub_ip'] = self.get_ip_address_from_peer_service_instance(
135 'sbi_network', "SDNControllerServiceInstance", 'zmq_sub_ip')
136 fields['zmq_pub_ip'] = self.get_ip_address_from_peer_service_instance(
137 'sbi_network', "SDNControllerServiceInstance", 'zmq_pub_ip')
138 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
139 'sbi_network', o, 'dp_comm_ip')
140 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
141 'nbi_network', "VSPGWCTenant", 'cp_comm_ip')
142 fields['fpc_ip'] = self.get_ip_address_from_peer_service_instance(
143 'nbi_network', "SDNControllerServiceInstance", 'fpc_ip')
144 fields['cp_nb_server_ip'] = self.get_ip_address_from_peer_service_instance(
145 'nbi_network', "VSPGWCTenant", 'cp_nb_server_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700146
147 # for dp_config.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500148 fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance(
149 's1u_network', o, 's1u_ip')
150 fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance(
151 'sgi_network', o, 'sgi_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700152
Woojoong Kim04d12162017-11-27 10:59:08 -0800153 # for static_arp.cfg file
154 fields['as_sgi_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'as_sgi_ip')
155 fields['as_sgi_mac'] = self.get_mac_address('sgi_network', VENBServiceInstance, 'as_sgi_mac')
156 fields['enb_s1u_ip'] = self.get_ip_address('s1u_network', VENBServiceInstance, 'enb_s1u_ip')
157 fields['enb_s1u_mac'] = self.get_mac_address('s1u_network', VENBServiceInstance, 'enb_s1u_mac')
158
Woojoong Kimf002aec2017-10-25 14:43:37 -0700159 return fields
160
Woojoong Kim04d12162017-11-27 10:59:08 -0800161 def get_values_for_emulator_scenario_wo_sdncontroller(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -0700162 fields = {}
Woojoong Kim04d12162017-11-27 10:59:08 -0800163 fields['scenario'] = "emulator_scenario_without_sdncontroller"
Woojoong Kimf002aec2017-10-25 14:43:37 -0700164 # for interface.cfg file
165 fields['zmq_sub_ip'] = "127.0.0.1"
166 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500167 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
168 'spgw_network', o, 'dp_comm_ip')
169 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
170 'spgw_network', "VSPGWCTenant", 'cp_comm_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700171 fields['fpc_ip'] = "127.0.0.1"
172 fields['cp_nb_server_ip'] = "127.0.0.1"
173
174 # for cp_config.cfg file
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500175 fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance(
176 's1u_network', o, 's1u_ip')
177 fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance(
178 'sgi_network', o, 'sgi_ip')
Woojoong Kimf002aec2017-10-25 14:43:37 -0700179
Woojoong Kim04d12162017-11-27 10:59:08 -0800180 # for static_arp.cfg file
181 fields['as_sgi_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'as_sgi_ip')
182 fields['as_sgi_mac'] = self.get_mac_address('sgi_network', VENBServiceInstance, 'as_sgi_mac')
183 fields['enb_s1u_ip'] = self.get_ip_address('s1u_network', VENBServiceInstance, 'enb_s1u_ip')
184 fields['enb_s1u_mac'] = self.get_mac_address('s1u_network', VENBServiceInstance, 'enb_s1u_mac')
185
Woojoong Kimf002aec2017-10-25 14:43:37 -0700186 return fields
187
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500188 def has_instance(self, sitype, o):
189 i = self.get_peer_serviceinstance_of_type(sitype, o)
190 if not i:
191 self.log.info("Missing in ServiceInstance graph",
192 serviceinstance=sitype)
Woojoong Kimf002aec2017-10-25 14:43:37 -0700193 return False
194
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500195 return i.instance_id
Woojoong Kimf002aec2017-10-25 14:43:37 -0700196
197 # Which scenario does it use among Spirent or NG4T?
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500198 def get_scenario(self, o):
Woojoong Kimf002aec2017-10-25 14:43:37 -0700199 # try get vENB instance: one of both Spirent and NG4T
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500200 venb_flag = self.has_instance("VENBServiceInstance", o)
201 vmme_flag = self.has_instance("VMMETenant", o)
202 sdncontroller_flag = self.has_instance(
203 "SDNControllerServiceInstance", o)
204 vspgwu_flag = self.has_instance("VSPGWUTenant", o)
205 internetemulator_flag = self.has_instance(
206 "SDNControllerServiceInstance", o)
Woojoong Kimf002aec2017-10-25 14:43:37 -0700207
Woojoong Kim04d12162017-11-27 10:59:08 -0800208 # wait until vspgwu and env are comming up
209 if (not vspgwu_flag):
210 self.defer_sync("Waiting for vSPGWU to come up")
211
212 if (not venb_flag):
213 self.defer_sync("Waiting for vENB to come up")
214
Woojoong Kimf002aec2017-10-25 14:43:37 -0700215 if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
Woojoong Kim04d12162017-11-27 10:59:08 -0800216 return 'normal_scenario'
Woojoong Kimf002aec2017-10-25 14:43:37 -0700217
218 if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag:
Woojoong Kim04d12162017-11-27 10:59:08 -0800219 return 'normal_scenario_without_sdncontroller'
Woojoong Kimf002aec2017-10-25 14:43:37 -0700220
221 if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
Woojoong Kim04d12162017-11-27 10:59:08 -0800222 return 'emulator_scenario'
Woojoong Kimf002aec2017-10-25 14:43:37 -0700223
Woojoong Kim04d12162017-11-27 10:59:08 -0800224 if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (not internetemulator_flag):
225 return 'emulator_scenario_without_sdncontroller'
Woojoong Kimf002aec2017-10-25 14:43:37 -0700226
227 return 'manual'
228
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500229 def get_peer_serviceinstance_of_type(self, sitype, o):
230 prov_link_set = ServiceInstanceLink.objects.filter(
231 subscriber_service_instance_id=o.id)
Woojoong Kimf002aec2017-10-25 14:43:37 -0700232
233 try:
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500234 peer_service = next(
235 p for p in prov_link_set if p.leaf_model_name == sitype)
236 except StopIteration:
237 sub_link_set = ServiceInstanceLink.objects.filter(
238 provider_service_instance_id=o.id)
239 try:
240 peer_service = next(
241 s for s in sub_link_set if s.leaf_model_name == sitype)
242 except StopIteration:
243 self.log.error(
244 'Could not find service type in service graph', service_type=sitype, object=o)
245 raise Exception(
246 "Synchronization failed due to incomplete service graph")
247
248 return peer_service
249
250 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
251 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
252 return self.get_ip_address_for_peer_service_instance_instance(network_name, peer_si, parameter)
253
254 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, parameter=None):
255 try:
Woojoong Kimf002aec2017-10-25 14:43:37 -0700256 net_id = self.get_network_id(network_name)
Sapan Bhatia968f28f2017-11-22 12:00:01 -0500257 ins_id = peer_si.instance_id
258 ip_address = Port.objects.get(
259 network_id=net_id, instance_id=ins_id).ip
Woojoong Kimf002aec2017-10-25 14:43:37 -0700260 except Exception:
Woojoong Kim04d12162017-11-27 10:59:08 -0800261 ip_address = "error"
262 self.log.error("Failed to fetch parameter to get IP", parameter = parameter, network_name = network_name)
Sapan Bhatiaa71f2212017-11-22 10:12:18 -0500263 self.defer_sync("Waiting for parameters to become available")
Woojoong Kimf002aec2017-10-25 14:43:37 -0700264
265 return ip_address
266
Woojoong Kim04d12162017-11-27 10:59:08 -0800267 def get_mac_address(self, network_name, service_instance, parameter):
268
269 try:
270 net_id = self.get_network_id(network_name)
271 ins_id = self.get_instance_id(service_instance)
272 mac_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
273
274 except Exception:
275 mac_address = "error"
276 self.log.error("Failed to fetch parameter to get MAC", parameter = parameter, network_name = network_name)
277 self.defer_sync("Waiting for parameters to become available")
278
279 return mac_address
280
Woojoong Kimf002aec2017-10-25 14:43:37 -0700281 # To get each network id
282 def get_network_id(self, network_name):
283 return Network.objects.get(name=network_name).id