Пример #1
0
def test_subscription_with_time_index():
    actual = build_subscription(quantumleap_url='http://ql',
                                etype=None,
                                eid=None,
                                eid_pattern=None,
                                attributes=None,
                                observed_attributes=None,
                                notified_attributes=None,
                                throttling_secs=None,
                                time_index_attribute='my-time-index-attr-name')
    expected = {
        'description': 'Created by QuantumLeap http://ql.',
        'subject': {
            'entities': [{
                'idPattern': '.*'
            }]
        },
        'notification': {
            'httpCustom': {
                'url': 'http://ql/notify',
                'headers': {
                    TIME_INDEX_HEADER_NAME: 'my-time-index-attr-name'
                }
            },
            'metadata': [
                'dateCreated', 'dateModified', 'TimeInstant', 'timestamp',
                'my-time-index-attr-name'
            ]
        },
        'throttling': 1
    }

    assert actual == expected
Пример #2
0
def test_entity_id_overrides_pattern():
    actual = build_subscription(quantumleap_url='http://ql',
                                etype=None,
                                eid='e123',
                                eid_pattern='e1.*',
                                attributes=None,
                                observed_attributes=None,
                                notified_attributes=None,
                                throttling_secs=None)
    expected = {
        'description': 'Created by QuantumLeap http://ql.',
        'subject': {
            'entities': [{
                'id': 'e123'
            }]
        },
        'notification': {
            'http': {
                'url': 'http://ql/notify'
            },
            'metadata':
            ['dateCreated', 'dateModified', 'TimeInstant', 'timestamp']
        },
        'throttling': 1
    }

    assert actual == expected
Пример #3
0
def test_attributes_overrides_other_attributes(observed, notified):
    actual = build_subscription(quantumleap_url='http://ql',
                                etype=None,
                                eid=None,
                                eid_pattern=None,
                                attributes='x',
                                observed_attributes=observed,
                                notified_attributes=notified,
                                throttling_secs=None)
    expected = {
        'description': 'Created by QuantumLeap http://ql.',
        'subject': {
            'entities': [{
                'idPattern': '.*'
            }],
            'condition': {
                'attrs': ['x']
            }
        },
        'notification': {
            'http': {
                'url': 'http://ql/notify'
            },
            'attrs': ['x'],
            'metadata':
            ['dateCreated', 'dateModified', 'TimeInstant', 'timestamp']
        },
        'throttling': 1
    }

    assert actual == expected
Пример #4
0
def test_notified_attributes(attrs):
    actual = build_subscription(quantumleap_url='http://ql',
                                etype=None,
                                eid=None,
                                eid_pattern=None,
                                attributes=None,
                                observed_attributes=None,
                                notified_attributes=attrs,
                                throttling_secs=None)
    expected = {
        'description': 'Created by QuantumLeap http://ql.',
        'subject': {
            'entities': [{
                'idPattern': '.*'
            }]
        },
        'notification': {
            'http': {
                'url': 'http://ql/notify'
            },
            'attrs': attrs.split(','),
            'metadata':
            ['dateCreated', 'dateModified', 'TimeInstant', 'timestamp']
        },
        'throttling': 1
    }

    assert actual == expected
Пример #5
0
def test_entity_type():
    actual = build_subscription(quantumleap_url='http://ql',
                                etype='gauge',
                                eid=None,
                                eid_pattern=None,
                                attributes=None,
                                observed_attributes=None,
                                notified_attributes=None,
                                throttling_secs=None)
    expected = {
        'description': 'Created by QuantumLeap http://ql.',
        'subject': {
            'entities': [{
                'type': 'gauge',
                'idPattern': '.*'
            }]
        },
        'notification': {
            'http': {
                'url': 'http://ql/notify'
            },
            'metadata': ['dateCreated', 'dateModified']
        },
        'throttling': 1
    }

    assert actual == expected
Пример #6
0
def test_all_attributes():
    actual = build_subscription(quantumleap_url='http://ql',
                                etype=None,
                                eid=None,
                                eid_pattern=None,
                                attributes=None,
                                observed_attributes='a,b',
                                notified_attributes='b,c',
                                throttling_secs=None)
    expected = {
        'description': 'Created by QuantumLeap http://ql.',
        'subject': {
            'entities': [{
                'idPattern': '.*'
            }],
            'condition': {
                'attrs': ['a', 'b']
            }
        },
        'notification': {
            'http': {
                'url': 'http://ql/notify'
            },
            'attrs': ['b', 'c'],
            'metadata': ['dateCreated', 'dateModified']
        },
        'throttling': 1
    }

    assert actual == expected
Пример #7
0
def subscribe(orion_url,
              quantumleap_url,
              entity_type=None,
              entity_id=None,
              id_pattern=None,
              attributes=None,
              observed_attributes=None,
              notified_attributes=None,
              throttling=None,
              time_index_attribute=None):
    # Validate Orion
    log().warning("This API is deprecated, it will be removed in version 0.9")
    try:
        r = requests.get(orion_url)
    except RequestException:
        r = None
    if r is None or not r.ok:
        msg = {
            "error": "Bad Request",
            "description": "Orion is not reachable at {}".format(orion_url)
        }
        return msg, 400

    # Prepare subscription
    subscription = build_subscription(quantumleap_url, entity_type, entity_id,
                                      id_pattern, attributes,
                                      observed_attributes, notified_attributes,
                                      throttling, time_index_attribute)

    # Send subscription
    endpoint = '{}/subscriptions'.format(orion_url)
    data = json.dumps(subscription)

    headers = {'Content-Type': 'application/json'}
    fiware_s = request.headers.get('fiware-service', None)
    if fiware_s:
        headers['fiware-service'] = fiware_s

        fiware_sp = request.headers.get('fiware-servicepath', None)
        if fiware_sp:
            headers['fiware-servicepath'] = fiware_sp

    r = requests.post(endpoint, data=data, headers=headers)
    if not r.ok:
        log().debug("subscribing to {} with headers: {} and data: {}")

    msg = r.text + \
        " - This API is deprecated, it will be removed in version 0.9"
    return msg, r.status_code
Пример #8
0
def subscribe(orion_url,
              quantumleap_url,
              entity_type=None,
              entity_id=None,
              id_pattern=None,
              attributes=None,
              observed_attributes=None,
              notified_attributes=None,
              throttling=None):
    # Validate Orion
    try:
        r = requests.get(orion_url)
    except RequestException:
        r = None
    if r is None or not r.ok:
        msg = {
            "error":
            "Bad Request",
            "description":
            "Orion is not reachable by QuantumLeap at {}".format(orion_url)
        }
        return msg, 400

    # Prepare subscription
    subscription = build_subscription(quantumleap_url, entity_type, entity_id,
                                      id_pattern, attributes,
                                      observed_attributes, notified_attributes,
                                      throttling)

    # Send subscription
    endpoint = '{}/subscriptions'.format(orion_url)
    data = json.dumps(subscription)

    headers = {'Content-Type': 'application/json'}
    fiware_s = request.headers.get('fiware-service', None)
    if fiware_s:
        headers['fiware-service'] = fiware_s

        fiware_sp = request.headers.get('fiware-servicepath', None)
        if fiware_sp:
            headers['fiware-servicepath'] = fiware_sp

    r = requests.post(endpoint, data=data, headers=headers)
    if not r.ok:
        logger = logging.getLogger(__name__)
        logger.debug("subscribing to {} with headers: {} and data: {}")

    return r.text, r.status_code