def test_validate_mac_address(self): mac_addr = "ff:16:3e:4f:00:00" msg = attributes._validate_mac_address(mac_addr) self.assertIsNone(msg) mac_addr = "ffa:16:3e:4f:00:00" msg = attributes._validate_mac_address(mac_addr) self.assertEqual(msg, "'%s' is not a valid MAC address" % mac_addr)
def _validate_allowed_address_pairs(address_pairs, valid_values=None): unique_check = {} for address_pair in address_pairs: # mac_address is optional, if not set we use the mac on the port if 'mac_address' in address_pair: msg = attr._validate_mac_address(address_pair['mac_address']) if msg: raise webob.exc.HTTPBadRequest(msg) if 'ip_address' not in address_pair: raise AllowedAddressPairsMissingIP() mac = address_pair.get('mac_address') ip_address = address_pair['ip_address'] if (mac, ip_address) not in unique_check: unique_check[(mac, ip_address)] = None else: raise DuplicateAddressPairInRequest(mac_address=mac, ip_address=ip_address) invalid_attrs = set(address_pair.keys()) - set( ['mac_address', 'ip_address']) if invalid_attrs: msg = (_("Unrecognized attribute(s) '%s'") % ', '.join( set(address_pair.keys()) - set(['mac_address', 'ip_address']))) raise webob.exc.HTTPBadRequest(msg) if '/' in ip_address: msg = attr._validate_subnet(ip_address) else: msg = attr._validate_ip_address(ip_address) if msg: raise webob.exc.HTTPBadRequest(msg)
def _validate_allowed_address_pairs(address_pairs, valid_values=None): unique_check = {} for address_pair in address_pairs: # mac_address is optional, if not set we use the mac on the port if 'mac_address' in address_pair: msg = attr._validate_mac_address(address_pair['mac_address']) if msg: raise webob.exc.HTTPBadRequest(msg) if 'ip_address' not in address_pair: raise AllowedAddressPairsMissingIP() mac = address_pair.get('mac_address') ip_address = address_pair['ip_address'] if (mac, ip_address) not in unique_check: unique_check[(mac, ip_address)] = None else: raise DuplicateAddressPairInRequest(mac_address=mac, ip_address=ip_address) invalid_attrs = set(address_pair.keys()) - set(['mac_address', 'ip_address']) if invalid_attrs: msg = (_("Unrecognized attribute(s) '%s'") % ', '.join(set(address_pair.keys()) - set(['mac_address', 'ip_address']))) raise webob.exc.HTTPBadRequest(msg) if '/' in ip_address: msg = attr._validate_subnet(ip_address) else: msg = attr._validate_ip_address(ip_address) if msg: raise webob.exc.HTTPBadRequest(msg)
def _validate_allowed_address_pairs(address_pairs, valid_values=None): unique_check = {} if len(address_pairs) > cfg.CONF.max_allowed_address_pair: raise AllowedAddressPairExhausted(quota=cfg.CONF.max_allowed_address_pair) for address_pair in address_pairs: # mac_address is optional, if not set we use the mac on the port if "mac_address" in address_pair: msg = attr._validate_mac_address(address_pair["mac_address"]) if msg: raise webob.exc.HTTPBadRequest(msg) if "ip_address" not in address_pair: raise AllowedAddressPairsMissingIP() mac = address_pair.get("mac_address") ip_address = address_pair["ip_address"] if (mac, ip_address) not in unique_check: unique_check[(mac, ip_address)] = None else: raise DuplicateAddressPairInRequest(mac_address=mac, ip_address=ip_address) invalid_attrs = set(address_pair.keys()) - set(["mac_address", "ip_address"]) if invalid_attrs: msg = _("Unrecognized attribute(s) '%s'") % ", ".join( set(address_pair.keys()) - set(["mac_address", "ip_address"]) ) raise webob.exc.HTTPBadRequest(msg) if "/" in ip_address: msg = attr._validate_subnet(ip_address) else: msg = attr._validate_ip_address(ip_address) if msg: raise webob.exc.HTTPBadRequest(msg)
def _validate_destination(instance_ctx, valid_values=None): expected_keys = ['destination_flag', 'destination_mac'] if not isinstance(instance_ctx, dict): msg = _("'%s' is not a dict") % instance_ctx LOG.debug(msg) return msg for port_key in instance_ctx: if port_key not in expected_keys: msg = (_("'%(data)s' is not in %(valid_values)s") % { 'data': port_key, 'valid_values': expected_keys }) LOG.debug(msg) return msg if instance_ctx.has_key('destination_flag'): msg = attr._validate_values(instance_ctx['destination_flag'], ['packet', 'chain', 'default']) if msg: msg = _("'%s' must be 'packet', 'chain' or 'default'" ) % instance_ctx['destination_flag'] LOG.debug(msg) return msg if instance_ctx['destination_flag'] == 'chain': if not instance_ctx.has_key('destination_mac'): msg = _("'%s' must has key destination_mac") % instance_ctx LOG.debug(msg) return msg else: if instance_ctx.has_key('destination_mac'): msg = _( "'%s', if destination_flag != chain, you should not input destination_mac" ) % instance_ctx LOG.debug(msg) return msg if instance_ctx.has_key('destination_mac'): msg = attr._validate_mac_address(instance_ctx['destination_mac']) if msg: msg = _("'%s' is not a valid MAC address" ) % instance_ctx['destination_mac'] LOG.debug(msg) return msg
def _validate_allowed_address_pairs(address_pairs, valid_values=None): unique_check = {} if len(address_pairs) > cfg.CONF.max_allowed_address_pair: raise AllowedAddressPairExhausted( quota=cfg.CONF.max_allowed_address_pair) for address_pair in address_pairs: # mac_address is optional, if not set we use the mac on the port if 'mac_address' in address_pair: msg = attr._validate_mac_address(address_pair['mac_address']) if msg: raise webob.exc.HTTPBadRequest(msg) if 'ip_address' not in address_pair: raise AllowedAddressPairsMissingIP() mac = address_pair.get('mac_address') ip_address = address_pair['ip_address'] if (mac, ip_address) not in unique_check: unique_check[(mac, ip_address)] = None else: raise DuplicateAddressPairInRequest(mac_address=mac, ip_address=ip_address) invalid_attrs = set(address_pair.keys()) - set(['mac_address', 'ip_address']) if invalid_attrs: msg = (_("Unrecognized attribute(s) '%s'") % ', '.join(set(address_pair.keys()) - set(['mac_address', 'ip_address']))) raise webob.exc.HTTPBadRequest(msg) if (netaddr.IPNetwork(ip_address).prefixlen == 0): raise AllowedAddressPairsZeroPrefixNotAllowed() elif '/' in ip_address: msg = attr._validate_subnet(ip_address) else: msg = attr._validate_ip_address(ip_address) if msg: raise webob.exc.HTTPBadRequest(msg)
def _validate_instance_ctx(instance_ctx, valid_values=None): expected_keys = [ 'user_side_port', 'network_side_port', 'classification_type', 'user_side_action', 'network_side_action' ] action_choose = ["vlan", "dmac"] type_choose = ['dl_src', '5tuple'] for port_key in instance_ctx: if port_key not in expected_keys: msg = (_("'%(data)s' is not in %(valid_values)s") % { 'data': port_key, 'valid_values': expected_keys }) LOG.debug(msg) return msg for port_key in ['user_side_port', 'network_side_port']: if instance_ctx.has_key(port_key) and \ not uuidutils.is_uuid_like(instance_ctx[port_key]): msg = _("'%s' is not a valid UUID") % port_key LOG.debug(msg) return msg for port_key in ['user_side_action', 'network_side_action']: if instance_ctx.has_key(port_key): if not isinstance(instance_ctx[port_key], dict): msg = _("'%s' is not a dictionary") % (instance_ctx[port_key]) LOG.debug(msg) return msg else: for key in instance_ctx[port_key]: if key not in action_choose: msg = (_("'%(data)s' is not in %(valid_values)s") % { 'data': key, 'valid_values': action_choose }) LOG.debug(msg) return msg elif key == 'dmac': msg = attr._validate_mac_address( instance_ctx[port_key][key]) if msg: msg = _("'%s' is not a valid MAC address" ) % instance_ctx[port_key][key] LOG.debug(msg) return msg elif key == 'vlan': try: if int(instance_ctx[port_key][key]) < 0 or int( instance_ctx[port_key][key]) > 4095: msg = (_( "vlan tag '%(data)s' is not a valid num in %(valid_values)s" ) % { 'data': instance_ctx[port_key][key], 'valid_values': '[0-4095]' }) LOG.debug(msg) return msg except: msg = (_("vlan tag should be number")) LOG.debug(msg) return msg for port_key in ['classification_type']: if instance_ctx.has_key( port_key) and instance_ctx[port_key] not in type_choose: msg = (_( "classification type '%(data)s' is not a valid type ,it should in %(valid_values)s" ) % { 'data': instance_ctx[port_key], 'valid_values': type_choose }) LOG.debug(msg) return msg