Esempio n. 1
0
        def wrapped_fun(**kwargs):
            """Wraps a function to do validation before calling actual func.

            Wraps a function to take key-value args. only. Checks if:
            1) all required argument of wrapped function are provided
            2) no extra/un-known arguments are passed
            3) checks if validator for required arguments is available
            4) validates required arguments
            5) if validator for optional arguments is registered,
               validates optional arguments.
            Raises exception if no validator can be found for required args.
            """
            # Check if we are missing arguments.
            if not kwargs and len(self._req_args) > 0:
                raise MissingRequiredConf(desc='Missing all required '
                                          'attributes.')

            # Check if we have unknown arguments.
            given_args = set(kwargs.keys())
            unknown_attrs = given_args - set(self._all_args)
            if unknown_attrs:
                raise RuntimeConfigError(desc=('Unknown attributes %r' %
                                               unknown_attrs))

            # Check if required arguments are missing
            missing_req_args = set(self._req_args) - given_args
            if missing_req_args:
                conf_name = ', '.join(missing_req_args)
                raise MissingRequiredConf(conf_name=conf_name)

            #
            # Prepare to call wrapped function.
            #
            # Collect required arguments in the order asked and validate it.
            req_values = []
            for req_arg in self._req_args:
                req_value = kwargs.get(req_arg)
                # Validate required value.
                validator = get_validator(req_arg)
                if not validator:
                    raise ValueError('No validator registered for function=%s'
                                     ' and arg=%s' % (func, req_arg))
                validator(req_value)
                req_values.append(req_value)

            # Collect optional arguments.
            opt_items = {}
            for opt_arg, opt_value in kwargs.items():
                if opt_arg in self._opt_args:
                    # Validate optional value.
                    # Note: If no validator registered for optional value,
                    # skips validation.
                    validator = get_validator(opt_arg)
                    if validator:
                        validator(opt_value)
                    opt_items[opt_arg] = opt_value

            # Call actual function
            return func(*req_values, **opt_items)
Esempio n. 2
0
def validate_local_as(asn):
    if asn is None:
        raise MissingRequiredConf(conf_name=LOCAL_AS)

    if not is_valid_old_asn(asn):
        raise ConfigValueError(
            desc='Invalid local_as configuration value: %s' % asn)
    return asn
Esempio n. 3
0
def validate_router_id(router_id):
    if not router_id:
        raise MissingRequiredConf(conf_name=ROUTER_ID)

    if not isinstance(router_id, str):
        raise ConfigTypeError(conf_name=ROUTER_ID)
    if not is_valid_ipv4(router_id):
        raise ConfigValueError(desc='Invalid router id %s' % router_id)

    return router_id