Ejemplo n.º 1
0
    def _process_provider_create(self, context, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        physical_network = attrs.get(provider.PHYSICAL_NETWORK)
        segmentation_id = attrs.get(provider.SEGMENTATION_ID)

        network_type_set = attributes.is_attr_set(network_type)
        physical_network_set = attributes.is_attr_set(physical_network)
        segmentation_id_set = attributes.is_attr_set(segmentation_id)

        if not (network_type_set or physical_network_set
                or segmentation_id_set):
            return (None, None, None)

        if not network_type_set:
            msg = _("provider:network_type required")
            raise q_exc.InvalidInput(error_message=msg)
        elif network_type == constants.TYPE_FLAT:
            self._process_flat_net(segmentation_id_set)
            segmentation_id = constants.FLAT_VLAN_ID

        elif network_type in [constants.TYPE_VLAN, constants.TYPE_IB]:
            self._process_vlan_net(segmentation_id, segmentation_id_set)

        elif network_type == constants.TYPE_LOCAL:
            self._process_local_net(physical_network_set, segmentation_id_set)
            segmentation_id = constants.LOCAL_VLAN_ID
            physical_network = None

        else:
            msg = _("provider:network_type %s not supported") % network_type
            raise q_exc.InvalidInput(error_message=msg)
        physical_network = self._process_net_type(network_type,
                                                  physical_network,
                                                  physical_network_set)
        return (network_type, physical_network, segmentation_id)
Ejemplo n.º 2
0
 def _process_local_net(self, physical_network_set, segmentation_id_set):
     if physical_network_set:
         msg = _("provider:physical_network specified for local " "network")
         raise q_exc.InvalidInput(error_message=msg)
     if segmentation_id_set:
         msg = _("provider:segmentation_id specified for local " "network")
         raise q_exc.InvalidInput(error_message=msg)
Ejemplo n.º 3
0
 def _validate_network_mapping_info(self, network_mapping_info):
     self._set_mapping_info_defaults(network_mapping_info)
     network_id = network_mapping_info.get(NETWORK_ID)
     if not network_id:
         raise exceptions.InvalidInput(
             error_message=_("A network identifier must be specified "
                             "when connecting a network to a network "
                             "gateway. Unable to complete operation"))
     connection_attrs = set(network_mapping_info.keys())
     if not connection_attrs.issubset(ALLOWED_CONNECTION_ATTRIBUTES):
         raise exceptions.InvalidInput(
             error_message=(_("Invalid keys found among the ones provided "
                              "in request body: %(connection_attrs)s."),
                            connection_attrs))
     seg_type = network_mapping_info.get(SEGMENTATION_TYPE)
     seg_id = network_mapping_info.get(SEGMENTATION_ID)
     if not seg_type and seg_id:
         msg = _("In order to specify a segmentation id the "
                 "segmentation type must be specified as well")
         raise exceptions.InvalidInput(error_message=msg)
     elif seg_type and seg_type.lower() == 'flat' and seg_id:
         msg = _("Cannot specify a segmentation id when "
                 "the segmentation type is flat")
         raise exceptions.InvalidInput(error_message=msg)
     return network_id
Ejemplo n.º 4
0
    def validate_provider_segment(self, segment):
        physical_network = segment.get(api.PHYSICAL_NETWORK)
        if not physical_network:
            msg = _("physical_network required for VLAN provider network")
            raise exc.InvalidInput(error_message=msg)
        if physical_network not in self.network_vlan_ranges:
            msg = (_("physical_network '%s' unknown for VLAN provider network")
                   % physical_network)
            raise exc.InvalidInput(error_message=msg)

        segmentation_id = segment.get(api.SEGMENTATION_ID)
        if segmentation_id is None:
            msg = _("segmentation_id required for VLAN provider network")
            raise exc.InvalidInput(error_message=msg)
        if not utils.is_valid_vlan_tag(segmentation_id):
            msg = (_("segmentation_id out of range (%(min)s through "
                     "%(max)s)") %
                   {'min': q_const.MIN_VLAN_TAG,
                    'max': q_const.MAX_VLAN_TAG})
            raise exc.InvalidInput(error_message=msg)

        for key, value in segment.iteritems():
            if value and key not in [api.NETWORK_TYPE,
                                     api.PHYSICAL_NETWORK,
                                     api.SEGMENTATION_ID]:
                msg = _("%s prohibited for VLAN provider network") % key
                raise exc.InvalidInput(error_message=msg)

        return segment
Ejemplo n.º 5
0
    def _test_fixed_ips_for_port(self, context, network_id, fixed_ips):
        """Test fixed IPs for port.

        Check that configured subnets are valid prior to allocating any
        IPs. Include the subnet_id in the result if only an IP address is
        configured.

        :raises: InvalidInput, IpAddressInUse
        """
        fixed_ip_set = []
        for fixed in fixed_ips:
            found = False
            if 'subnet_id' not in fixed:
                if 'ip_address' not in fixed:
                    msg = _('IP allocation requires subnet_id or ip_address')
                    raise q_exc.InvalidInput(error_message=msg)

                filter = {'network_id': [network_id]}
                subnets = self.get_subnets(context, filters=filter)
                for subnet in subnets:
                    if QuantumDbPluginV2._check_subnet_ip(subnet['cidr'],
                                                          fixed['ip_address']):
                        found = True
                        subnet_id = subnet['id']
                        break
                if not found:
                    msg = _('IP address %s is not a valid IP for the defined '
                            'networks subnets') % fixed['ip_address']
                    raise q_exc.InvalidInput(error_message=msg)
            else:
                subnet = self._get_subnet(context, fixed['subnet_id'])
                if subnet['network_id'] != network_id:
                    msg = _('Failed to create port on network %s, '
                            'because fixed_ips included invalid subnet '
                            '%s') % (network_id, fixed['subnet_id'])
                    raise q_exc.InvalidInput(error_message=msg)
                subnet_id = subnet['id']

            if 'ip_address' in fixed:
                # Ensure that the IP's are unique
                if not QuantumDbPluginV2._check_unique_ip(context, network_id,
                                                          subnet_id,
                                                          fixed['ip_address']):
                    raise q_exc.IpAddressInUse(net_id=network_id,
                                               ip_address=fixed['ip_address'])

                # Ensure that the IP is valid on the subnet
                if (not found and
                    not QuantumDbPluginV2._check_subnet_ip(
                        subnet['cidr'], fixed['ip_address'])):
                    msg = _('IP address %s is not a valid IP for the defined '
                            'subnet') % fixed['ip_address']
                    raise q_exc.InvalidInput(error_message=msg)

                fixed_ip_set.append({'subnet_id': subnet_id,
                                     'ip_address': fixed['ip_address']})
            else:
                fixed_ip_set.append({'subnet_id': subnet_id})
        return fixed_ip_set
Ejemplo n.º 6
0
 def _process_vlan_net(self, segmentation_id, segmentation_id_set):
     if not segmentation_id_set:
         msg = _("provider:segmentation_id required")
         raise q_exc.InvalidInput(error_message=msg)
     if not utils.is_valid_vlan_tag(segmentation_id):
         msg = (_("provider:segmentation_id out of range "
                  "(%(min_id)s through %(max_id)s)") % {
                      'min_id': q_const.MIN_VLAN_TAG,
                      'max_id': q_const.MAX_VLAN_TAG
                  })
         raise q_exc.InvalidInput(error_message=msg)
    def create_network(self, session, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        segmentation_id = attrs.get(provider.SEGMENTATION_ID)
        if attributes.is_attr_set(segmentation_id):
            msg = _("segmentation_id specified "
                    "for %s network") % network_type
            raise q_exc.InvalidInput(error_message=msg)
        attrs[provider.SEGMENTATION_ID] = None

        physical_network = attrs.get(provider.PHYSICAL_NETWORK)
        if attributes.is_attr_set(physical_network):
            msg = _("physical_network specified "
                    "for %s network") % network_type
            raise q_exc.InvalidInput(error_message=msg)
        attrs[provider.PHYSICAL_NETWORK] = None
Ejemplo n.º 8
0
def _validate_no_whitespace(data):
    """Validates that input has no whitespace."""
    if len(data.split()) > 1:
        msg = _("'%s' contains whitespace") % data
        LOG.debug(msg)
        raise q_exc.InvalidInput(error_message=msg)
    return data
Ejemplo n.º 9
0
    def validate_provider_segment(self, segment):
        for key, value in segment.iteritems():
            if value and key not in [api.NETWORK_TYPE]:
                msg = _("%s prohibited for local provider network") % key
                raise exc.InvalidInput(error_message=msg)

        return segment
Ejemplo n.º 10
0
 def _process_net_type(self, network_type, physical_network,
                       physical_network_set):
     if network_type in [
             constants.TYPE_VLAN, constants.TYPE_IB, constants.TYPE_FLAT
     ]:
         if physical_network_set:
             if physical_network not in self.network_vlan_ranges:
                 msg = _("unknown provider:physical_network "
                         "%s") % physical_network
                 raise q_exc.InvalidInput(error_message=msg)
         elif 'default' in self.network_vlan_ranges:
             physical_network = 'default'
         else:
             msg = _("provider:physical_network required")
             raise q_exc.InvalidInput(error_message=msg)
     return physical_network
Ejemplo n.º 11
0
def convert_to_uuid_list_or_none(value_list):
    if value_list is None:
        return
    for sg_id in value_list:
        if not uuidutils.is_uuid_like(sg_id):
            msg = _("'%s' is not an integer or uuid") % sg_id
            raise qexception.InvalidInput(error_message=msg)
    return value_list
Ejemplo n.º 12
0
    def validate_provider_segment(self, segment):
        physical_network = segment.get(api.PHYSICAL_NETWORK)
        if not physical_network:
            msg = _("physical_network required for flat provider network")
            raise exc.InvalidInput(error_message=msg)
        if self.flat_networks and physical_network not in self.flat_networks:
            msg = (
                _("physical_network '%s' unknown for flat provider network") %
                physical_network)
            raise exc.InvalidInput(error_message=msg)

        for key, value in segment.iteritems():
            if value and key not in [api.NETWORK_TYPE, api.PHYSICAL_NETWORK]:
                msg = _("%s prohibited for flat provider network") % key
                raise exc.InvalidInput(error_message=msg)

        return segment
Ejemplo n.º 13
0
 def validate_provider_segment(self, segment):
     network_type = segment[api.NETWORK_TYPE]
     driver = self.drivers.get(network_type)
     if driver:
         return driver.obj.validate_provider_segment(segment)
     else:
         msg = _("network_type value '%s' not supported") % network_type
         raise exc.InvalidInput(error_message=msg)
 def _set_tenant_network_type(self):
     tenant_network_type = cfg.CONF.HYPERV.tenant_network_type
     if tenant_network_type not in [
             constants.TYPE_LOCAL, constants.TYPE_FLAT, constants.TYPE_VLAN,
             constants.TYPE_NONE
     ]:
         msg = _("Invalid tenant_network_type: %(tenant_network_type)s. "
                 "Agent terminated!") % locals()
         raise q_exc.InvalidInput(error_message=msg)
     self._tenant_network_type = tenant_network_type
Ejemplo n.º 15
0
def convert_kvp_str_to_list(data):
    """Convert a value of the form 'key=value' to ['key', 'value'].

    :raises: q_exc.InvalidInput if any of the strings are malformed
                                (e.g. do not contain a key).
    """
    kvp = [x.strip() for x in data.split('=', 1)]
    if len(kvp) == 2 and kvp[0]:
        return kvp
    msg = _("'%s' is not of the form <key>=[value]") % data
    raise q_exc.InvalidInput(error_message=msg)
Ejemplo n.º 16
0
def convert_to_unsigned_int_or_none(val):
    if val is None:
        return
    try:
        val = int(val)
        if val < 0:
            raise ValueError
    except (ValueError, TypeError):
        msg = _("'%s' must be a non negative integer.") % val
        raise qexception.InvalidInput(error_message=msg)
    return val
Ejemplo n.º 17
0
def _validate_uniquerules(rules):
    pairs = []
    for r in rules:
        if 'source' not in r or 'destination' not in r:
            continue
        pairs.append((r['source'], r['destination']))

    if len(set(pairs)) != len(pairs):
        error = _("Duplicate router rules (src,dst)  found '%s'") % pairs
        LOG.debug(error)
        raise qexception.InvalidInput(error_message=error)
Ejemplo n.º 18
0
    def update_port(self, context, id, port):
        p = super(RyuQuantumPluginV2, self).update_port(context, id, port)
        net_id = p['network_id']
        mac_address = p['mac_address']

        deleted = port['port'].get('deleted', False)
        if deleted:
            session = context.session
            try:
                db_api_v2.port_binding_destroy(session, id, net_id)
            except q_exc.PortNotFound:
                pass
            db_api_v2.set_port_status(session, id, q_const.PORT_STATUS_DOWN)
            return p

        datapath_id = port['port'].get('datapath_id', None)
        port_no = port['port'].get('port_no', None)
        if datapath_id is None or port_no is None:
            LOG.debug('p %s', p)
            return p

        try:
            port_binding = db_api_v2.port_binding_get(id, net_id)
        except orm_exc.NoResultFound:
            try:
                db_api_v2.port_binding_create(id, net_id, datapath_id, port_no)
            except IntegrityError:
                # TODO:XXX should do transaction?
                return p
            else:
                self.client.create_port(net_id, datapath_id, port_no)
                self.client.create_mac(net_id, datapath_id, port_no,
                                       mac_address)
        else:
            if (port_binding.dpid != datapath_id
                    or port_binding.port_no != port_no):
                variables = {
                    'datapath_id': datapath_id,
                    'port_no': port_no,
                    'port_binding_dpid': port_binding.dpid,
                    'port_binding_port_no': port_binding.port_no
                }
                raise q_exc.InvalidInput(
                    error_message=_('invalid (datapath_id, port_no) '
                                    'is requested'
                                    '(%(datapath_id)s, %(port_no)s), acutal'
                                    '(%(port_binding_dpid)s, '
                                    '%(port_binding_port_no)s)') % variables)
            self.client.update_network(net_id)
            self.client.update_port(net_id, datapath_id, port_no)
            self.client.update_mac(net_id, datapath_id, port_no, mac_address)
        return p
Ejemplo n.º 19
0
def convert_to_valid_router_rules(data):
    """
    Validates and converts router rules to the appropriate data structure
    Example argument = [{'source': 'any', 'destination': 'any',
                         'action':'deny'},
                        {'source': '1.1.1.1/32', 'destination': 'any',
                         'action':'permit',
                         'nexthops': ['1.1.1.254', '1.1.1.253']}
                       ]
    """
    V4ANY = '0.0.0.0/0'
    if not isinstance(data, list):
        emsg = _("Invalid data format for router rule: '%s'") % data
        LOG.debug(emsg)
        raise qexception.InvalidInput(error_message=emsg)
    _validate_uniquerules(data)
    rules = []
    expected_keys = ['source', 'destination', 'action']
    for rule in data:
        rule['nexthops'] = rule.get('nexthops', [])
        if not isinstance(rule['nexthops'], list):
            rule['nexthops'] = rule['nexthops'].split('+')

        src = V4ANY if rule['source'] == 'any' else rule['source']
        dst = V4ANY if rule['destination'] == 'any' else rule['destination']

        errors = [
            attr._verify_dict_keys(expected_keys, rule, False),
            attr._validate_subnet(dst),
            attr._validate_subnet(src),
            _validate_nexthops(rule['nexthops']),
            _validate_action(rule['action'])
        ]
        errors = [m for m in errors if m]
        if errors:
            LOG.debug(errors)
            raise qexception.InvalidInput(error_message=errors)
        rules.append(rule)
    return rules
Ejemplo n.º 20
0
    def _check_provider_update(self, context, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        physical_network = attrs.get(provider.PHYSICAL_NETWORK)
        segmentation_id = attrs.get(provider.SEGMENTATION_ID)

        network_type_set = attributes.is_attr_set(network_type)
        physical_network_set = attributes.is_attr_set(physical_network)
        segmentation_id_set = attributes.is_attr_set(segmentation_id)

        if not (network_type_set or physical_network_set
                or segmentation_id_set):
            return
        msg = _("Plugin does not support updating provider attributes")
        raise q_exc.InvalidInput(error_message=msg)
 def create_network(self, session, attrs):
     segmentation_id = attrs.get(provider.SEGMENTATION_ID)
     if attributes.is_attr_set(segmentation_id):
         physical_network = attrs.get(provider.PHYSICAL_NETWORK)
         if not attributes.is_attr_set(physical_network):
             msg = _("physical_network not provided")
             raise q_exc.InvalidInput(error_message=msg)
         self._db.reserve_specific_vlan(session, physical_network,
                                        segmentation_id)
     else:
         (physical_network,
          segmentation_id) = self._db.reserve_vlan(session)
         attrs[provider.SEGMENTATION_ID] = segmentation_id
         attrs[provider.PHYSICAL_NETWORK] = physical_network
Ejemplo n.º 22
0
    def _process_provider_create(self, context, session, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        network_type_set = attributes.is_attr_set(network_type)
        if not network_type_set:
            if self._tenant_network_type == constants.TYPE_NONE:
                raise q_exc.TenantNetworksDisabled()
            network_type = self._tenant_network_type
            attrs[provider.NETWORK_TYPE] = network_type

        if network_type not in self._network_providers_map:
            msg = _("Network type %s not supported") % network_type
            raise q_exc.InvalidInput(error_message=msg)
        p = self._network_providers_map[network_type]
        # Provider specific network creation
        p.create_network(session, attrs)
Ejemplo n.º 23
0
 def _process_port_binding_create(self, context, attrs):
     binding_profile = attrs.get(portbindings.PROFILE)
     binding_profile_set = attributes.is_attr_set(binding_profile)
     if not binding_profile_set:
         return self.vnic_type
     if constants.VNIC_TYPE in binding_profile:
         req_vnic_type = binding_profile[constants.VNIC_TYPE]
         if req_vnic_type in (constants.VIF_TYPE_DIRECT,
                              constants.VIF_TYPE_HOSTDEV):
             return req_vnic_type
         else:
             msg = _("invalid vnic_type on port_create")
     else:
         msg = _("vnic_type is not defined in port profile")
     raise q_exc.InvalidInput(error_message=msg)
    def create_network(self, session, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        segmentation_id = attrs.get(provider.SEGMENTATION_ID)
        if attributes.is_attr_set(segmentation_id):
            msg = _("segmentation_id specified "
                    "for %s network") % network_type
            raise q_exc.InvalidInput(error_message=msg)
        segmentation_id = constants.FLAT_VLAN_ID
        attrs[provider.SEGMENTATION_ID] = segmentation_id

        physical_network = attrs.get(provider.PHYSICAL_NETWORK)
        if not attributes.is_attr_set(physical_network):
            physical_network = self._db.reserve_flat_net(session)
            attrs[provider.PHYSICAL_NETWORK] = physical_network
        else:
            self._db.reserve_specific_flat_net(session, physical_network)
Ejemplo n.º 25
0
def convert_to_boolean(data):
    if isinstance(data, basestring):
        val = data.lower()
        if val == "true" or val == "1":
            return True
        if val == "false" or val == "0":
            return False
    elif isinstance(data, bool):
        return data
    elif isinstance(data, int):
        if data == 0:
            return False
        elif data == 1:
            return True
    msg = _("'%s' cannot be converted to boolean") % data
    raise q_exc.InvalidInput(error_message=msg)
Ejemplo n.º 26
0
    def _process_provider_create(self, context, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        physical_network = attrs.get(provider.PHYSICAL_NETWORK)
        segmentation_id = attrs.get(provider.SEGMENTATION_ID)

        network_type_set = attributes.is_attr_set(network_type)
        physical_network_set = attributes.is_attr_set(physical_network)
        segmentation_id_set = attributes.is_attr_set(segmentation_id)

        if not (network_type_set or physical_network_set or
                segmentation_id_set):
            return (None, None, None)

        # Authorize before exposing plugin details to client
        self._enforce_provider_set_auth(context, attrs)

        if not network_type_set:
            msg = _("provider:network_type required")
            raise q_exc.InvalidInput(error_message=msg)
        elif network_type == 'flat':
            if segmentation_id_set:
                msg = _("provider:segmentation_id specified for flat network")
                raise q_exc.InvalidInput(error_message=msg)
            else:
                segmentation_id = lconst.FLAT_VLAN_ID
        elif network_type == 'vlan':
            if not segmentation_id_set:
                msg = _("provider:segmentation_id required")
                raise q_exc.InvalidInput(error_message=msg)
            if segmentation_id < 1 or segmentation_id > 4094:
                msg = _("provider:segmentation_id out of range "
                        "(1 through 4094)")
                raise q_exc.InvalidInput(error_message=msg)
        else:
            msg = _("invalid provider:network_type %s" % network_type)
            raise q_exc.InvalidInput(error_message=msg)

        if physical_network_set:
            if physical_network not in self.network_vlan_ranges:
                msg = _("unknown provider:physical_network %s" %
                        physical_network)
                raise q_exc.InvalidInput(error_message=msg)
        elif 'default' in self.network_vlan_ranges:
            physical_network = 'default'
        else:
            msg = _("provider:physical_network required")
            raise q_exc.InvalidInput(error_message=msg)

        return (network_type, physical_network, segmentation_id)
Ejemplo n.º 27
0
    def _process_provider_create(self, context, attrs):
        network_type = self._get_attribute(attrs, provider.NETWORK_TYPE)
        physical_network = self._get_attribute(attrs,
                                               provider.PHYSICAL_NETWORK)
        segmentation_id = self._get_attribute(attrs, provider.SEGMENTATION_ID)

        if attributes.is_attr_set(network_type):
            segment = {api.NETWORK_TYPE: network_type,
                       api.PHYSICAL_NETWORK: physical_network,
                       api.SEGMENTATION_ID: segmentation_id}
            return self.type_manager.validate_provider_segment(segment)

        if (attributes.is_attr_set(attrs.get(provider.PHYSICAL_NETWORK)) or
            attributes.is_attr_set(attrs.get(provider.SEGMENTATION_ID))):
            msg = _("network_type required if other provider attributes "
                    "specified")
            raise exc.InvalidInput(error_message=msg)
Ejemplo n.º 28
0
    def _validate_subnet_cidr(self, network, new_subnet_cidr):
        """Validate the CIDR for a subnet.

        Verifies the specified CIDR does not overlap with the ones defined
        for the other subnets specified for this network.

        """
        for subnet in network.subnets:
            if (netaddr.IPSet([subnet.cidr]) &
                    netaddr.IPSet([new_subnet_cidr])):
                err_msg = ("Requested subnet with cidr: %s "
                           "for network: %s "
                           "overlaps with subnet: %s)" % (new_subnet_cidr,
                                                          network.id,
                                                          subnet.cidr))
                LOG.error(err_msg)
                raise q_exc.InvalidInput(error_message=err_msg)
 def _parse_network_vlan_ranges(self):
     self._network_vlan_ranges = {}
     for entry in cfg.CONF.HYPERV.network_vlan_ranges:
         entry = entry.strip()
         if ':' in entry:
             try:
                 physical_network, vlan_min, vlan_max = entry.split(':')
                 self._add_network_vlan_range(physical_network.strip(),
                                              int(vlan_min), int(vlan_max))
             except ValueError as ex:
                 msg = _(
                     "Invalid network VLAN range: "
                     "'%(range)s' - %(e)s. Agent terminated!"), \
                     {'range': entry, 'e': ex}
                 raise q_exc.InvalidInput(error_message=msg)
         else:
             self._add_network(entry)
     LOG.info(_("Network VLAN ranges: %s"), self._network_vlan_ranges)
Ejemplo n.º 30
0
    def _check_provider_update(self, context, attrs):
        network_type = attrs.get(provider.NETWORK_TYPE)
        physical_network = attrs.get(provider.PHYSICAL_NETWORK)
        segmentation_id = attrs.get(provider.SEGMENTATION_ID)

        network_type_set = attributes.is_attr_set(network_type)
        physical_network_set = attributes.is_attr_set(physical_network)
        segmentation_id_set = attributes.is_attr_set(segmentation_id)

        if not (network_type_set or physical_network_set or
                segmentation_id_set):
            return

        # Authorize before exposing plugin details to client
        self._enforce_set_auth(context, attrs, self.network_set)

        msg = _("plugin does not support updating provider attributes")
        raise q_exc.InvalidInput(error_message=msg)