Example #1
0
def clear_context_broker(url: str, fiware_header: FiwareHeader):
    """
    Function deletes all entities, registrations and subscriptions for a
    given fiware header

    Note:
        Always clear the devices first because the IoT-Agent will otherwise
        through errors if it cannot find its registration anymore.

    Args:
        url: Url of the context broker service
        fiware_header: header of the tenant

    Returns:
        None
    """
    # create client
    client = ContextBrokerClient(url=url, fiware_header=fiware_header)
    # clean entities
    client.delete_entities(entities=client.get_entity_list())

    # clear subscriptions
    for sub in client.get_subscription_list():
        client.delete_subscription(subscription_id=sub.id)
    assert len(client.get_subscription_list()) == 0

    # clear registrations
    for reg in client.get_registration_list():
        client.delete_registration(registration_id=reg.id)
    assert len(client.get_registration_list()) == 0
    except:
        logger.info("There might be no historical data for some calls.")

    # ## 2.3 Delete
    #
    # delete entity in QL
    try:
        ql_client.delete_entity(entity_id=hall_entity.id,
                                entity_type=hall_entity.type)
    except:
        logger.error("Can not delete data from QL")

    # delete entity in  CV
    try:
        cb_client.delete_entity(entity_id=hall_entity.id,
                                entity_type=hall_entity.type)
    except:
        logger.error("Can not delete entity from context broker")

    # delete subscription
    try:
        cb_client.delete_subscription(subscription_id)
    except:
        logger.error("Can not delete subscription from context broker.")

    # # 3 Clean up (Optional)
    #
    # Close clients
    ql_client.close()
    cb_client.close()