JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 1 | # 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 | |
| 15 | import os |
| 16 | import sys |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 17 | import time |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 18 | from django.db.models import Q, F |
| 19 | from synchronizers.new_base.modelaccessor import * |
| 20 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 21 | |
| 22 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 23 | sys.path.insert(0, parentdir) |
| 24 | |
| 25 | class SyncVSPGWCTenant(SyncInstanceUsingAnsible): |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 26 | observes = VSPGWCTenant |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 27 | template_name = "vspgwctenant_playbook.yaml" |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 28 | 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 Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 33 | def get_extra_attributes(self, o): |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 34 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 35 | scenario = self.get_scenario() |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 36 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 37 | 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 Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 45 | else: |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 46 | return self.get_extra_attributes_for_manual() |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 47 | |
| 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 65 | # for rules setup in ONOS |
| 66 | fields['sgi_as_ip'] = "manual" |
| 67 | fields['sgi_spgwu_ip'] = "manual" |
| 68 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 69 | return fields |
| 70 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 71 | def get_values_for_normal_scenario(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 72 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 73 | fields['scenario'] = "normal_scenario" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 74 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 87 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 91 | return fields |
| 92 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 93 | def get_values_for_normal_scenario_wo_sdncontroller(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 94 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 95 | fields['scenario'] = "normal_scenario_without_sdncontroller" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 96 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 109 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 113 | return fields |
| 114 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 115 | def get_values_for_emulator_scenario(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 116 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 117 | fields['scenario'] = "emulator_scenario" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 118 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 131 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 135 | return fields |
| 136 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 137 | def get_values_for_emulator_scenario_wo_sdncontroller(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 138 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 139 | fields['scenario'] = "emulator_scenario_without_sdncontroller" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 140 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 153 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 157 | return fields |
| 158 | |
| 159 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 160 | def has_venb(self): |
| 161 | # try get vMME instance |
| 162 | try: |
| 163 | instance_id = self.get_instance_id(VENBServiceInstance) |
| 164 | except Exception: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame] | 165 | self.log.debug('VENBServiceInstance not found') |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 166 | 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 Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame] | 175 | self.log.debug('VMMETenant not found') |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 176 | return False |
| 177 | |
| 178 | return True |
| 179 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 180 | def has_sdncontroller(self): |
| 181 | # try get vMME instance |
| 182 | try: |
| 183 | instance_id = self.get_instance_id(SDNControllerServiceInstance) |
| 184 | except Exception: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame] | 185 | self.log.debug('SDNControllerServiceInstance not found') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 186 | 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 Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame] | 195 | self.log.debug('VSPGWUTenant instance not found') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 196 | 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 Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame] | 205 | self.log.debug('InternetEmulatorServiceInstance not found') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 206 | return False |
| 207 | |
| 208 | return True |
| 209 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 210 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 211 | 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 215 | sdncontroller_flag = self.has_sdncontroller() |
| 216 | vspgwu_flag = self.has_vspgwu() |
| 217 | internetemulator_flag = self.has_internetemulator() |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 218 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 219 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 230 | if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag: |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 231 | return 'normal_scenario' |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 232 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 233 | if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag: |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 234 | return 'normal_scenario_without_sdncontroller' |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 235 | |
| 236 | if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag): |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 237 | return 'emulator_scenario' |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 238 | |
| 239 | if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (not internetemulator_flag): |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 240 | return 'emulator_scenario_without_sdncontroller' |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 241 | |
| 242 | return 'manual' |
| 243 | |
| 244 | # To get IP address |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 245 | def get_ip_address(self, network_name, service_instance, parameter): |
| 246 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 247 | condition = False |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 248 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 249 | 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 Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame] | 257 | self.log.error('Could not fetch parameter', parameter = parameter, network_name = network_name) |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 258 | |
| 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 |