def _add_to_allowed_address_pairs(self, neutron, port, ip_addresses, mac_address=None): if not ip_addresses: raise k_exc.IntegrityError( "Cannot add pair from the " "allowed_address_pairs of port %s: missing IP address", port['id']) mac = mac_address if mac_address else port['mac_address'] address_pairs = port['allowed_address_pairs'] # look for duplicates or near-matches for pair in address_pairs: if pair['ip_address'] in ip_addresses: if pair['mac_address'] is mac: raise k_exc.AllowedAddressAlreadyPresent( "Pair %s already " "present in the 'allowed_address_pair' list. This is " "due to a misconfiguration or a bug", pair) else: LOG.warning( "A pair with IP %s but different MAC address " "is already present in the 'allowed_address_pair'. " "This could indicate a misconfiguration or a " "bug", pair['ip_address']) for ip in ip_addresses: address_pairs.append({'ip_address': ip, 'mac_address': mac}) self._update_port_address_pairs(neutron, port['id'], address_pairs)
def _make_vif_subnets(neutron_port, subnets): """Gets a list of os-vif Subnet objects for port. :param neutron_port: dict containing port information as returned by neutron client's 'show_port' :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets :return: list of os-vif Subnet object """ vif_subnets = {} for neutron_fixed_ip in neutron_port.get('fixed_ips', []): subnet_id = neutron_fixed_ip['subnet_id'] ip_address = neutron_fixed_ip['ip_address'] if subnet_id not in subnets: continue try: subnet = vif_subnets[subnet_id] except KeyError: subnet = _make_vif_subnet(subnets, subnet_id) vif_subnets[subnet_id] = subnet subnet.ips.objects.append(osv_fixed_ip.FixedIP(address=ip_address)) if not vif_subnets: raise k_exc.IntegrityError(_( "No valid subnets found for port %(port_id)s") % { 'port_id': neutron_port.get('id')}) return list(vif_subnets.values())
def _remove_from_allowed_address_pairs(self, neutron, port, ip_addresses, mac_address=None): if not ip_addresses: raise k_exc.IntegrityError( "Cannot remove pair from the " "allowed_address_pairs of port %s: missing IP address", port['id']) mac = mac_address if mac_address else port['mac_address'] address_pairs = port['allowed_address_pairs'] updated = False for ip in ip_addresses: try: address_pairs.remove({'ip_address': ip, 'mac_address': mac}) updated = True except ValueError: LOG.error( "No {'ip_address': %s, 'mac_address': %s} pair " "found in the 'allowed_address_pair' list while " "trying to remove it.", ip, mac) if updated: self._update_port_address_pairs(neutron, port['id'], address_pairs)
def _get_fixed_ips(self, subnets, pod): fixed_ips = [] pod_ip = pod['ipAddr'] for subnet_id, network in subnets.items(): ips = [] if len(network.subnets.objects) > 1: raise k_exc.IntegrityError( _("Network object for subnet %(subnet_id)s is invalid, " "must contain a single subnet, but %(num_subnets)s found" ) % { 'subnet_id': subnet_id, 'num_subnets': len(network.subnets.objects) }) for subnet in network.subnets.objects: if subnet.obj_attr_is_set('ips'): ips.extend([str(ip.address) for ip in subnet.ips.objects]) if ips: fixed_ips.extend([{ 'subnet_id': subnet_id, 'ip_address': ip } for ip in ips]) else: fixed_ips.append({ 'subnet_id': subnet_id, 'ip_address': pod_ip }) return fixed_ips
def _get_service_link(self, endpoints): ep_link = endpoints['metadata']['selfLink'] link_parts = ep_link.split('/') if link_parts[-2] != 'endpoints': raise k_exc.IntegrityError( _("Unsupported endpoints link: %(link)s") % {'link': ep_link}) link_parts[-2] = 'services' return "/".join(link_parts)
def _get_endpoints_link(self, service): svc_link = service['metadata']['selfLink'] link_parts = svc_link.split('/') if link_parts[-2] != 'services': raise k_exc.IntegrityError( _("Unsupported service link: %(link)s") % {'link': svc_link}) link_parts[-2] = 'endpoints' return "/".join(link_parts)
def _get_network_id(self, subnets): ids = ovu.osvif_to_neutron_network_ids(subnets) if len(ids) != 1: raise k_exc.IntegrityError( "Subnet mapping %(subnets)s is not valid: " "%(num_networks)s unique networks found" % { 'subnets': subnets, 'num_networks': len(ids) }) return ids[0]
def _get_subnet_id(self, service, project_id, ip): subnets_mapping = self._drv_subnets.get_subnets(service, project_id) subnet_ids = { subnet_id for subnet_id, network in subnets_mapping.items() for subnet in network.subnets.objects if ip in subnet.cidr } if len(subnet_ids) != 1: raise k_exc.IntegrityError( _("Found %(num)s subnets for service %(link)s IP %(ip)s") % { 'link': service['metadata']['selfLink'], 'ip': ip, 'num': len(subnet_ids) }) return subnet_ids.pop()
def _make_vif_subnet(subnets, subnet_id): """Makes a copy of an os-vif Subnet from subnets mapping. :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets :param subnet_id: ID of the subnet to extract from 'subnets' mapping :return: a copy of an os-vif Subnet object matching 'subnet_id' """ network = subnets[subnet_id] if len(network.subnets.objects) != 1: raise k_exc.IntegrityError(_( "Network object for subnet %(subnet_id)s is invalid, " "must contain a single subnet, but %(num_subnets)s found") % { 'subnet_id': subnet_id, 'num_subnets': len(network.subnets.objects)}) subnet = network.subnets.objects[0].obj_clone() subnet.ips = osv_fixed_ip.FixedIPList(objects=[]) return subnet
def _make_vif_network(neutron_port, subnets): """Gets a os-vif Network object for port. :param neutron_port: dict containing port information as returned by neutron client's 'show_port' :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets :return: os-vif Network object """ try: network = next(net.obj_clone() for net in subnets.values() if net.id == neutron_port.get('network_id')) except StopIteration: raise k_exc.IntegrityError(_( "Port %(port_id)s belongs to network %(network_id)s, " "but requested networks are: %(requested_networks)s") % { 'port_id': neutron_port.get('id'), 'network_id': neutron_port.get('network_id'), 'requested_networks': [net.id for net in subnets.values()]}) network.subnets = osv_subnet.SubnetList( objects=_make_vif_subnets(neutron_port, subnets)) return network