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): |
| 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 Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 39 | def get_extra_attributes(self, o): |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 40 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 41 | scenario = self.get_scenario() |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 42 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 43 | 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 Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 51 | else: |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 52 | return self.get_extra_attributes_for_manual() |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 53 | |
| 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 71 | # for rules setup in ONOS |
| 72 | fields['sgi_as_ip'] = "manual" |
| 73 | fields['sgi_spgwu_ip'] = "manual" |
| 74 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 75 | return fields |
| 76 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 77 | def get_values_for_normal_scenario(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 78 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 79 | fields['scenario'] = "normal_scenario" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 80 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 93 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 97 | return fields |
| 98 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 99 | def get_values_for_normal_scenario_wo_sdncontroller(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 100 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 101 | fields['scenario'] = "normal_scenario_without_sdncontroller" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 102 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 115 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 119 | return fields |
| 120 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 121 | def get_values_for_emulator_scenario(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 122 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 123 | fields['scenario'] = "emulator_scenario" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 124 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 137 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 141 | return fields |
| 142 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 143 | def get_values_for_emulator_scenario_wo_sdncontroller(self): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 144 | fields = {} |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 145 | fields['scenario'] = "emulator_scenario_without_sdncontroller" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 146 | # 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 Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 159 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 163 | return fields |
| 164 | |
| 165 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 166 | def has_venb(self): |
| 167 | # try get vMME instance |
| 168 | try: |
| 169 | instance_id = self.get_instance_id(VENBServiceInstance) |
| 170 | except Exception: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame^] | 171 | self.log.debug('VENBServiceInstance not found') |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 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: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame^] | 181 | self.log.debug('VMMETenant not found') |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 182 | return False |
| 183 | |
| 184 | return True |
| 185 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 186 | def has_sdncontroller(self): |
| 187 | # try get vMME instance |
| 188 | try: |
| 189 | instance_id = self.get_instance_id(SDNControllerServiceInstance) |
| 190 | except Exception: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame^] | 191 | self.log.debug('SDNControllerServiceInstance not found') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 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: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame^] | 201 | self.log.debug('VSPGWUTenant instance not found') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 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: |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame^] | 211 | self.log.debug('InternetEmulatorServiceInstance not found') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 212 | return False |
| 213 | |
| 214 | return True |
| 215 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 216 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 217 | 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 221 | sdncontroller_flag = self.has_sdncontroller() |
| 222 | vspgwu_flag = self.has_vspgwu() |
| 223 | internetemulator_flag = self.has_internetemulator() |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 224 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 225 | # 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 Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 236 | 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] | 237 | return 'normal_scenario' |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 238 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 239 | 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] | 240 | return 'normal_scenario_without_sdncontroller' |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 241 | |
| 242 | 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] | 243 | return 'emulator_scenario' |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 244 | |
| 245 | 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] | 246 | return 'emulator_scenario_without_sdncontroller' |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 247 | |
| 248 | return 'manual' |
| 249 | |
| 250 | # To get IP address |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 251 | def get_ip_address(self, network_name, service_instance, parameter): |
| 252 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 253 | condition = False |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 254 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 255 | 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" |
Sapan Bhatia | 7d2e708 | 2017-11-22 10:08:11 -0500 | [diff] [blame^] | 263 | 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] | 264 | |
| 265 | return ip_address |
| 266 | |
| 267 | # To get each network id |
| 268 | def get_network_id(self, network_name): |
| 269 | return Network.objects.get(name=network_name).id |
| 270 | |
| 271 | # To get service_instance (assumption: there is a single instance for each service) |
| 272 | def get_instance_id(self, serviceinstance): |
| 273 | instances = serviceinstance.objects.all() |
| 274 | instance_id = instances[0].instance_id |
| 275 | return instance_id |