Example #1
0
def prepare_data(patch, patched_row_json, resource, resource_update, schema,
                 idl):

    # Verify final transaction data
    verified_data = verify.verify_data(patched_row_json, resource, schema, idl,
                                       REQUEST_TYPE_PATCH)

    app_log.debug("Verified data pre- move/remove fix-> %s" % verified_data)

    # Removed columns need to be filled with
    # "empty" values before sending to IDL
    verified_data = refill_removed_columns(patch, verified_data,
                                           resource_update, schema)

    return verified_data
Example #2
0
def put_resource(data, resource, schema, txn, idl):

    # Allow PUT operation on System table
    if resource is None:
        raise MethodNotAllowed

    # We want to modify System table
    if resource.next is None:
        resource_update = resource
    else:
        while True:
            if resource.next.next is None:
                break
            resource = resource.next
        resource_update = resource.next

    app_log.debug("Resource = Table: %s Relation: %s Column: %s"
                  % (resource.table, resource.relation, resource.column))

    if resource_update is not None:
        app_log.debug("Resource to Update = Table: %s "
                      % resource_update.table)

    if verify.verify_http_method(resource, schema,
                                 REQUEST_TYPE_UPDATE) is False:
        raise MethodNotAllowed

    # verify data
    try:
        verified_data = verify.verify_data(data, resource, schema, idl,
                                           REQUEST_TYPE_UPDATE)
    except DataValidationFailed as e:
        app_log.debug(e)
        raise e

    # We want to modify System table
    if resource.next is None:
        updated_row = utils.update_row(resource, verified_data,
                                       schema, txn, idl)

    elif resource.relation == OVSDB_SCHEMA_CHILD:
        '''
        Updating row from a child table
        Example:
        /system/bridges: PUT is allowed when modifying the bridge child table
        '''
        # update row, populate it with data, add it as a reference to
        # the parent resource
        updated_row = utils.update_row(resource_update,
                                       verified_data, schema, txn, idl)

    elif resource.relation == OVSDB_SCHEMA_BACK_REFERENCE:
        '''
        In this case we only modify the data of the table, but we not modify
        the back reference.
        Example:
        /system/vrfs/vrf_default/bgp_routers: PUT allowed as we are
         modifying a back referenced resource
        '''
        # row for a back referenced item contains the parent's reference
        # in the verified data
        updated_row = utils.update_row(resource_update, verified_data,
                                       schema, txn, idl)

    elif resource.relation == OVSDB_SCHEMA_TOP_LEVEL:
        '''
        Updating row when we have a relationship with a top_level table
        Is not allowed to update the references in other tables.
        Example:
        /system/ports: PUT allowed as we are modifying Port to top level table
        '''
        updated_row = utils.update_row(resource_update, verified_data,
                                       schema, txn, idl)

    try:
        utils.exec_validators_with_resource(idl, schema, resource,
                                            REQUEST_TYPE_UPDATE)
    except ValidationError as e:
        app_log.debug("Custom validations failed:")
        app_log.debug(e.error)
        raise DataValidationFailed(e.error)

    result = txn.commit()
    return OvsdbTransactionResult(result)
Example #3
0
def post_resource(data, resource, schema, txn, idl):
    """
    /system/bridges: POST allowed as we are adding a new Bridge
                     to a child table
    /system/ports: POST allowed as we are adding a new Port to
                   top level table
    /system/vrfs/vrf_default/bgp_routers: POST allowed as we
                   are adding a back referenced resource
    /system/bridges/bridge_normal/ports: POST NOT allowed as we
    are attemtping to add a Port as a reference on bridge
    """

    if resource is None or resource.next is None:
        app_log.info("POST is not allowed on System table")
        raise MethodNotAllowed

    # get the last resource pair
    while True:
        if resource.next.next is None:
            break
        resource = resource.next

    if verify.verify_http_method(resource, schema, REQUEST_TYPE_CREATE) is False:
        raise MethodNotAllowed

    # verify data
    try:
        verified_data = verify.verify_data(data, resource, schema, idl, REQUEST_TYPE_CREATE)
    except DataValidationFailed as e:
        app_log.debug(e)
        raise e

    app_log.debug("adding new resource to " + resource.next.table + " table")

    if resource.relation == OVSDB_SCHEMA_CHILD:
        # create new row, populate it with data
        # add it as a reference to the parent resource
        new_row = utils.setup_new_row(resource.next, verified_data, schema, txn, idl)

        ref = schema.ovs_tables[resource.table].references[resource.column]
        if ref.kv_type:
            keyname = ref.column.keyname
            utils.add_kv_reference(verified_data[keyname], new_row, resource, idl)
        else:
            utils.add_reference(new_row, resource, idl)

    elif resource.relation == OVSDB_SCHEMA_BACK_REFERENCE:
        # row for a back referenced item contains the parent's reference
        # in the verified data
        new_row = utils.setup_new_row(resource.next, verified_data, schema, txn, idl)

    elif resource.relation == OVSDB_SCHEMA_TOP_LEVEL:
        new_row = utils.setup_new_row(resource.next, verified_data, schema, txn, idl)

        # a non-root table entry MUST be referenced elsewhere
        if OVSDB_SCHEMA_REFERENCED_BY in verified_data:
            for reference in verified_data[OVSDB_SCHEMA_REFERENCED_BY]:
                utils.add_reference(new_row, reference, idl)

    try:
        utils.exec_validators_with_resource(idl, schema, resource, REQUEST_TYPE_CREATE)
    except ValidationError as e:
        app_log.debug("Custom validations failed:")
        app_log.debug(e.error)
        raise DataValidationFailed(e.error)

    result = txn.commit()
    return OvsdbTransactionResult(result)
Example #4
0
def post_resource(data, resource, schema, txn, idl):
    """
    /system/bridges: POST allowed as we are adding a new Bridge
                     to a child table
    /system/ports: POST allowed as we are adding a new Port to
                   top level table
    /system/vrfs/vrf_default/bgp_routers: POST allowed as we
                   are adding a back referenced resource
    /system/bridges/bridge_normal/ports: POST NOT allowed as we
    are attemtping to add a Port as a reference on bridge
    """

    if resource is None or resource.next is None:
        app_log.info("POST is not allowed on System table")
        raise MethodNotAllowed

    # get the last resource pair
    while True:
        if resource.next.next is None:
            break
        resource = resource.next

    utils.update_resource_keys(resource.next, schema, idl,
                               data[OVSDB_SCHEMA_CONFIG])

    if verify.verify_http_method(resource, schema,
                                 REQUEST_TYPE_CREATE) is False:
        raise MethodNotAllowed

    # verify data
    try:
        verified_data = verify.verify_data(data, resource, schema, idl,
                                           REQUEST_TYPE_CREATE)
    except DataValidationFailed as e:
        app_log.debug(e)
        raise e

    app_log.debug("adding new resource to " + resource.next.table + " table")

    if resource.relation == OVSDB_SCHEMA_CHILD:
        # create new row, populate it with data
        # add it as a reference to the parent resource
        new_row = utils.setup_new_row_by_resource(resource.next, verified_data,
                                                  schema, txn, idl)

        ref = schema.ovs_tables[resource.table].references[resource.column]
        if ref.kv_type:
            keyname = ref.keyname
            utils.add_kv_reference(verified_data[keyname], new_row, resource,
                                   idl)
        else:
            utils.add_reference(new_row, resource, idl)

    elif resource.relation == OVSDB_SCHEMA_BACK_REFERENCE:
        # row for a back referenced item contains the parent's reference
        # in the verified data
        new_row = utils.setup_new_row_by_resource(resource.next, verified_data,
                                                  schema, txn, idl)

    elif resource.relation == OVSDB_SCHEMA_TOP_LEVEL:
        new_row = utils.setup_new_row_by_resource(resource.next, verified_data,
                                                  schema, txn, idl)

        # a non-root table entry MUST be referenced elsewhere
        if OVSDB_SCHEMA_REFERENCED_BY in verified_data:
            for reference in verified_data[OVSDB_SCHEMA_REFERENCED_BY]:
                utils.add_reference(new_row, reference, idl)

    try:
        utils.exec_validators_with_resource(idl, schema, resource,
                                            REQUEST_TYPE_CREATE)
    except ValidationError as e:
        app_log.debug("Custom validations failed:")
        app_log.debug(e.error)
        raise DataValidationFailed(e.error)

    index = utils.create_index(schema, verified_data, resource, new_row)
    result = txn.commit()

    return OvsdbTransactionResult(result, index)
Example #5
0
def put_resource(data, resource, schema, txn, idl):

    # Allow PUT operation on System table
    if resource is None:
        raise MethodNotAllowed

    # We want to modify System table
    if resource.next is None:
        resource_update = resource
    else:
        while True:
            if resource.next.next is None:
                break
            resource = resource.next
        resource_update = resource.next

    app_log.debug("Resource = Table: %s Relation: %s Column: %s" %
                  (resource.table, resource.relation, resource.column))

    utils.update_resource_keys(resource_update, schema, idl)

    if resource_update is None or resource_update.row is None or\
            verify.verify_http_method(resource,
                                      schema, REQUEST_TYPE_UPDATE) is False:
        raise MethodNotAllowed

    # verify data
    try:
        verified_data = verify.verify_data(data, resource, schema, idl,
                                           REQUEST_TYPE_UPDATE)
    except DataValidationFailed as e:
        app_log.debug(e)
        raise e

    # We want to modify System table
    if resource.next is None:
        updated_row = utils.update_row(resource_update, verified_data, schema,
                                       txn, idl)

    elif resource.relation == OVSDB_SCHEMA_CHILD:
        '''
        Updating row from a child table
        Example:
        /system/bridges: PUT is allowed when modifying the bridge child table
        '''
        # update row, populate it with data, add it as a reference to
        # the parent resource
        updated_row = utils.update_row(resource_update, verified_data, schema,
                                       txn, idl)

    elif resource.relation == OVSDB_SCHEMA_BACK_REFERENCE:
        '''
        In this case we only modify the data of the table, but we not modify
        the back reference.
        Example:
        /system/vrfs/vrf_default/bgp_routers: PUT allowed as we are
         modifying a back referenced resource
        '''
        # row for a back referenced item contains the parent's reference
        # in the verified data
        updated_row = utils.update_row(resource_update, verified_data, schema,
                                       txn, idl)

    elif resource.relation == OVSDB_SCHEMA_TOP_LEVEL:
        '''
        Updating row when we have a relationship with a top_level table
        Is not allowed to update the references in other tables.
        Example:
        /system/ports: PUT allowed as we are modifying Port to top level table
        '''
        updated_row = utils.update_row(resource_update, verified_data, schema,
                                       txn, idl)

    try:
        utils.exec_validators_with_resource(idl, schema, resource,
                                            REQUEST_TYPE_UPDATE)
    except ValidationError as e:
        app_log.debug("Custom validations failed:")
        app_log.debug(e.error)
        raise DataValidationFailed(e.error)

    result = txn.commit()
    return OvsdbTransactionResult(result)