- wim_account = persistence.get_wim_account_by(uuid=self.wim_account_id)
-
- # TODO: make more generic to support networks that are not created with
- # the SDN assist. This method should have a consistent way of getting
- # the endpoint for all different types of networks used in the VIM
- # (provider networks, SDN assist, overlay networks, ...)
- if instance_net.get('sdn_net_id'):
- return self._get_connection_point_info_sdn(
- persistence, ovim, instance_net, wim_account['wim_id'])
- else:
- raise InconsistentState(
- 'The field `instance_nets.sdn_net_id` was expected to be '
- 'found in the database for the record %s after the network '
- 'become active, but it is still NULL', instance_net['uuid'])
+ # First, we need to find a route from the datacenter to the outside
+ # world. For that, we can use the rules given in the datacenter
+ # configuration:
+ datacenter_id = instance_net['datacenter_id']
+ datacenter = persistence.get_datacenter_by(datacenter_id)
+ rules = safe_get(datacenter, 'config.external_connections', {}) or {}
+ vim_info = instance_net.get('vim_info', {}) or {}
+ # Alternatively, we can look for it, using the SDN assist
+ external_port = (self._evaluate_rules(rules, vim_info) or
+ self._get_port_sdn(ovim, instance_net))
+
+ if not external_port:
+ raise NoExternalPortFound(instance_net)
+
+ # Then, we find the WAN switch that is connected to this external port
+ try:
+ wim_account = persistence.get_wim_account_by(
+ uuid=self.wim_account_id)
+
+ criteria = {
+ 'wim_id': wim_account['wim_id'],
+ 'pop_switch_dpid': external_port[0],
+ 'pop_switch_port': external_port[1],
+ 'datacenter_id': datacenter_id}
+
+ wan_port_mapping = persistence.query_one(
+ FROM='wim_port_mappings',
+ WHERE=criteria)
+ except NoRecordFound:
+ ex = InconsistentState('No WIM port mapping found:'
+ 'wim_account: {}\ncriteria:\n{}'.format(
+ self.wim_account_id, pformat(criteria)))
+ reraise(ex.__class__, ex, exc_info()[2])
+
+ # It is important to return encapsulation information if present
+ mapping = merge_dicts(
+ wan_port_mapping.get('wan_service_mapping_info'),
+ filter_keys(vim_info, ('encapsulation_type', 'encapsulation_id'))
+ )
+
+ return merge_dicts(wan_port_mapping, wan_service_mapping_info=mapping)