示例#1
0
    def _get_subnets_from_port(self, context, port):
        """Return the subnets for a given port."""

        fixed_ips = port['fixed_ips']
        # No fixed_ips for the port means there is no subnet associated
        # with the network the port is created on.
        # Since list_subnets(id=[]) returns all subnets visible for the
        # current tenant, returned subnets may contain subnets which is not
        # related to the port. To avoid this, the method returns here.
        if not fixed_ips:
            return []
        search_opts = {'id': [ip['subnet_id'] for ip in fixed_ips]}
        data = neutronv2.get_client(context).list_subnets(**search_opts)
        ipam_subnets = data.get('subnets', [])
        subnets = []

        for subnet in ipam_subnets:
            subnet_dict = {'cidr': subnet['cidr'],
                           'gateway': network_model.IP(
                                address=subnet['gateway_ip'],
                                type='gateway'),
            }

            # attempt to populate DHCP server field
            search_opts = {'network_id': subnet['network_id'],
                           'device_owner': 'network:dhcp'}
            data = neutronv2.get_client(context).list_ports(**search_opts)
            dhcp_ports = data.get('ports', [])
            for p in dhcp_ports:
                for ip_pair in p['fixed_ips']:
                    if ip_pair['subnet_id'] == subnet['id']:
                        subnet_dict['dhcp_server'] = ip_pair['ip_address']
                        break

            subnet_object = network_model.Subnet(**subnet_dict)
            for dns in subnet.get('dns_nameservers', []):
                subnet_object.add_dns(
                    network_model.IP(address=dns, type='dns'))

            for route in subnet.get('host_routes', []):
                subnet_object.add_route(
                    network_model.Route(cidr=route['destination'],
                                        gateway=network_model.IP(
                                            address=route['nexthop'],
                                            type='gateway')))

            subnets.append(subnet_object)
        return subnets
示例#2
0
 def deallocate(self, context, instance_id):
     """Deallocate all network resources related to the instance."""
     search_opts = {'device_id': instance_id}
     neutron = neutronv2.get_client(context, admin=True)
     data = neutron.list_ports(**search_opts)
     ports = [port['id'] for port in data.get('ports', [])]
     self._delete_ports(neutron, instance_id, ports, raise_if_fail=True)
示例#3
0
    def allocate(self, context, instance_id, host, tenant_id, **kwargs):
        neutron = neutronv2.get_client(context, admin=True)
        dhcp_opts = kwargs.get('dhcp_options', None)
        zone = kwargs.get('zone')
        net_ids = [kwargs.get('network_id')]
        nets = self._get_available_networks(context, tenant_id, net_ids)
        if not nets:
            LOG.warn(_LW("No network configured!"))
            return network_model.NetworkInfo([])
        security_groups = kwargs.get('security_groups', [])
        security_group_ids = []
        if len(security_groups):
            search_opts = {'tenant_id': tenant_id}
            user_security_groups = neutron.list_security_groups(
                **search_opts).get('security_groups')
        for security_group in security_groups:
            name_match = None
            uuid_match = None
            for user_security_group in user_security_groups:
                if user_security_group['name'] == security_group:
                    if name_match:
                        raise exception.NoUniqueMatch(
                            _("Multiple security groups found matching"
                              " '%s'. Use an ID to be more specific.") %
                               security_group)

                    name_match = user_security_group['id']
                if user_security_group['id'] == security_group:
                    uuid_match = user_security_group['id']

            # If a user names the security group the same as
            # another's security groups uuid, the name takes priority.
            if not name_match and not uuid_match:
                raise exception.SecurityGroupNotFound(
                    security_group_id=security_group)
            elif name_match:
                security_group_ids.append(name_match)
            elif uuid_match:
                security_group_ids.append(uuid_match)
        for net in nets:
            if net['id'] == kwargs.get('network_id'):
                network = net
                break
        if (security_groups and not (
                network['subnets']
                and network.get('port_security_enabled', True))):
            raise exception.SecurityGroupCannotBeApplied()
        port_req_body = {'port': {'device_id': instance_id,
                                  'device_owner': zone,
                                  'binding:host_id': host}}
        created_port = self._create_port(
                neutron, tenant_id, network['id'],
                port_req_body, None, security_group_ids, dhcp_opts)
        nw_info = self._build_network_info_model(context, instance_id, tenant_id,
                                                 networks=[net],
                                                 port_ids=[created_port])
        return network_model.NetworkInfo([vif for vif in nw_info])
示例#4
0
 def _get_available_networks(self, context, project_id,
                             net_ids=None, neutron=None):
     if not neutron:
         neutron = neutronv2.get_client(context, admin=True)
     if net_ids:
         search_opts = {'id': net_ids}
         nets = neutron.list_networks(**search_opts).get('networks', [])
     else:
         search_opts = {'tenant_id': project_id, 'shared': False}
         nets = neutron.list_networks(**search_opts).get('networks', [])
     return nets
示例#5
0
 def get_network_info(self, context, container_name, tenant_id, **kwargs):
     neutron = neutronv2.get_client(context, admin=True)
     net_ids = [kwargs.get('network_id')]
     nets = self._get_available_networks(context, tenant_id, net_ids=net_ids, 
                                         neutron=neutron)
     search_opts = {'device_id': container_name}
     data = neutron.list_ports(**search_opts)
     ports = [port['id'] for port in data.get('ports', [])]
     nw_info = self._build_network_info_model(context, container_name, tenant_id,
                                              networks=nets,
                                              port_ids=ports)
     return network_model.NetworkInfo([vif for vif in nw_info])
示例#6
0
    def _build_network_info_model(self, context, instance_id, tenant_id, 
                                  networks=None, port_ids=None):
        search_opts = {'tenant_id': tenant_id,
                   'device_id': instance_id, }
        client = neutronv2.get_client(context, admin=True)
        data = client.list_ports(**search_opts)
        current_neutron_ports = data.get('ports', [])
        nw_info = network_model.NetworkInfo()

        current_neutron_port_map = {}
        for current_neutron_port in current_neutron_ports:
            current_neutron_port_map[current_neutron_port['id']] = (
                current_neutron_port)

        for port_id in port_ids:
            current_neutron_port = current_neutron_port_map.get(port_id)
            if current_neutron_port:
                vif_active = False
                if (current_neutron_port['admin_state_up'] is False
                    or current_neutron_port['status'] == 'ACTIVE'):
                    vif_active = True

                network_IPs = self._nw_info_get_ips(client,
                                                    current_neutron_port)
                subnets = self._nw_info_get_subnets(context,
                                                    current_neutron_port,
                                                    network_IPs)
                devname = "tap" + current_neutron_port['id']
                devname = devname[:network_model.NIC_NAME_LEN]
                network, ovs_interfaceid = (
                    self._nw_info_build_network(current_neutron_port,
                                            networks, subnets))
                nw_info.append(network_model.VIF(
                    id=current_neutron_port['id'],
                    address=current_neutron_port['mac_address'],
                    network=network,
                    vnic_type=current_neutron_port.get('binding:vnic_type',
                        network_model.VNIC_TYPE_NORMAL),
                    type=current_neutron_port.get('binding:vif_type'),
                    profile=current_neutron_port.get('binding:profile'),
                    details=current_neutron_port.get('binding:vif_details'),
                    ovs_interfaceid=ovs_interfaceid,
                    devname=devname,
                    active=vif_active))
        return nw_info