Skip to content
Snippets Groups Projects
utilities.py 2.20 KiB
#!/usr/bin/python3
"""
// © University of Southampton IT Innovation Centre, 2018
//
// Copyright in this software belongs to University of Southampton
// IT Innovation Centre of Gamma House, Enterprise Road,
// Chilworth Science Park, Southampton, SO16 7NS, UK.
//
// This software may not be used, sold, licensed, transferred, copied
// or reproduced in whole or in part in any manner or form or in or
// on any media by any person other than in accordance with the terms
// of the Licence Agreement supplied with the software, or otherwise
// without the prior written consent of the copyright owners.
//
// This software is distributed WITHOUT ANY WARRANTY, without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE, except where stated in the Licence Agreement supplied with
// the software.
//
//      Created By :            Nikolay Stanchev
//      Created Date :          02-07-2018
//      Created for Project :   FLAME
"""

from json import loads
from clmcservice.models import ServiceFunctionChain


def validate_sfchain_body(body):
    """
    Validates the request body used to create an service function chain resource in the database.

    :param body: the request body to validate
    :return the validated sfc dictionary object
    :raise AssertionError: if the body is not a valid service function chain
    """

    try:
        body = loads(body)
    except:
        raise AssertionError("Service function chain must be represented by a JSON object.")

    assert len(body) == len(ServiceFunctionChain.__table__.columns), "Service function chain JSON object mustn't contain a different number of attributes than the number of required ones."

    # validate that all required attributes are given in the body
    for attribute in ServiceFunctionChain.__table__.columns:
        assert attribute.name in body, "Required attribute not found in the request content."

    assert type(body["chain"]) == dict, "The chain attribute of a service function chain must be a graph representing the relations between service functions."

    for sf in body["chain"]:
        assert type(body["chain"][sf]) == list, "A list must be used to represent each dependency between service functions"

    return body