blob: 0c90978856abfa5f80f2d28d09bd8efab13b6b6d [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
17from django.db.models import Q, F
18from 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
24class SyncVSPGWCTenant(SyncInstanceUsingAnsible):
25 provides = [VSPGWCTenant]
26
27 observes = VSPGWCTenant
28
29 requested_interval = 0
30
31 template_name = "vspgwctenant_playbook.yaml"
32
33 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
34
35 def __init__(self, *args, **kwargs):
36 super(SyncVSPGWCTenant, self).__init__(*args, **kwargs)
37
Woojoong Kimf000eda2017-10-20 15:00:43 -070038 def get_extra_attributes(self, o):
JianHao4a9550f2017-10-19 11:05:14 +080039
Woojoong Kim1a1e3702017-10-27 13:26:34 -070040 scenario = self.get_scenario()
JianHao4a9550f2017-10-19 11:05:14 +080041
Woojoong Kim1a1e3702017-10-27 13:26:34 -070042 if scenario == 'ng4t_with_sdncontroller':
43 return self.get_values_for_ng4t_w_sdncontroller()
44 elif scenario == 'ng4t_without_sdncontroller':
45 return self.get_values_for_ng4t_wo_sdncontroller()
46 elif scenario == 'spirent_with_sdncontroller':
47 return self.get_values_for_spirent_w_sdncontroller()
48 elif scenario == 'spirent_without_sdncontroller':
49 return self.get_values_for_spirent_wo_sdncontroller()
Woojoong Kimf000eda2017-10-20 15:00:43 -070050 else:
Woojoong Kim1a1e3702017-10-27 13:26:34 -070051 return self.get_extra_attributes_for_manual()
Woojoong Kimf000eda2017-10-20 15:00:43 -070052
53 # fields for manual case
54 def get_extra_attributes_for_manual(self):
55 fields = {}
56 fields['scenario'] = self.get_scenario()
57 # for interface.cfg file
58 fields['zmq_sub_ip'] = "manual"
59 fields['zmq_pub_ip'] = "manual"
60 fields['dp_comm_ip'] = "manual"
61 fields['cp_comm_ip'] = "manual"
62 fields['fpc_ip'] = "manual"
63 fields['cp_nb_server_ip'] = "manual"
64
65 # for cp_config.cfg file
66 fields['s11_sgw_ip'] = "manual"
67 fields['s11_mme_ip'] = "manual"
68 fields['s1u_sgw_ip'] = "manual"
69
70 return fields
71
Woojoong Kim1a1e3702017-10-27 13:26:34 -070072 def get_values_for_ng4t_w_sdncontroller(self):
73 fields = {}
74 fields['scenario'] = "ng4t_with_sdncontroller"
75 # for interface.cfg file
76 fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
77 fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
78 fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
79 fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
80 fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
81 fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
82
83 # for cp_config.cfg file
84 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
85 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
86 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
87
88 return fields
89
90 def get_values_for_ng4t_wo_sdncontroller(self):
91 fields = {}
92 fields['scenario'] = "ng4t_without_sdncontroller"
93 # for interface.cfg file
94 fields['zmq_sub_ip'] = "127.0.0.1"
95 fields['zmq_pub_ip'] = "127.0.0.1"
96 fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
97 fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
98 fields['fpc_ip'] = "127.0.0.1"
99 fields['cp_nb_server_ip'] = "127.0.0.1"
100
101 # for cp_config.cfg file
102 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
103 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
104 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
105
106 return fields
107
108 def get_values_for_spirent_w_sdncontroller(self):
109 fields = {}
110 fields['scenario'] = "spirent_with_sdncontroller"
111 # for interface.cfg file
112 fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
113 fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
114 fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
115 fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
116 fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
117 fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
118
119 # for cp_config.cfg file
120 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
121 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
122 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
123
124 return fields
125
126 def get_values_for_spirent_wo_sdncontroller(self):
127 fields = {}
128 fields['scenario'] = "spirent_without_sdncontroller"
129 # for interface.cfg file
130 fields['zmq_sub_ip'] = "127.0.0.1"
131 fields['zmq_pub_ip'] = "127.0.0.1"
132 fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
133 fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
134 fields['fpc_ip'] = "127.0.0.1"
135 fields['cp_nb_server_ip'] = "127.0.0.1"
136
137 # for cp_config.cfg file
138 fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip')
139 fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
140 fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
141
142 return fields
143
144
Woojoong Kimf000eda2017-10-20 15:00:43 -0700145 def has_venb(self):
146 # try get vMME instance
147 try:
148 instance_id = self.get_instance_id(VENBServiceInstance)
149 except Exception:
150 print 'cannot get VENBServiceInstance'
151 return False
152
153 return True
154
155 def has_vmme(self):
156 # try get vMME instance
157 try:
158 instance_id = self.get_instance_id(VMMETenant)
159 except Exception:
160 print 'cannot get VMMETenant'
161 return False
162
163 return True
164
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700165 def has_sdncontroller(self):
166 # try get vMME instance
167 try:
168 instance_id = self.get_instance_id(SDNControllerServiceInstance)
169 except Exception:
170 print 'cannot get SDNControllerServiceInstance'
171 return False
172
173 return True
174
175 def has_vspgwu(self):
176 # try get vMME instance
177 try:
178 instance_id = self.get_instance_id(VSPGWUTenant)
179 except Exception:
180 print 'cannot get VSPGWUTenant'
181 return False
182
183 return True
184
185 def has_internetemulator(self):
186 # try get vMME instance
187 try:
188 instance_id = self.get_instance_id(InternetEmulatorServiceInstance)
189 except Exception:
190 print 'cannot get InternetEmulatorServiceInstance'
191 return False
192
193 return True
194
Woojoong Kimf000eda2017-10-20 15:00:43 -0700195
196 # Which scenario does it use among Spirent or NG4T?
197 def get_scenario(self):
198 # try get vENB instance: one of both Spirent and NG4T
199 venb_flag = self.has_venb()
200 vmme_flag = self.has_vmme()
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700201 sdncontroller_flag = self.has_sdncontroller()
202 vspgwu_flag = self.has_vspgwu()
203 internetemulator_flag = self.has_internetemulator()
Woojoong Kimf000eda2017-10-20 15:00:43 -0700204
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700205 if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
206 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:
209 return 'ng4t_without_sdncontroller'
210
211 if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
212 return 'spirent_with_sdncontroller'
213
214 if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (not internetemulator_flag):
215 return 'spirent_without_sdncontroller'
216
217 return 'manual'
218
219 # To get IP address
Woojoong Kimf000eda2017-10-20 15:00:43 -0700220 def get_ip_address(self, network_name, service_instance, parameter):
221
Woojoong Kimf000eda2017-10-20 15:00:43 -0700222 try:
223 net_id = self.get_network_id(network_name)
224 ins_id = self.get_instance_id(service_instance)
225 ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
226
227 except Exception:
228 ip_address = "error"
229 print "get failed -- %s" % (parameter)
230
231 return ip_address
232
233 # To get each network id
234 def get_network_id(self, network_name):
235 return Network.objects.get(name=network_name).id
236
237 # To get service_instance (assumption: there is a single instance for each service)
238 def get_instance_id(self, serviceinstance):
239 instances = serviceinstance.objects.all()
240 instance_id = instances[0].instance_id
241 return instance_id