コード例 #1
0
def generate_payload(translator, redis_connection, topic, vdu_uuid):
    """ Get the OSM related data

    Args:
        translator (object): The translator object
        redis_connection (object): The redis connection object
        topic (str): The kafka topic
        vdu_uuid (str): The VDU uuid in OpenStack

    Returns:
        dict: The mano-related data

    Raises:
        OsmInfoNotFound: if VDU uuid does not exist in OSM records
    """
    redis_key = compose_redis_key(topic, vdu_uuid, identifier_type='vdu')
    cached_value_bytes = redis_connection.get(name=redis_key)

    if cached_value_bytes is not None:
        # Load the relevant OSM-info entry from the redis
        record = json.loads(convert_bytes_to_str(cached_value_bytes))
        if record.get('status', 404) == 404:
            raise VduUuidMissRedis(
                "OSM data not found in Redis for the VDU uuid: `{}`".format(
                    vdu_uuid))
        mano_data = record.get('mano')
        logger.debug(
            "Load OSM entry for `{}` VDU uuid: `{}` from Redis".format(
                vdu_uuid))
    else:
        # Generate a standard structure for each metric
        mano_data = translator.get_translation(vdu_uuid)
        mano_data_len = len(mano_data)

        # Keep status in redis to highlight if a VDU record exists in OSM or not.
        # If VDU does not exist use status 404 and ignore it in the next redis read.
        if not mano_data_len:
            redis_record = {
                "status": 404
            }  # 404 means means that VDU uuid does not exist in OSM
        else:
            redis_record = {
                "status": 200,
                "mano": mano_data
            }  # 200 means VDU uuid exists in OSM
            logger.info(
                "Load OSM data for vCE VDU uuid: `{}` from OSM ".format(
                    vdu_uuid))

        # Save the entry in the Redis
        redis_connection.set(name=redis_key,
                             value=json.dumps(redis_record),
                             ex=REDIS_EXPIRATION_SECONDS)

        if not mano_data_len:
            raise OsmInfoNotFound(
                "OSM data not found in OSM API for the VDU uuid: `{}`".format(
                    vdu_uuid))

    return mano_data
コード例 #2
0
def get_vnf_uuid(redis_connection, stream, topic):
    """ Get the VNF uuid, if any

    Args:
        redis_connection (object): The redis connection object
        stream (dict): The stream
        topic (str): The Kafka topic

    Returns:
        str: the VNF uuid

    Raises:
        InvalidTranscoderId: if the transcoder ID is not valid
        VnfNotFound: if VNF uuid does not exist
    """
    transcoder_id = stream.get("transcoder_id", None)
    if transcoder_id is None:
        raise InvalidTranscoderId(
            'Invalid transcoder_id value. Its value is {}'.format(transcoder_id))

    try:
        if int(transcoder_id) == 0:
            raise InvalidTranscoderId('Invalid transcoder_id value. Its value is {}'.format(
                transcoder_id))
    except ValueError:
        pass

    search_for_transcoder_id = "{}:{}".format(topic, transcoder_id)
    cached_vnf_uuid_bytes = redis_connection.get(name=search_for_transcoder_id)

    if cached_vnf_uuid_bytes is not None:
        vnf_uuid = convert_bytes_to_str(cached_vnf_uuid_bytes)
    else:
        vnf_uuid = discover_vnf_uuid_by_vnfd_name_index(transcoder_id)
        if vnf_uuid is not None:
            redis_connection.set(name=search_for_transcoder_id,
                                 value="{}".format(vnf_uuid), ex=300)  # 5 minutes

    logger.debug("VNF is {} for id {}".format(vnf_uuid, transcoder_id))

    quality_id = stream.get("quality_id", None)
    if vnf_uuid is None or quality_id is None:
        raise VnfNotFound('The VNF uuid does not exist in the consumed message')
    vnf_uuid = vnf_uuid.replace(" ", "_")
    return vnf_uuid
コード例 #3
0
def get_vdu(redis_connection, topic, metric):
    """ Get the VDU by using the VNF index

    Args:
        redis_connection (object): Teh redis connection object
        topic (str): The kafka topic
        metric (dict): The incoming message

    Returns:
        str: the VDU uuid (aka container id)

    Raises:
        VnfIndexInvalid: if the VNF index is not valid or does not exist
        VduUuidDoesNotExist: if the VDU uuid does not exist
    """
    vdu_uuid = None
    vnf_index = metric.get("vdu_uuid", None)
    if vnf_index is None or int(vnf_index) == 0:
        raise VnfIndexInvalid(
            'Invalid vnf_index value. Its value is {}'.format(vnf_index))

    # Discover the vdu given the vnf index
    search_for_vnf_index = "{}:vnf_index_{}".format(topic, vnf_index)
    cached_vdu_uuid_bytes = redis_connection.get(name=search_for_vnf_index)

    if cached_vdu_uuid_bytes is not None:
        vdu_uuid = convert_bytes_to_str(cached_vdu_uuid_bytes)
    else:
        vdu_uuid = discover_vdu_uuid_by_vnf_index(vnf_index)
        redis_connection.set(name=search_for_vnf_index,
                             value="{}".format(vdu_uuid),
                             ex=300)  # 5 minutes

    logger.debug("VDU uuid is {} for id {}".format(vdu_uuid, vnf_index))

    if vdu_uuid is None:
        raise VduUuidDoesNotExist(
            'The VDU uuid for does not exist in the consumed message')
    return vdu_uuid