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 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 21 | from collections import defaultdict |
| 22 | |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 23 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 24 | sys.path.insert(0, parentdir) |
| 25 | |
Sapan Bhatia | fc9f6b3 | 2017-11-28 14:13:31 -0500 | [diff] [blame] | 26 | class ServiceGraphException(Exception): |
| 27 | pass |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 28 | |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 29 | class SyncVSPGWUTenant(SyncInstanceUsingAnsible): |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 30 | observes = VSPGWUTenant |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 31 | template_name = "vspgwutenant_playbook.yaml" |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 32 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 33 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 34 | """ Convert ServiceInstance graph into an adjacency set""" |
| 35 | def adj_set_of_service_graph(self, o, visited = None, adj_set = None): |
| 36 | def key(o): |
| 37 | return o.leaf_model_name |
| 38 | |
| 39 | if not adj_set: |
| 40 | adj_set = defaultdict(set) |
| 41 | |
| 42 | if not o: |
| 43 | return adj_set |
| 44 | else: |
| 45 | if not visited: |
| 46 | visited = set() |
| 47 | |
| 48 | ko = key(o) |
| 49 | visited.add(ko) |
| 50 | |
| 51 | provider_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id = o.id) |
| 52 | for l in provider_links: |
| 53 | n = l.provider_service_instance |
| 54 | kn = key(n) |
| 55 | adj_set[ko].add(kn) |
| 56 | adj_set[kn].update([]) |
| 57 | if kn not in visited: |
| 58 | adj_set = self.adj_set_of_service_graph(n, visited, adj_set) |
| 59 | |
| 60 | subscriber_links = ServiceInstanceLink.objects.filter(provider_service_instance_id = o.id) |
| 61 | for l in subscriber_links: |
| 62 | n = l.subscriber_service_instance |
| 63 | sn = key(n) |
| 64 | adj_set[sn].add(ko) |
| 65 | adj_set[ko].update([]) |
| 66 | if sn not in visited: |
| 67 | adj_set = self.adj_set_of_service_graph(n, visited, adj_set) |
| 68 | |
| 69 | return adj_set |
| 70 | |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 71 | def __init__(self, *args, **kwargs): |
| 72 | super(SyncVSPGWUTenant, self).__init__(*args, **kwargs) |
| 73 | |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 74 | def get_extra_attributes(self, o): |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 75 | blueprint = self.get_blueprint_and_check_dependencies(o) |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 76 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 77 | if blueprint == 'cord_4_1_blueprint': |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 78 | return self.get_values_for_CORD_4_1(o) |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 79 | elif blueprint == 'cord_5_0_blueprint': |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 80 | return self.get_values_for_CORD_5_0(o) |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 81 | else: |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 82 | return self.get_extra_attributes_for_manual(o) |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 83 | |
| 84 | # fields for manual case |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 85 | def get_extra_attributes_for_manual(self, o): |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 86 | fields = {} |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 87 | fields['blueprint'] = "manual" |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 88 | fields['cord_version'] = "manual" |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 89 | # for interface.cfg file |
| 90 | fields['zmq_sub_ip'] = "manual" |
| 91 | fields['zmq_pub_ip'] = "manual" |
| 92 | fields['dp_comm_ip'] = "manual" |
| 93 | fields['cp_comm_ip'] = "manual" |
| 94 | fields['fpc_ip'] = "manual" |
| 95 | fields['cp_nb_server_ip'] = "manual" |
| 96 | |
| 97 | # for dp_config.cfg file |
| 98 | fields['s1u_ip'] = "manual" |
| 99 | fields['sgi_ip'] = "manual" |
| 100 | |
Woojoong Kim | 04d1216 | 2017-11-27 10:59:08 -0800 | [diff] [blame] | 101 | # for static_arp.cfg file |
| 102 | fields['as_sgi_ip'] = "manual" |
| 103 | fields['as_sgi_mac'] = "manual" |
| 104 | fields['enb_s1u_ip'] = "manual" |
| 105 | fields['enb_s1u_mac'] = "manual" |
| 106 | |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 107 | return fields |
| 108 | |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 109 | def get_values_for_CORD_4_1(self, o): |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 110 | fields = {} |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 111 | fields['cord_version'] = "4.1" |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 112 | fields['blueprint'] = "cord_4_1_blueprint" |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 113 | # for interface.cfg file |
| 114 | fields['zmq_sub_ip'] = "127.0.0.1" |
| 115 | fields['zmq_pub_ip'] = "127.0.0.1" |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 116 | fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 117 | 'spgw_network', o, o, 'dp_comm_ip') |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 118 | fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance( |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 119 | 'spgw_network', "VSPGWCTenant", o, 'cp_comm_ip') |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 120 | fields['fpc_ip'] = "127.0.0.1" |
| 121 | fields['cp_nb_server_ip'] = "127.0.0.1" |
| 122 | |
| 123 | # for cp_config.cfg file |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 124 | fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 125 | 's1u_network', o, o, 's1u_ip') |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 126 | fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 127 | 'sgi_network', o, o, 'sgi_ip') |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 128 | |
Woojoong Kim | 04d1216 | 2017-11-27 10:59:08 -0800 | [diff] [blame] | 129 | # for static_arp.cfg file |
Sapan Bhatia | 1572193 | 2017-12-08 15:36:33 -0500 | [diff] [blame] | 130 | fields['as_sgi_ip'] = self.get_ip_address_from_peer_service_instance( |
| 131 | 'sgi_network', "VENBServiceInstance", o, 'as_sgi_ip') |
| 132 | fields['as_sgi_mac'] = self.get_mac_address_from_peer_service_instance( |
| 133 | 'sgi_network', "VENBServiceInstance", o, 'as_sgi_mac') |
| 134 | fields['enb_s1u_ip'] = self.get_ip_address_from_peer_service_instance( |
| 135 | 's1u_network', "VENBServiceInstance", o, 'enb_s1u_ip') |
| 136 | fields['enb_s1u_mac'] = self.get_mac_address_from_peer_service_instance( |
| 137 | 's1u_network', "VENBServiceInstance", o, 'enb_s1u_mac') |
Woojoong Kim | 04d1216 | 2017-11-27 10:59:08 -0800 | [diff] [blame] | 138 | |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 139 | return fields |
| 140 | |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 141 | def get_values_for_CORD_5_0(self, o): |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [diff] [blame] | 142 | fields = {} |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 143 | fields['cord_version'] = "5.0" |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 144 | fields['blueprint'] = "cord_5_0_blueprint" |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [diff] [blame] | 145 | |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [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_from_peer_service_instance_instance( |
| 150 | 'spgw_network', o, o, 'dp_comm_ip') |
| 151 | fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance( |
| 152 | 'spgw_network', "VSPGWCTenant", o, 'cp_comm_ip') |
| 153 | fields['fpc_ip'] = "127.0.0.1" |
| 154 | fields['cp_nb_server_ip'] = "127.0.0.1" |
| 155 | |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 156 | # for cp_config.cfg file |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [diff] [blame] | 157 | fields['s1u_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
Woojoong Kim | 4f3ea43 | 2018-01-22 17:35:52 -0800 | [diff] [blame] | 158 | 'flat_network_s1u', o, o, 's1u_ip') |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [diff] [blame] | 159 | fields['sgi_ip'] = self.get_ip_address_from_peer_service_instance_instance( |
| 160 | 'sgi_network', o, o, 'sgi_ip') |
| 161 | |
| 162 | # for static_arp.cfg file |
Woojoong Kim | 2f03e44 | 2018-01-18 20:02:22 -0800 | [diff] [blame] | 163 | internetemulator_flag = self.has_instance("InternetEmulatorServiceInstance", o) |
| 164 | if (internetemulator_flag): |
| 165 | fields['as_sgi_ip'] = self.get_ip_address_from_peer_service_instance('sgi_network', "InternetEmulatorServiceInstance", o, 'as_sgi_ip') |
| 166 | fields['as_sgi_mac'] = self.get_mac_address_from_peer_service_instance('sgi_network', "InternetEmulatorServiceInstance", o, 'as_sgi_mac') |
| 167 | else: |
| 168 | fields['as_sgi_ip'] = o.appserver_ip_addr |
| 169 | fields['as_sgi_mac'] = o.appserver_mac_addr |
| 170 | fields['enb_s1u_ip'] = o.enodeb_ip_addr # write down eNB IP address manually (S1U) |
| 171 | fields['enb_s1u_mac'] = o.enodeb_mac_addr # write down eNB MAC address manually (S1U) |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [diff] [blame] | 172 | |
| 173 | return fields |
| 174 | |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 175 | def has_instance(self, sitype, o): |
Sapan Bhatia | fc9f6b3 | 2017-11-28 14:13:31 -0500 | [diff] [blame] | 176 | try: |
| 177 | i = self.get_peer_serviceinstance_of_type(sitype, o) |
| 178 | except ServiceGraphException: |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 179 | self.log.info("Missing in ServiceInstance graph", |
| 180 | serviceinstance=sitype) |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 181 | return False |
| 182 | |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 183 | return i.leaf_model.instance_id |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 184 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 185 | def find_first_blueprint_subgraph(self, blueprints, adj_set): |
| 186 | found_blueprint = None |
| 187 | for blueprint in blueprints: |
| 188 | found = True |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 189 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 190 | for node in blueprint['graph']: |
| 191 | if node['name'] not in adj_set: |
| 192 | found = False |
| 193 | break |
| 194 | try: |
| 195 | links = node['links'] |
| 196 | except KeyError: |
| 197 | links = [] |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 198 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 199 | for link in links: |
| 200 | if link['name'] not in adj_set[node['name']]: |
| 201 | found = False |
| 202 | break |
| 203 | if not found: |
| 204 | break |
Woojoong Kim | 4a15d41 | 2017-12-18 17:17:40 -0800 | [diff] [blame] | 205 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 206 | if found: |
| 207 | found_blueprint = blueprint['name'] |
| 208 | break |
| 209 | |
| 210 | return found_blueprint |
| 211 | |
| 212 | def check_instance_dependencies(self, blueprints, blueprint_name, o): |
| 213 | blueprint = next( |
| 214 | b for b in blueprints if b['name'] == blueprint_name) |
| 215 | node = next(n for n in blueprint['graph'] if n['name'] == o.leaf_model_name) |
Andy Bavier | 2a0f9f3 | 2018-01-31 16:01:08 -0700 | [diff] [blame] | 216 | |
| 217 | try: |
| 218 | links = node['links'] |
| 219 | except KeyError: |
| 220 | links = [] |
| 221 | |
| 222 | for link in links: |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 223 | flag = self.has_instance(link['name'], o) |
| 224 | if not flag: |
| 225 | self.defer_sync('%s does not have an instance. Deferring synchronization.'%link['name']) |
Andy Bavier | 2a0f9f3 | 2018-01-31 16:01:08 -0700 | [diff] [blame] | 226 | |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 227 | def get_blueprint_and_check_dependencies(self, o): |
| 228 | blueprints = Config().get('blueprints') |
| 229 | |
| 230 | adj_set = self.adj_set_of_service_graph(o) |
| 231 | blueprint_name = self.find_first_blueprint_subgraph(blueprints, adj_set) |
| 232 | if blueprint_name: |
| 233 | self.check_instance_dependencies(blueprints, blueprint_name, o) |
| 234 | |
| 235 | ret_blueprint = 'manual' if not blueprint_name else blueprint_name |
| 236 | return ret_blueprint |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 237 | |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 238 | def get_peer_serviceinstance_of_type(self, sitype, o): |
Woojoong Kim | 4f3ea43 | 2018-01-22 17:35:52 -0800 | [diff] [blame] | 239 | |
| 240 | global_list = self.get_all_instances_in_graph(o) |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 241 | |
| 242 | try: |
Woojoong Kim | 4f3ea43 | 2018-01-22 17:35:52 -0800 | [diff] [blame] | 243 | peer_service = next(p for p in global_list if p.leaf_model_name == sitype) |
| 244 | |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 245 | except StopIteration: |
Woojoong Kim | 4f3ea43 | 2018-01-22 17:35:52 -0800 | [diff] [blame] | 246 | self.log.error( |
| 247 | 'Could not find service type in service graph', service_type=sitype, object=o) |
| 248 | raise ServiceGraphException( |
| 249 | "Synchronization failed due to incomplete service graph") |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 250 | |
| 251 | return peer_service |
| 252 | |
Woojoong Kim | 4f3ea43 | 2018-01-22 17:35:52 -0800 | [diff] [blame] | 253 | def has_instance_in_list(self, list, o): |
| 254 | for instance in list: |
| 255 | if instance.leaf_model_name == o.leaf_model_name: |
| 256 | return True |
| 257 | |
| 258 | return False |
| 259 | |
| 260 | def get_all_instances_in_graph(self, o): |
| 261 | |
| 262 | to_search_list = self.get_one_hop_instances_in_graph(o) |
| 263 | result_list = [] |
| 264 | |
| 265 | while len(to_search_list) > 0: |
| 266 | tmp_obj = to_search_list[0] |
| 267 | to_search_list.remove(tmp_obj) |
| 268 | tmp_list = self.get_one_hop_instances_in_graph(tmp_obj) |
| 269 | |
| 270 | for index_obj in tmp_list: |
| 271 | if (not self.has_instance_in_list(to_search_list, index_obj)) and (not self.has_instance_in_list(result_list, index_obj)): |
| 272 | to_search_list.append(index_obj) |
| 273 | |
| 274 | result_list.append(tmp_obj) |
| 275 | return result_list |
| 276 | |
| 277 | def get_one_hop_instances_in_graph(self, o): |
| 278 | instance_list = [] |
| 279 | |
| 280 | # 1 hop forward and backward |
| 281 | prov_links = ServiceInstanceLink.objects.filter(subscriber_service_instance_id=o.id) |
| 282 | subs_links = ServiceInstanceLink.objects.filter(provider_service_instance_id=o.id) |
| 283 | |
| 284 | # add instances located in 1 hop into instance_list |
| 285 | for tmp_link1 in prov_links: |
| 286 | if not self.has_instance_in_list(instance_list, tmp_link1.provider_service_instance): |
| 287 | instance_list.append(tmp_link1.provider_service_instance) |
| 288 | |
| 289 | for tmp_link1 in subs_links: |
| 290 | if not self.has_instance_in_list(instance_list, tmp_link1.subscriber_service_instance): |
| 291 | instance_list.append(tmp_link1.subscriber_service_instance) |
| 292 | |
| 293 | return instance_list |
| 294 | |
| 295 | |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 296 | # Maybe merge the two pairs of functions into one, with an address type "mac" or "ip" - SB |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 297 | def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None): |
| 298 | peer_si = self.get_peer_serviceinstance_of_type(sitype, o) |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 299 | return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter) |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 300 | |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 301 | def get_mac_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None): |
| 302 | peer_si = self.get_peer_serviceinstance_of_type(sitype, o) |
| 303 | return self.get_mac_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter) |
| 304 | |
| 305 | def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None): |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 306 | try: |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 307 | net_id = self.get_network_id(network_name) |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 308 | ins_id = peer_si.leaf_model.instance_id |
Sapan Bhatia | 968f28f | 2017-11-22 12:00:01 -0500 | [diff] [blame] | 309 | ip_address = Port.objects.get( |
| 310 | network_id=net_id, instance_id=ins_id).ip |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 311 | except Exception: |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 312 | self.log.error("Failed to fetch parameter", |
| 313 | parameter=parameter, |
| 314 | network_name=network_name) |
| 315 | self.defer_sync(o, "Waiting for parameters to become available") |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 316 | |
| 317 | return ip_address |
| 318 | |
Sapan Bhatia | 1572193 | 2017-12-08 15:36:33 -0500 | [diff] [blame] | 319 | def get_mac_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter): |
Woojoong Kim | 04d1216 | 2017-11-27 10:59:08 -0800 | [diff] [blame] | 320 | try: |
| 321 | net_id = self.get_network_id(network_name) |
Sapan Bhatia | 1572193 | 2017-12-08 15:36:33 -0500 | [diff] [blame] | 322 | ins_id = peer_si.leaf_model.instance_id |
| 323 | mac_address = Port.objects.get( |
| 324 | network_id=net_id, instance_id=ins_id).mac |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 325 | |
Woojoong Kim | 04d1216 | 2017-11-27 10:59:08 -0800 | [diff] [blame] | 326 | except Exception: |
Sapan Bhatia | 1572193 | 2017-12-08 15:36:33 -0500 | [diff] [blame] | 327 | self.log.error("Failed to fetch parameter to get MAC", |
| 328 | parameter=parameter, network_name=network_name) |
Sapan Bhatia | 2f27dbe | 2017-11-27 21:17:32 -0500 | [diff] [blame] | 329 | self.defer_sync(o, "Waiting for parameters to become available") |
Woojoong Kim | 04d1216 | 2017-11-27 10:59:08 -0800 | [diff] [blame] | 330 | |
| 331 | return mac_address |
| 332 | |
Woojoong Kim | f002aec | 2017-10-25 14:43:37 -0700 | [diff] [blame] | 333 | # To get each network id |
| 334 | def get_network_id(self, network_name): |
| 335 | return Network.objects.get(name=network_name).id |
Sapan Bhatia | ae30965 | 2018-01-23 23:40:40 -0500 | [diff] [blame] | 336 | |