Commit 3c10a72f authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

ECOC F5GA Telemetry Demo:

- Converted subscribe telemetry script to python
parent 8621a7b3
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
# Copyright 2022-2025 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import requests, websocket


RESTCONF_ADDRESS = '0.0.0.0'
RESTCONF_PORT = 80
TARGET_SIMAP_NAME = 'e2e'
TARGET_LINK_NAME  = 'E2E-L1'
SAMPLING_INTERVAL = 10.0


SUBSCRIBE_URI = '/restconf/operations/subscriptions:establish-subscription'
SUBSCRIBE_URL = 'http://{:s}:{:d}{:s}'.format(RESTCONF_ADDRESS, RESTCONF_PORT, SUBSCRIBE_URI)
XPATH_FILTER = '/ietf-network:networks/network={:s}/ietf-network-topology:link={:s}/simap-telemetry:simap-telemetry'
REQUEST = {
    'ietf-subscribed-notifications:input': {
        'datastore': 'operational',
        'ietf-yang-push:datastore-xpath-filter': XPATH_FILTER.format(TARGET_SIMAP_NAME, TARGET_LINK_NAME),
        'ietf-yang-push:periodic': {
            'ietf-yang-push:period': SAMPLING_INTERVAL
        }
    }
}


def on_open(ws):
    print('### Opened stream ###')

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print('### Closed stream ###')

def main() -> None:
    print('[E2E] Subscribe Telemetry slice1...')
    reply = requests.get(SUBSCRIBE_URL, json=REQUEST, allow_redirects=True)
    assert reply.is_json
    reply_data = reply.json()

    if 'uri' not in reply_data:
        raise Exception('Unexpected Reply: {:s}'.format(str(reply_data)))
    subscription_uri = reply_data['uri']

    stream_url = 'http://{:s}:{:d}{:s}'.format(RESTCONF_ADDRESS, RESTCONF_PORT, subscription_uri)
    print('Opening stream "{:s}" (press Ctrl+C to stop)...'.format(stream_url))

    ws = websocket.WebSocketApp(
        stream_url, on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close
    )
    ws.run_forever()

if __name__ == '__main__':
    main()