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 | |
Sapan Bhatia | 28b7560 | 2017-11-28 14:18:21 -0500 | [diff] [blame] | 25 | class ServiceGraphException(Exception): |
| 26 | pass |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 27 | |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 28 | class SyncVSPGWCTenant(SyncInstanceUsingAnsible): |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 29 | observes = VSPGWCTenant |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 30 | template_name = "vspgwctenant_playbook.yaml" |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 31 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 32 | |
| 33 | def __init__(self, *args, **kwargs): |
| 34 | super(SyncVSPGWCTenant, self).__init__(*args, **kwargs) |
| 35 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 36 | def get_extra_attributes(self, o): |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 37 | |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 38 | scenario = self.get_scenario(o) |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 39 | |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 40 | if scenario == 'cord_4_1_scenario': |
| 41 | return self.get_values_for_CORD_4_1(o) |
| 42 | elif scenario == 'cord_5_0_scenario': |
| 43 | return self.get_values_for_CORD_5_0(o) |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 44 | else: |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 45 | return self.get_extra_attributes_for_manual(o) |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 46 | |
| 47 | # fields for manual case |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 48 | def get_extra_attributes_for_manual(self, o): |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 49 | fields = {} |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 50 | fields['scenario'] = "manual" |
| 51 | fields['cord_version'] = "manual" |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 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 | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 71 | def get_values_for_CORD_4_1(self, o): |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 72 | fields = {} |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 73 | fields['cord_version'] = "4.1" |
| 74 | fields['scenario'] = "cord_4_1_scenario" |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 75 | # for interface.cfg file |
| 76 | fields['zmq_sub_ip'] = "127.0.0.1" |
| 77 | fields['zmq_pub_ip'] = "127.0.0.1" |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 78 | fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 79 | 'spgw_network', "VSPGWUTenant", o, 'dp_comm_ip') |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 80 | fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 81 | 'spgw_network', o, o, 'cp_comm_ip') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 82 | fields['fpc_ip'] = "127.0.0.1" |
| 83 | fields['cp_nb_server_ip'] = "127.0.0.1" |
| 84 | |
| 85 | # for cp_config.cfg file |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 86 | fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 87 | 's11_network', o, o, 's11_sgw_ip') |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 88 | fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 89 | 's1u_network', "VSPGWUTenant", o, 's1u_sgw_ip') |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 90 | fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 91 | 's11_network', "VENBServiceInstance", o, 's11_mme_ip') |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 92 | |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 93 | # for rules setup in ONOS |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 94 | fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 95 | 'sgi_network', "VENBServiceInstance", o, 'sgi_as_ip') |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 96 | fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance( |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 97 | 'sgi_network', "VSPGWUTenant", o, 'sgi_spgwu_ip') |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 98 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 99 | return fields |
| 100 | |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 101 | def get_values_for_CORD_5_0(self, o): |
Woojoong Kim | 313d35c | 2017-12-18 17:15:46 -0800 | [diff] [blame] | 102 | fields = {} |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 103 | fields['cord_version'] = "5.0" |
| 104 | fields['scenario'] = "cord_5_0_scenario" |
Woojoong Kim | 313d35c | 2017-12-18 17:15:46 -0800 | [diff] [blame] | 105 | |
| 106 | # for interface.cfg file |
| 107 | fields['zmq_sub_ip'] = "127.0.0.1" |
| 108 | fields['zmq_pub_ip'] = "127.0.0.1" |
| 109 | fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance( |
| 110 | 'spgw_network', "VSPGWUTenant", o, 'dp_comm_ip') |
| 111 | fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
| 112 | 'spgw_network', o, o, 'cp_comm_ip') |
| 113 | fields['fpc_ip'] = "127.0.0.1" |
| 114 | fields['cp_nb_server_ip'] = "127.0.0.1" |
| 115 | |
| 116 | # for cp_config.cfg file |
| 117 | fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
| 118 | 's11_network', o, o, 's11_sgw_ip') |
| 119 | fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance( |
Woojoong Kim | c96c24f | 2018-01-22 17:37:05 -0800 | [diff] [blame^] | 120 | 'flat_network_s1u', "VSPGWUTenant", o, 's1u_sgw_ip') |
Woojoong Kim | 313d35c | 2017-12-18 17:15:46 -0800 | [diff] [blame] | 121 | fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance( |
| 122 | 's11_network', "VMMETenant", o, 's11_mme_ip') |
| 123 | |
| 124 | # for rules setup in ONOS |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 125 | internetemulator_flag = self.has_instance("InternetEmulatorServiceInstance", o) |
| 126 | if (internetemulator_flag): |
| 127 | fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance( |
| 128 | 'sgi_network', "InternetEmulatorServiceInstance", o, 'sgi_as_ip') |
| 129 | else: |
| 130 | fields['sgi_as_ip'] = o.appserver_ip_addr |
Woojoong Kim | 313d35c | 2017-12-18 17:15:46 -0800 | [diff] [blame] | 131 | fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance( |
| 132 | 'sgi_network', "VSPGWUTenant", o, 'sgi_spgwu_ip') |
| 133 | |
| 134 | return fields |
| 135 | |
| 136 | |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 137 | def get_scenario(self, o): |
| 138 | venb_flag = self.has_instance("VENBServiceInstance", o) |
| 139 | vmme_flag = self.has_instance("VMMETenant", o) |
| 140 | sdncontroller_flag = self.has_instance( |
| 141 | "SDNControllerServiceInstance", o) |
| 142 | vspgwu_flag = self.has_instance("VSPGWUTenant", o) |
| 143 | internetemulator_flag = self.has_instance( |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 144 | "InternetEmulatorServiceInstance", o) |
| 145 | vhss_flag = self.has_instance("VHSSTenant", o) |
| 146 | hssdb_flag = self.has_instance("HSSDBServiceInstance", o) |
Woojoong Kim | 4f09cbf | 2017-11-19 21:25:37 -0800 | [diff] [blame] | 147 | |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 148 | if (o.blueprint == "build") or (o.blueprint == "MCORD 4.1"): |
| 149 | if not venb_flag: |
| 150 | self.defer_sync(o, "Waiting for eNB image to become available") |
| 151 | if not vspgwu_flag: |
| 152 | self.defer_sync(o, "Waiting for SPGWU image to become available") |
| 153 | return 'cord_4_1_scenario' |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 154 | |
Woojoong Kim | 103fba0 | 2018-01-18 20:01:06 -0800 | [diff] [blame] | 155 | if (o.blueprint == "mcord_5") or (o.blueprint == "MCORD 5"): |
| 156 | if not hssdb_flag: |
| 157 | self.defer_sync(o, "Waiting for HSS_DB image to become available") |
| 158 | if not vhss_flag: |
| 159 | self.defer_sync(o, "Waiting for vHSS image to become available") |
| 160 | if not vmme_flag: |
| 161 | self.defer_sync(o, "Waiting for vMME image to become available") |
| 162 | if not vspgwu_flag: |
| 163 | self.defer_sync(o, "Waiting for vSPGWU image to become available") |
| 164 | return 'cord_5_0_scenario' |
Woojoong Kim | 313d35c | 2017-12-18 17:15:46 -0800 | [diff] [blame] | 165 | |
Woojoong Kim | 1a1e370 | 2017-10-27 13:26:34 -0700 | [diff] [blame] | 166 | return 'manual' |
| 167 | |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 168 | def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None): |
| 169 | peer_si = self.get_peer_serviceinstance_of_type(sitype, o) |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 170 | return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter) |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 171 | |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 172 | def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None): |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 173 | try: |
| 174 | net_id = self.get_network_id(network_name) |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 175 | ins_id = peer_si.leaf_model.instance_id |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 176 | ip_address = Port.objects.get( |
| 177 | network_id=net_id, instance_id=ins_id).ip |
| 178 | except Exception: |
| 179 | self.log.error("Failed to fetch parameter", |
| 180 | parameter=parameter, |
| 181 | network_name=network_name) |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 182 | self.defer_sync(o, "Waiting for parameters to become available") |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 183 | |
| 184 | return ip_address |
| 185 | |
| 186 | def get_peer_serviceinstance_of_type(self, sitype, o): |
Woojoong Kim | c96c24f | 2018-01-22 17:37:05 -0800 | [diff] [blame^] | 187 | |
| 188 | global_list = self.get_all_instances_in_graph(o) |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 189 | |
| 190 | try: |
Woojoong Kim | c96c24f | 2018-01-22 17:37:05 -0800 | [diff] [blame^] | 191 | peer_service = next(p for p in global_list if p.leaf_model_name == sitype) |
Sapan Bhatia | 9753629 | 2017-11-27 21:35:34 -0500 | [diff] [blame] | 192 | |
Woojoong Kim | c96c24f | 2018-01-22 17:37:05 -0800 | [diff] [blame^] | 193 | except StopIteration: |
| 194 | self.log.error( |
| 195 | 'Could not find service type in service graph', service_type=sitype, object=o) |
| 196 | raise ServiceGraphException( |
| 197 | "Synchronization failed due to incomplete service graph") |
| 198 | |
| 199 | return peer_service |
| 200 | |
| 201 | def has_instance_in_list(self, list, o): |
| 202 | for instance in list: |
| 203 | if instance.leaf_model_name == o.leaf_model_name: |
| 204 | return True |
| 205 | |
| 206 | return False |
| 207 | |
| 208 | def get_all_instances_in_graph(self, o): |
| 209 | |
| 210 | to_search_list = self.get_one_hop_instances_in_graph(o) |
| 211 | result_list = [] |
| 212 | |
| 213 | while len(to_search_list) > 0: |
| 214 | tmp_obj = to_search_list[0] |
| 215 | to_search_list.remove(tmp_obj) |
| 216 | tmp_list = self.get_one_hop_instances_in_graph(tmp_obj) |
| 217 | |
| 218 | for index_obj in tmp_list: |
| 219 | if (not self.has_instance_in_list(to_search_list, index_obj)) and ( |
| 220 | not self.has_instance_in_list(result_list, index_obj)): |
| 221 | to_search_list.append(index_obj) |
| 222 | |
| 223 | result_list.append(tmp_obj) |
| 224 | return result_list |
| 225 | |
| 226 | def get_one_hop_instances_in_graph(self, o): |
| 227 | instance_list = [] |
| 228 | |
| 229 | # 1 hop forward and backward |
| 230 | prov_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id=o.id) |
| 231 | subs_links = ServiceInstanceLink.objects.filter(provider_service_instance_id=o.id) |
| 232 | |
| 233 | # add instances located in 1 hop into instance_list |
| 234 | for tmp_link1 in prov_links: |
| 235 | if not self.has_instance_in_list(instance_list, tmp_link1.provider_service_instance): |
| 236 | instance_list.append(tmp_link1.provider_service_instance) |
| 237 | |
| 238 | for tmp_link1 in subs_links: |
| 239 | if not self.has_instance_in_list(instance_list, tmp_link1.subscriber_service_instance): |
| 240 | instance_list.append(tmp_link1.subscriber_service_instance) |
| 241 | |
| 242 | return instance_list |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 243 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame] | 244 | # To get each network id |
| 245 | def get_network_id(self, network_name): |
| 246 | return Network.objects.get(name=network_name).id |
| 247 | |
| 248 | # To get service_instance (assumption: there is a single instance for each service) |
| 249 | def get_instance_id(self, serviceinstance): |
| 250 | instances = serviceinstance.objects.all() |
| 251 | instance_id = instances[0].instance_id |
| 252 | return instance_id |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 253 | |
| 254 | def has_instance(self, sitype, o): |
Sapan Bhatia | 28b7560 | 2017-11-28 14:18:21 -0500 | [diff] [blame] | 255 | try: |
| 256 | i = self.get_peer_serviceinstance_of_type(sitype, o) |
| 257 | except ServiceGraphException: |
Sapan Bhatia | a1fbd38 | 2017-11-22 13:18:11 -0500 | [diff] [blame] | 258 | self.log.info("Missing in ServiceInstance graph", |
| 259 | serviceinstance=sitype) |
| 260 | return False |
| 261 | |
Woojoong Kim | 38d8488 | 2017-11-28 15:38:39 -0800 | [diff] [blame] | 262 | return i.leaf_model.instance_id |