Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +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 |
| 17 | from django.db.models import Q, F |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 18 | from synchronizers.new_base.modelaccessor import * |
| 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 20 | |
| 21 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 22 | sys.path.insert(0, parentdir) |
| 23 | |
| 24 | class SyncVSPGWUTenant(SyncInstanceUsingAnsible): |
| 25 | |
| 26 | provides = [VSPGWUTenant] |
| 27 | |
| 28 | observes = VSPGWUTenant |
| 29 | |
| 30 | requested_interval = 0 |
| 31 | |
| 32 | template_name = "vspgwutenant_playbook.yaml" |
| 33 | |
| 34 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 35 | |
| 36 | def __init__(self, *args, **kwargs): |
| 37 | super(SyncVSPGWUTenant, self).__init__(*args, **kwargs) |
| 38 | |
| 39 | |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 40 | def get_extra_attributes(self, o): |
| 41 | |
| 42 | scenario = self.get_scenario() |
| 43 | |
| 44 | if scenario == 'ng4t_with_sdncontroller': |
| 45 | return self.get_values_for_ng4t_w_sdncontroller() |
| 46 | elif scenario == 'ng4t_without_sdncontroller': |
| 47 | return self.get_values_for_ng4t_wo_sdncontroller() |
| 48 | elif scenario == 'spirent_with_sdncontroller': |
| 49 | return self.get_values_for_spirent_w_sdncontroller() |
| 50 | elif scenario == 'spirent_without_sdncontroller': |
| 51 | return self.get_values_for_spirent_wo_sdncontroller() |
| 52 | else: |
| 53 | return self.get_extra_attributes_for_manual() |
| 54 | |
| 55 | # fields for manual case |
| 56 | def get_extra_attributes_for_manual(self): |
| 57 | fields = {} |
| 58 | fields['scenario'] = self.get_scenario() |
| 59 | # for interface.cfg file |
| 60 | fields['zmq_sub_ip'] = "manual" |
| 61 | fields['zmq_pub_ip'] = "manual" |
| 62 | fields['dp_comm_ip'] = "manual" |
| 63 | fields['cp_comm_ip'] = "manual" |
| 64 | fields['fpc_ip'] = "manual" |
| 65 | fields['cp_nb_server_ip'] = "manual" |
| 66 | |
| 67 | # for dp_config.cfg file |
| 68 | fields['s1u_ip'] = "manual" |
| 69 | fields['sgi_ip'] = "manual" |
| 70 | |
| 71 | return fields |
| 72 | |
| 73 | def get_values_for_ng4t_w_sdncontroller(self): |
| 74 | fields = {} |
| 75 | fields['scenario'] = "ng4t_with_sdncontroller" |
| 76 | # for interface.cfg file |
| 77 | fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip') |
| 78 | fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip') |
| 79 | fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip') |
| 80 | fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip') |
| 81 | fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip') |
| 82 | fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip') |
| 83 | |
| 84 | # for dp_config.cfg file |
| 85 | fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip') |
| 86 | fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_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['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip') |
| 103 | fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip') |
| 104 | |
| 105 | return fields |
| 106 | |
| 107 | def get_values_for_spirent_w_sdncontroller(self): |
| 108 | fields = {} |
| 109 | fields['scenario'] = "ng4t_with_sdncontroller" |
| 110 | # for interface.cfg file |
| 111 | fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip') |
| 112 | fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip') |
| 113 | fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip') |
| 114 | fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip') |
| 115 | fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip') |
| 116 | fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip') |
| 117 | |
| 118 | # for dp_config.cfg file |
| 119 | fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip') |
| 120 | fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip') |
| 121 | |
| 122 | return fields |
| 123 | |
| 124 | def get_values_for_spirent_wo_sdncontroller(self): |
| 125 | fields = {} |
| 126 | fields['scenario'] = "ng4t_without_sdncontroller" |
| 127 | # for interface.cfg file |
| 128 | fields['zmq_sub_ip'] = "127.0.0.1" |
| 129 | fields['zmq_pub_ip'] = "127.0.0.1" |
| 130 | fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip') |
| 131 | fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip') |
| 132 | fields['fpc_ip'] = "127.0.0.1" |
| 133 | fields['cp_nb_server_ip'] = "127.0.0.1" |
| 134 | |
| 135 | # for cp_config.cfg file |
| 136 | fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip') |
| 137 | fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip') |
| 138 | |
| 139 | return fields |
| 140 | |
| 141 | def has_venb(self): |
| 142 | # try get vMME instance |
| 143 | try: |
| 144 | instance_id = self.get_instance_id(VENBServiceInstance) |
| 145 | except Exception: |
| 146 | print 'cannot get VENBServiceInstance' |
| 147 | return False |
| 148 | |
| 149 | return True |
| 150 | |
| 151 | def has_vmme(self): |
| 152 | # try get vMME instance |
| 153 | try: |
| 154 | instance_id = self.get_instance_id(VMMETenant) |
| 155 | except Exception: |
| 156 | print 'cannot get VMMETenant' |
| 157 | return False |
| 158 | |
| 159 | return True |
| 160 | |
| 161 | def has_sdncontroller(self): |
| 162 | # try get vMME instance |
| 163 | try: |
| 164 | instance_id = self.get_instance_id(SDNControllerServiceInstance) |
| 165 | except Exception: |
| 166 | print 'cannot get SDNControllerServiceInstance' |
| 167 | return False |
| 168 | |
| 169 | return True |
| 170 | |
| 171 | def has_vspgwu(self): |
| 172 | # try get vMME instance |
| 173 | try: |
| 174 | instance_id = self.get_instance_id(VSPGWUTenant) |
| 175 | except Exception: |
| 176 | print 'cannot get VSPGWUTenant' |
| 177 | return False |
| 178 | |
| 179 | return True |
| 180 | |
| 181 | def has_internetemulator(self): |
| 182 | # try get vMME instance |
| 183 | try: |
| 184 | instance_id = self.get_instance_id(InternetEmulatorServiceInstance) |
| 185 | except Exception: |
| 186 | print 'cannot get InternetEmulatorServiceInstance' |
| 187 | return False |
| 188 | |
| 189 | return True |
| 190 | |
| 191 | # Which scenario does it use among Spirent or NG4T? |
| 192 | def get_scenario(self): |
| 193 | # try get vENB instance: one of both Spirent and NG4T |
| 194 | venb_flag = self.has_venb() |
| 195 | vmme_flag = self.has_vmme() |
| 196 | sdncontroller_flag = self.has_sdncontroller() |
| 197 | vspgwu_flag = self.has_vspgwu() |
| 198 | internetemulator_flag = self.has_internetemulator() |
| 199 | |
| 200 | if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag: |
| 201 | return 'ng4t_with_sdncontroller' |
| 202 | |
| 203 | if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag: |
| 204 | return 'ng4t_without_sdncontroller' |
| 205 | |
| 206 | if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag): |
| 207 | return 'spirent_with_sdncontroller' |
| 208 | |
| 209 | if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and ( |
| 210 | not internetemulator_flag): |
| 211 | return 'spirent_without_sdncontroller' |
| 212 | |
| 213 | return 'manual' |
| 214 | |
| 215 | def get_ip_address(self, network_name, service_instance, parameter): |
| 216 | |
| 217 | try: |
| 218 | net_id = self.get_network_id(network_name) |
| 219 | ins_id = self.get_instance_id(service_instance) |
| 220 | ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip |
| 221 | |
| 222 | except Exception: |
| 223 | ip_address = "error" |
| 224 | print "get failed -- %s" % (parameter) |
| 225 | |
| 226 | return ip_address |
| 227 | |
| 228 | # To get each network id |
| 229 | def get_network_id(self, network_name): |
| 230 | return Network.objects.get(name=network_name).id |
| 231 | |
| 232 | # To get service_instance (assumption: there is a single instance for each service) |
| 233 | def get_instance_id(self, serviceinstance): |
| 234 | instances = serviceinstance.objects.all() |
| 235 | instance_id = instances[0].instance_id |
| 236 | return instance_id |