Change fault-ID for oruclosedlooprecovery usecase
[nonrtric.git] / test / usecases / oruclosedlooprecovery / scriptversion / simulators / sdnr_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 os
23 import random
24 import requests
25 import threading
26 import time
27
28 # Provides an endpoint for the "UNLOCK" configuration change for an O-DU.
29 # Stores the ID of the O-DU and randomly, after between 0 and 10 seconds, sends an Alarm Notification that clears the
30 # "CUS Link Failure" alarm event to MR.
31 app = Flask(__name__)
32
33 mr_host = "http://localhost"
34 mr_port = "3904"
35 MR_PATH = "/events/unauthenticated.SEC_FAULT_OUTPUT"
36
37 # Server info
38 HOST_IP = "::"
39 HOST_PORT = 9990
40 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>"
41
42 FAULT_ID = "28"
43
44 linkFailureMessage = {
45     "event": {
46         "commonEventHeader": {
47             "domain": "fault",
48             "eventId": "nt:network-topology/nt:topology/nt:node/nt:node-id",
49             "eventName": "fault_O-RAN-RU-Fault_Alarms_CUS_Link_Failure",
50             "eventType": "O-RAN-RU-Fault_Alarms",
51             "sequence": 0,
52             "priority": "High",
53             "reportingEntityId": "SDNR",
54             "reportingEntityName": "",
55             "sourceId": "",
56             "sourceName": "oru1",
57             "startEpochMicrosec": "@timestamp@",
58             "lastEpochMicrosec": "@timestamp@",
59             "nfNamingCode": "",
60             "nfVendorName": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-name",
61             "timeZoneOffset": "+00:00",
62             "version": "4.1",
63             "vesEventListenerVersion": "7.2.1"
64         },
65         "faultFields": {
66             "faultFieldsVersion": "4.0",
67             "alarmCondition": FAULT_ID,
68             "alarmInterfaceA": "o-ran-fm:alarm-notif/fault-source",
69             "eventSourceType": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-model or \"O-RU\"",
70             "specificProblem": "",
71             "eventSeverity": "NORMAL",
72             "vfStatus": "Active",
73             "alarmAdditionalInformation": {
74                 "eventTime": "@eventTime@",
75                 "equipType": "@type@",
76                 "vendor": "@vendor@",
77                 "model": "@model@"
78             }
79         }
80     }
81 }
82
83
84 class AlarmClearThread (threading.Thread):
85
86     def __init__(self, sleep_time, o_ru_id):
87         threading.Thread.__init__(self)
88         self.sleep_time = sleep_time
89         self.o_ru_id = o_ru_id
90
91     def run(self):
92         print(f'Sleeping: {self.sleep_time} before clearing O-DU: {self.o_ru_id}')
93         time.sleep(self.sleep_time)
94         msg_as_json = json.loads(json.dumps(linkFailureMessage))
95         msg_as_json["event"]["commonEventHeader"]["sourceName"] = self.o_ru_id
96         print("Sedning alarm clear for O-RU: " + self.o_ru_id)
97         requests.post(mr_host + ":" + mr_port + MR_PATH, json=msg_as_json);
98
99
100 # I'm alive function
101 @app.route('/',
102     methods=['GET'])
103 def index():
104     return 'OK', 200
105
106
107 @app.route(APP_URL,
108     methods=['POST'])
109 def sendrequest(o_du_id, o_ru_id):
110     print("Got request with O-DU ID: " + o_du_id + " and O-RU ID: " + o_ru_id)
111     random_time = int(10 * random.random())
112     alarm_clear_thread = AlarmClearThread(random_time, o_ru_id)
113     alarm_clear_thread.start()
114
115     return Response(status=200)
116
117
118 if __name__ == "__main__":
119     if os.getenv("MR-HOST") is not None:
120         mr_host = os.getenv("MR-HOST")
121         print("Using MR Host from os: " + mr_host)
122     if os.getenv("MR-PORT") is not None:
123         mr_port = os.getenv("MR-PORT")
124         print("Using MR Port from os: " + mr_port)
125
126     app.run(port=HOST_PORT, host=HOST_IP)