def check_valid_uuid(uuid):
    id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
    try:
        js_v(uuid, id_schema)
        return True
    except js_e.ValidationError:
        return False
Exemple #2
0
def format_in(schema):
    #print "HEADERS :" + str(bottle.request.headers.items())
    try:
        format_type = bottle.request.headers.get('Content-Type', 'application/json')
        if 'application/json' in format_type:
            client_data = bottle.request.json
        elif 'application/yaml' in format_type:
            client_data = yaml.load(bottle.request.body)
            print json.dumps(client_data, indent=4) # ALF borrar
        elif 'application/xml' in format_type:
            bottle.abort(501, "Content-Type: application/xml not supported yet.")
        else:
            print 'Content-Type ' + str(format_type) + ' not supported.'
            bottle.abort(HTTP_Not_Acceptable, 'Content-Type ' + str(format_type) + ' not supported.')
            return
        #if client_data == None:
        #    bottle.abort(HTTP_Bad_Request, "Content error, empty")
        #    return
        #check needed_items

        js_v(client_data, schema)

        #print "input data: ", str(client_data)
        return client_data
    except yaml.YAMLError, exc:
        print "validate_in error, yaml exception ", exc
        error_pos = ""
        if hasattr(exc, 'problem_mark'):
            mark = exc.problem_mark
            error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
        bottle.abort(HTTP_Bad_Request, "Content error: Failed to parse Content-Type" + error_pos)
Exemple #3
0
def load_configuration(configuration_file):
    default_tokens ={'http_port':9090, 'http_host':'localhost'}
    try:
        #Check config file exists
        if not os.path.isfile(configuration_file):
            return (False, "Error: Configuration file '"+configuration_file+"' does not exists.")
            
        #Read file
        (return_status, code) = af.read_file(configuration_file)
        if not return_status:
            return (return_status, "Error loading configuration file '"+configuration_file+"': "+code)
        #Parse configuration file
        try:
            config = yaml.load(code)
        except yaml.YAMLError, exc:
            error_pos = ""
            if hasattr(exc, 'problem_mark'):
                mark = exc.problem_mark
                error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
            return (False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": content format error: Failed to parse yaml format")

        #Validate configuration file with the config_schema
        try:
            js_v(config, config_schema)
        except js_e.ValidationError, exc:
            error_pos = ""
            if len(exc.path)>0: error_pos=" at '" + ":".join(map(str, exc.path))+"'"
            return False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": "+exc.message 
Exemple #4
0
def load_configuration(configuration_file):
    default_tokens ={'http_port':9080, 'http_host':'localhost', 
                     'of_controller_nets_with_same_vlan':True,
                     'image_path':'/opt/VNF/images'
            }
    try:
        #First load configuration from configuration file
        #Check config file exists
        if not os.path.isfile(configuration_file):
            return (False, 'Error: Configuration file '+configuration_file+' does not exists.')
            
        #Read and parse file
        (return_status, code) = af.read_file(configuration_file)
        if not return_status:
            return (return_status, "Error loading configuration file '"+configuration_file+"': "+code)
        try:
            config = yaml.load(code)
        except yaml.YAMLError, exc:
            error_pos = ""
            if hasattr(exc, 'problem_mark'):
                mark = exc.problem_mark
                error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
            return (False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": content format error: Failed to parse yaml format")
        
        
        try:
            js_v(config, config_schema)
        except js_e.ValidationError, exc:
            error_pos = ""
            if len(exc.path)>0: error_pos=" at '" + ":".join(map(str, exc.path))+"'"
            return False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": "+exc.message 
def check_valid_uuid(uuid):
    id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
    try:
        js_v(uuid, id_schema)
        return True
    except js_e.ValidationError:
        return False
Exemple #6
0
def load_configuration(configuration_file):
    default_tokens ={'http_port':9080, 'http_host':'localhost', 
                     'of_controller_nets_with_same_vlan':True,
                     'image_path':'/opt/VNF/images',
                     'network_vlan_range_start':1000,
                     'network_vlan_range_end': 4096
            }
    try:
        #First load configuration from configuration file
        #Check config file exists
        if not os.path.isfile(configuration_file):
            return (False, 'Error: Configuration file '+configuration_file+' does not exists.')
            
        #Read and parse file
        (return_status, code) = af.read_file(configuration_file)
        if not return_status:
            return (return_status, "Error loading configuration file '"+configuration_file+"': "+code)
        try:
            config = yaml.load(code)
        except yaml.YAMLError, exc:
            error_pos = ""
            if hasattr(exc, 'problem_mark'):
                mark = exc.problem_mark
                error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
            return (False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": content format error: Failed to parse yaml format")
        
        
        try:
            js_v(config, config_schema)
        except js_e.ValidationError, exc:
            error_pos = ""
            if len(exc.path)>0: error_pos=" at '" + ":".join(map(str, exc.path))+"'"
            return False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": "+exc.message 
Exemple #7
0
def load_configuration(configuration_file):
    default_tokens = {'http_port': 9090, 'http_host': 'localhost'}
    try:
        #Check config file exists
        if not os.path.isfile(configuration_file):
            return (False, "Error: Configuration file '" + configuration_file +
                    "' does not exists.")

        #Read file
        (return_status, code) = af.read_file(configuration_file)
        if not return_status:
            return (return_status, "Error loading configuration file '" +
                    configuration_file + "': " + code)
        #Parse configuration file
        try:
            config = yaml.load(code)
        except yaml.YAMLError, exc:
            error_pos = ""
            if hasattr(exc, 'problem_mark'):
                mark = exc.problem_mark
                error_pos = " at position: (%s:%s)" % (mark.line + 1,
                                                       mark.column + 1)
            return (False, "Error loading configuration file '" +
                    configuration_file + "'" + error_pos +
                    ": content format error: Failed to parse yaml format")

        #Validate configuration file with the config_schema
        try:
            js_v(config, config_schema)
        except js_e.ValidationError, exc:
            error_pos = ""
            if len(exc.path) > 0:
                error_pos = " at '" + ":".join(map(str, exc.path)) + "'"
            return False, "Error loading configuration file '" + configuration_file + "'" + error_pos + ": " + exc.message
Exemple #8
0
def format_in(default_schema, version_fields=None, version_dict_schema=None):
    ''' Parse the content of HTTP request against a json_schema
        Parameters
            default_schema: The schema to be parsed by default if no version field is found in the client data
            version_fields: If provided it contains a tuple or list with the fields to iterate across the client data to obtain the version
            version_dict_schema: It contains a dictionary with the version as key, and json schema to apply as value
                It can contain a None as key, and this is apply if the client data version does not match any key 
        Return:
            user_data, used_schema: if the data is successfully decoded and matches the schema
            launch a bottle abort if fails
    '''
    #print "HEADERS :" + str(bottle.request.headers.items())
    try:
        format_type = bottle.request.headers.get('Content-Type', 'application/json')
        if 'application/json' in format_type:
            client_data = bottle.request.json
        elif 'application/yaml' in format_type:
            client_data = yaml.load(bottle.request.body)
            print json.dumps(client_data, indent=4) # ALF borrar
        elif 'application/xml' in format_type:
            bottle.abort(501, "Content-Type: application/xml not supported yet.")
        else:
            print 'Content-Type ' + str(format_type) + ' not supported.'
            bottle.abort(HTTP_Not_Acceptable, 'Content-Type ' + str(format_type) + ' not supported.')
            return
        #if client_data == None:
        #    bottle.abort(HTTP_Bad_Request, "Content error, empty")
        #    return

        #look for the client provider version
        client_version = None
        used_schema = None
        if version_fields != None:
            client_version = client_data
            for field in version_fields:
                if field in client_version:
                    client_version = client_version[field]
                else:
                    client_version=None
                    break
        if client_version==None:
            used_schema=default_schema
        elif version_dict_schema!=None:
            if client_version in version_dict_schema:
                used_schema = version_dict_schema[client_version]
            elif None in version_dict_schema:
                used_schema = version_dict_schema[None]
        if used_schema==None:
            bottle.abort(HTTP_Bad_Request, "Invalid schema version or missing version field")
            
        js_v(client_data, used_schema)
        return client_data, used_schema
    except yaml.YAMLError, exc:
        print "validate_in error, yaml exception ", exc
        error_pos = ""
        if hasattr(exc, 'problem_mark'):
            mark = exc.problem_mark
            error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
        bottle.abort(HTTP_Bad_Request, "Content error: Failed to parse Content-Type" + error_pos)
def format_in(http_response, schema):
    try:
        client_data = http_response.json()
        js_v(client_data, schema)
        # print "Input data: ", str(client_data)
        return True, client_data
    except js_e.ValidationError, exc:
        print "validate_in error, jsonschema exception ", exc.message, "at", exc.path
        return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path)
Exemple #10
0
def format_in(http_response, schema):
    try:
        client_data = http_response.json()
        js_v(client_data, schema)
        #print "Input data: ", str(client_data)
        return True, client_data
    except js_e.ValidationError, exc:
        print "validate_in error, jsonschema exception ", exc.message, "at", exc.path
        return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path)
Exemple #11
0
def load_configuration(configuration_file):
    default_tokens = {
        'http_port': 9090,
        'http_host': 'localhost',
        'http_console_proxy': True,
        'http_console_host': None,
        'log_level': 'DEBUG',
        'log_socket_port': 9022,
        'auto_push_VNF_to_VIMs': True,
        'db_host': 'localhost',
        'db_ovim_host': 'localhost'
    }
    try:
        # Check config file exists
        with open(configuration_file, 'r') as f:
            config_str = f.read()
        # Parse configuration file
        config = yaml.load(config_str, Loader=yaml.SafeLoader)
        # Validate configuration file with the config_schema
        js_v(config, config_schema)

        # Add default values tokens
        for k, v in default_tokens.items():
            if k not in config:
                config[k] = v
        return config

    except yaml.YAMLError as e:
        error_pos = ""
        if isinstance(e, MarkedYAMLError):
            mark = e.problem_mark
            error_pos = " at line:{} column:{}".format(mark.line + 1,
                                                       mark.column + 1)
        raise LoadConfigurationException(
            "Bad YAML format at configuration file '{file}'{pos}: {message}".
            format(file=configuration_file, pos=error_pos, message=e))
    except js_e.ValidationError as e:
        error_pos = ""
        if e.path:
            error_pos = " at '" + ":".join(map(str, e.path)) + "'"
        raise LoadConfigurationException(
            "Invalid field at configuration file '{file}'{pos} {message}".
            format(file=configuration_file, pos=error_pos, message=e))
    except Exception as e:
        raise LoadConfigurationException(
            "Cannot load configuration file '{file}' {message}".format(
                file=configuration_file, message=e))
Exemple #12
0
    def _check_descriptor(self, filePath):
        """
        Check whether the descriptor provided in the filepath is valid and convert into a json string.
        This method has been taken from OpenMANO and a little modified.
        We should not reinvent the wheel.

        :param filePath: File path of the descriptor.
        :return: The descriptor as a JSON String.
        """

        readFile = file(filePath, "r")
        descriptor = readFile.read()
        readFile.close()

        if 'topology' in descriptor:
            self.logger.info("This is a network scenario description")
            # TODO : convert the yaml file to json and verify if the structure of the json file match the nsd schema
            # convert the yaml string into json string
            nsd = yaml.load(descriptor)
            self.logger.debug(nsd)
            try:
                js_v(nsd, nsd_schema)
                # pass

            except js_vError as e:
                self.logger.debug("Parsing: Error validation, make sure that your file contains " \
                                  "all required fields or that the description file corresponds to the schema and vis versa")
                self.logger.debug(e)
                return -1, None
            return 1, nsd
        else:
            if 'vnf' in descriptor:
                self.logger.debug("This is a vnf descriptor")
                vnfd = yaml.load(descriptor)
                try:
                    js_v(vnfd, vnfd_schema_v01)
                    # pass
                except js_vError as e:
                    self.logger.debug("Parsing: Error validation, make sure that your file contains " \
                                      "all required fields or that the description file corresponds to the schema and vis versa")
                    self.logger.debug(e)
                    return -1, None
                return 2, vnfd
            else:
                self.logger.debug("Parsing: This is a wrong file, please give the right one")
                return -1, None
Exemple #13
0
def load_configuration(configuration_file):
    default_tokens = {
        "http_port": 9080,
        "http_host": "localhost",
        "of_controller_nets_with_same_vlan": True,
        "image_path": "/opt/VNF/images",
    }
    try:
        # First load configuration from configuration file
        # Check config file exists
        if not os.path.isfile(configuration_file):
            return (False, "Error: Configuration file " + configuration_file + " does not exists.")

        # Read and parse file
        (return_status, code) = af.read_file(configuration_file)
        if not return_status:
            return (return_status, "Error loading configuration file '" + configuration_file + "': " + code)
        try:
            config = yaml.load(code)
        except yaml.YAMLError, exc:
            error_pos = ""
            if hasattr(exc, "problem_mark"):
                mark = exc.problem_mark
                error_pos = " at position: (%s:%s)" % (mark.line + 1, mark.column + 1)
            return (
                False,
                "Error loading configuration file '"
                + configuration_file
                + "'"
                + error_pos
                + ": content format error: Failed to parse yaml format",
            )

        try:
            js_v(config, config_schema)
        except js_e.ValidationError, exc:
            error_pos = ""
            if len(exc.path) > 0:
                error_pos = " at '" + ":".join(map(str, exc.path)) + "'"
            return (
                False,
                "Error loading configuration file '" + configuration_file + "'" + error_pos + ": " + exc.message,
            )
Exemple #14
0
def validate_input(indata, schema_to_use):
    """
    Validates input data against json schema
    :param indata: user input data. Should be a dictionary
    :param schema_to_use: jsonschema to test
    :return: None if ok, raises ValidationError exception on error
    """
    try:
        if schema_to_use:
            js_v(indata, schema_to_use)
        return None
    except js_e.ValidationError as e:
        if e.path:
            error_pos = "at '" + ":".join(map(str, e.path)) + "'"
        else:
            error_pos = ""
        raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
    except js_e.SchemaError:
        raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
Exemple #15
0
def format_in(default_schema, version_fields=None, version_dict_schema=None):
    ''' Parse the content of HTTP request against a json_schema
        Parameters
            default_schema: The schema to be parsed by default if no version field is found in the client data
            version_fields: If provided it contains a tuple or list with the fields to iterate across the client data to obtain the version
            version_dict_schema: It contains a dictionary with the version as key, and json schema to apply as value
                It can contain a None as key, and this is apply if the client data version does not match any key 
        Return:
            user_data, used_schema: if the data is successfully decoded and matches the schema
            launch a bottle abort if fails
    '''
    #print "HEADERS :" + str(bottle.request.headers.items())
    try:
        error_text = "Invalid header format "
        format_type = bottle.request.headers.get('Content-Type', 'application/json')
        if 'application/json' in format_type:
            error_text = "Invalid json format "
            #Use the json decoder instead of bottle decoder because it informs about the location of error formats with a ValueError exception
            client_data = json.load(bottle.request.body)
            #client_data = bottle.request.json()
        elif 'application/yaml' in format_type:
            error_text = "Invalid yaml format "
            client_data = yaml.load(bottle.request.body)
        elif 'application/xml' in format_type:
            bottle.abort(501, "Content-Type: application/xml not supported yet.")
        else:
            print 'Content-Type ' + str(format_type) + ' not supported.'
            bottle.abort(HTTP_Not_Acceptable, 'Content-Type ' + str(format_type) + ' not supported.')
            return
        #if client_data == None:
        #    bottle.abort(HTTP_Bad_Request, "Content error, empty")
        #    return

        #look for the client provider version
        error_text = "Invalid content "
        client_version = None
        used_schema = None
        if version_fields != None:
            client_version = client_data
            for field in version_fields:
                if field in client_version:
                    client_version = client_version[field]
                else:
                    client_version=None
                    break
        if client_version==None:
            used_schema=default_schema
        elif version_dict_schema!=None:
            if client_version in version_dict_schema:
                used_schema = version_dict_schema[client_version]
            elif None in version_dict_schema:
                used_schema = version_dict_schema[None]
        if used_schema==None:
            bottle.abort(HTTP_Bad_Request, "Invalid schema version or missing version field")
            
        js_v(client_data, used_schema)
        return client_data, used_schema
    except (ValueError, yaml.YAMLError) as exc:
        error_text += str(exc)
        print error_text 
        bottle.abort(HTTP_Bad_Request, error_text)
    except js_e.ValidationError as exc:
        print "validate_in error, jsonschema exception ", exc.message, "at", exc.path
        error_pos = ""
        if len(exc.path)>0: error_pos=" at " + ":".join(map(json.dumps, exc.path))
        bottle.abort(HTTP_Bad_Request, error_text + exc.message + error_pos)
def format_in(default_schema, version_fields=None, version_dict_schema=None):
    ''' Parse the content of HTTP request against a json_schema
        Parameters
            default_schema: The schema to be parsed by default if no version field is found in the client data
            version_fields: If provided it contains a tuple or list with the fields to iterate across the client data to obtain the version
            version_dict_schema: It contains a dictionary with the version as key, and json schema to apply as value
                It can contain a None as key, and this is apply if the client data version does not match any key 
        Return:
            user_data, used_schema: if the data is successfully decoded and matches the schema
            launch a bottle abort if fails
    '''
    #print "HEADERS :" + str(bottle.request.headers.items())
    try:
        error_text = "Invalid header format "
        format_type = bottle.request.headers.get('Content-Type', 'application/json')
        if 'application/json' in format_type:
            error_text = "Invalid json format "
            #Use the json decoder instead of bottle decoder because it informs about the location of error formats with a ValueError exception
            client_data = json.load(bottle.request.body)
            #client_data = bottle.request.json()
        elif 'application/yaml' in format_type:
            error_text = "Invalid yaml format "
            client_data = yaml.load(bottle.request.body)
        elif 'application/xml' in format_type:
            bottle.abort(501, "Content-Type: application/xml not supported yet.")
        else:
            print 'Content-Type ' + str(format_type) + ' not supported.'
            bottle.abort(HTTP_Not_Acceptable, 'Content-Type ' + str(format_type) + ' not supported.')
            return
        #if client_data == None:
        #    bottle.abort(HTTP_Bad_Request, "Content error, empty")
        #    return

        #look for the client provider version
        error_text = "Invalid content "
        client_version = None
        used_schema = None
        if version_fields != None:
            client_version = client_data
            for field in version_fields:
                if field in client_version:
                    client_version = client_version[field]
                else:
                    client_version=None
                    break
        if client_version==None:
            used_schema=default_schema
        elif version_dict_schema!=None:
            if client_version in version_dict_schema:
                used_schema = version_dict_schema[client_version]
            elif None in version_dict_schema:
                used_schema = version_dict_schema[None]
        if used_schema==None:
            bottle.abort(HTTP_Bad_Request, "Invalid schema version or missing version field")
            
        js_v(client_data, used_schema)
        return client_data, used_schema
    except (ValueError, yaml.YAMLError) as exc:
        error_text += str(exc)
        print error_text 
        bottle.abort(HTTP_Bad_Request, error_text)
    except js_e.ValidationError as exc:
        print "validate_in error, jsonschema exception ", exc.message, "at", exc.path
        error_pos = ""
        if len(exc.path)>0: error_pos=" at " + ":".join(map(json.dumps, exc.path))
        bottle.abort(HTTP_Bad_Request, error_text + exc.message + error_pos)
Exemple #17
0
def format_in(default_schema,
              version_fields=None,
              version_dict_schema=None,
              confidential_data=False):
    """
    Parse the content of HTTP request against a json_schema

    :param default_schema: The schema to be parsed by default
        if no version field is found in the client data.
        In None no validation is done
    :param version_fields: If provided it contains a tuple or list with the
        fields to iterate across the client data to obtain the version
    :param version_dict_schema: It contains a dictionary with the version as key,
        and json schema to apply as value.
        It can contain a None as key, and this is apply
        if the client data version does not match any key
    :return:  user_data, used_schema: if the data is successfully decoded and
        matches the schema.

    Launch a bottle abort if fails
    """
    #print "HEADERS :" + str(bottle.request.headers.items())
    try:
        error_text = "Invalid header format "
        format_type = bottle.request.headers.get('Content-Type',
                                                 'application/json')
        if 'application/json' in format_type:
            error_text = "Invalid json format "
            #Use the json decoder instead of bottle decoder because it informs about the location of error formats with a ValueError exception
            client_data = json.load(bottle.request.body)
            #client_data = bottle.request.json()
        elif 'application/yaml' in format_type:
            error_text = "Invalid yaml format "
            client_data = yaml.load(bottle.request.body)
        elif 'application/xml' in format_type:
            bottle.abort(501,
                         "Content-Type: application/xml not supported yet.")
        else:
            logger.warning('Content-Type ' + str(format_type) +
                           ' not supported.')
            bottle.abort(
                httperrors.Not_Acceptable,
                'Content-Type ' + str(format_type) + ' not supported.')
            return
        # if client_data == None:
        #    bottle.abort(httperrors.Bad_Request, "Content error, empty")
        #    return
        if confidential_data:
            logger.debug(
                'IN: %s',
                remove_clear_passwd(
                    yaml.safe_dump(client_data,
                                   explicit_start=True,
                                   indent=4,
                                   default_flow_style=False,
                                   tags=False,
                                   encoding='utf-8',
                                   allow_unicode=True)))
        else:
            logger.debug(
                'IN: %s',
                yaml.safe_dump(client_data,
                               explicit_start=True,
                               indent=4,
                               default_flow_style=False,
                               tags=False,
                               encoding='utf-8',
                               allow_unicode=True))
        # look for the client provider version
        error_text = "Invalid content "
        if not default_schema and not version_fields:
            return client_data, None
        client_version = None
        used_schema = None
        if version_fields != None:
            client_version = client_data
            for field in version_fields:
                if field in client_version:
                    client_version = client_version[field]
                else:
                    client_version = None
                    break
        if client_version == None:
            used_schema = default_schema
        elif version_dict_schema != None:
            if client_version in version_dict_schema:
                used_schema = version_dict_schema[client_version]
            elif None in version_dict_schema:
                used_schema = version_dict_schema[None]
        if used_schema == None:
            bottle.abort(httperrors.Bad_Request,
                         "Invalid schema version or missing version field")

        js_v(client_data, used_schema)
        return client_data, used_schema
    except (TypeError, ValueError, yaml.YAMLError) as exc:
        error_text += str(exc)
        logger.error(error_text)
        bottle.abort(httperrors.Bad_Request, error_text)
    except js_e.ValidationError as exc:
        logger.error("validate_in error, jsonschema exception")
        error_pos = ""
        if len(exc.path) > 0:
            error_pos = " at " + ":".join(map(json.dumps, exc.path))
        bottle.abort(httperrors.Bad_Request,
                     error_text + exc.message + error_pos)