Adapt to O-RU use case spec
[nonrtric.git] / test / usecases / linkfailure / simulators / sdnc_simulator.py
1
2 #  ============LICENSE_START===============================================
3 #  Copyright (C) 2021 Nordix Foundation. All rights reserved.
4 #  ========================================================================
5 #  Licensed under the Apache License, Version 2.0 (the "License");
6 #  you may not use this file except in compliance with the License.
7 #  You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #  Unless required by applicable law or agreed to in writing, software
12 #  distributed under the License is distributed on an "AS IS" BASIS,
13 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #  See the License for the specific language governing permissions and
15 #  limitations under the License.
16 #  ============LICENSE_END=================================================
17 #
18
19 from flask import Flask
20 from flask import Response
21 import json
22 import random
23 import requests
24 import threading
25 import time
26
27 # Provides an endpoint for the "UNLOCK" configuration change for an O-DU.
28 # Stores the ID of the O-DU and randomly, after between 0 and 10 seconds, sends an Alarm Notification that clears the
29 # "CUS Link Failure" alarm event to MR.
30 app = Flask(__name__)
31
32 MR_PATH = "http://localhost:3904/events/unauthenticated.SEC_FAULT_OUTPUT"
33
34 # Server info
35 HOST_IP = "::"
36 HOST_PORT = 9990
37 APP_URL = "/rests/data/network-topology:network-topology/topology=topology-netconf/node=<string:o_du_id>/yang-ext:mount/o-ran-sc-du-hello-world:network-function/du-to-ru-connection=<string:o_ru_id>"
38
39 linkFailureMessage = {
40     "event": {
41         "commonEventHeader": {
42             "domain": "fault",
43             "eventId": "nt:network-topology/nt:topology/nt:node/nt:node-id",
44             "eventName": "fault_O-RAN-RU-Fault_Alarms_CUS_Link_Failure",
45             "eventType": "O-RAN-RU-Fault_Alarms",
46             "sequence": 0,
47             "priority": "High",
48             "reportingEntityId": "SDNR",
49             "reportingEntityName": "",
50             "sourceId": "",
51             "sourceName": "oru1",
52             "startEpochMicrosec": "@timestamp@",
53             "lastEpochMicrosec": "@timestamp@",
54             "nfNamingCode": "",
55             "nfVendorName": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-name",
56             "timeZoneOffset": "+00:00",
57             "version": "4.1",
58             "vesEventListenerVersion": "7.2.1"
59         },
60         "faultFields": {
61             "faultFieldsVersion": "4.0",
62             "alarmCondition": "30",
63             "alarmInterfaceA": "o-ran-fm:alarm-notif/fault-source",
64             "eventSourceType": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-model or \"O-RU\"",
65             "specificProblem": "",
66             "eventSeverity": "NORMAL",
67             "vfStatus": "Active",
68             "alarmAdditionalInformation": {
69                 "eventTime": "@eventTime@",
70                 "equipType": "@type@",
71                 "vendor": "@vendor@",
72                 "model": "@model@"
73             }
74         }
75     }
76 }
77
78
79 class AlarmClearThread (threading.Thread):
80
81     def __init__(self, sleep_time, o_ru_id):
82         threading.Thread.__init__(self)
83         self.sleep_time = sleep_time
84         self.o_ru_id = o_ru_id
85
86     def run(self):
87         print(f'Sleeping: {self.sleep_time} before clearing O-DU: {self.o_ru_id}')
88         time.sleep(self.sleep_time)
89         msg_as_json = json.loads(json.dumps(linkFailureMessage))
90         msg_as_json["event"]["commonEventHeader"]["sourceName"] = self.o_ru_id
91         print("Sedning alarm clear for O-RU: " + self.o_ru_id)
92         requests.post(MR_PATH, json=msg_as_json);
93
94
95 # I'm alive function
96 @app.route('/',
97     methods=['GET'])
98 def index():
99     return 'OK', 200
100
101
102 @app.route(APP_URL,
103     methods=['POST'])
104 def sendrequest(o_du_id, o_ru_id):
105     print("Got request with O-DU ID: " + o_du_id + " and O-RU ID: " + o_ru_id)
106     random_time = int(10 * random.random())
107     alarm_clear_thread = AlarmClearThread(random_time, o_ru_id)
108     alarm_clear_thread.start()
109
110     return Response(status=201)
111
112
113 if __name__ == "__main__":
114     app.run(port=HOST_PORT, host=HOST_IP)