Adapt to O-RU use case spec
[nonrtric.git] / test / usecases / linkfailure / app / main.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 import argparse
20 import ast
21 import requests
22 import json
23 import time
24
25 MR_PATH = "/events/[TOPIC]/users/test/"
26 SDNR_PATH = "/rests/data/network-topology:network-topology/topology=topology-netconf/node=[O-DU-ID]/yang-ext:mount/o-ran-sc-du-hello-world:network-function/du-to-ru-connection=[O-RU-ID]"
27
28 UNLOCK_MESSAGE = {
29     "o-ran-sc-du-hello-world:du-to-ru-connection": [
30         {
31             "name":"",
32             "administrative-state":"UNLOCKED"
33         }
34     ]
35 }
36
37
38 def is_message_new_link_failure(message):
39     msg_as_json = json.loads(message)
40     event_headers = msg_as_json["event"]["commonEventHeader"]
41
42     link_failure = False
43     if (event_headers["domain"] == "fault"):
44         fault_fields = msg_as_json["event"]["faultFields"]
45         link_failure = fault_fields["alarmCondition"] == "30" and fault_fields["eventSeverity"] != "NORMAL"
46
47     return link_failure
48
49
50 def is_message_clear_link_failure(message):
51     msg_as_json = json.loads(message)
52     event_headers = msg_as_json["event"]["commonEventHeader"]
53
54     link_failure_clear = False
55     if (event_headers["domain"] == "fault"):
56         fault_fields = msg_as_json["event"]["faultFields"]
57         link_failure_clear = fault_fields["alarmCondition"] == "30" and fault_fields["eventSeverity"] == "NORMAL"
58
59     return link_failure_clear
60
61
62 def handle_link_failure(message, o_ru_to_o_du_map, sdnr_address):
63     verboseprint("Got a link failure: ")
64     alarm_msg_as_json = json.loads(message)
65     event_headers = alarm_msg_as_json["event"]["commonEventHeader"]
66     o_ru_id = event_headers["sourceName"]
67     verboseprint("O-RU ID: " + o_ru_id)
68     o_du_id = o_ru_to_o_du_map[o_ru_id]
69     verboseprint("O-DU ID: " + o_du_id)
70     unlock_msg = json.loads(json.dumps(UNLOCK_MESSAGE))
71     unlock_msg["o-ran-sc-du-hello-world:du-to-ru-connection"][0]["name"] = o_ru_id
72     send_path = SDNR_PATH.replace("[O-DU-ID]", o_du_id).replace("[O-RU-ID]", o_ru_id)
73     requests.post(sdnr_address + send_path, json=unlock_msg)
74
75
76 def handle_clear_link_failure(message):
77     msg_as_json = json.loads(message)
78     event_headers = msg_as_json["event"]["commonEventHeader"]
79     o_ru_id = event_headers["sourceName"]
80     verboseprint("Cleared Link Failure for O-RU ID: " + o_ru_id)
81
82
83 def read_o_ru_to_o_du_map_from_file(map_file):
84     file = open(map_file, "r")
85     contents = file.read()
86     dictionary = ast.literal_eval(contents)
87     file.close()
88     return dictionary
89
90
91 if __name__ == '__main__':
92     parser = argparse.ArgumentParser(prog='PROG')
93     parser.add_argument('--mrHost', help='The URL of the MR host', default="http://message-router.onap")
94     parser.add_argument('--mrPort', help='The port of the MR host', type=int, default=3904)
95     parser.add_argument('--mrTopic', help='The topic to poll messages from', default="unauthenticated.SEC_FAULT_OUTPUT")
96     parser.add_argument('--sdnrHost', help='The URL of the SNDR host', default="http://localhost")
97     parser.add_argument('--sdnrPort', help='The port of the SDNR host', type=int, default=9990)
98     parser.add_argument('--oRuTooDuMapFile', help='A file with the mapping between O-RU ID and O-DU ID as a dictionary', default="o-ru-to-o-du-map.txt")
99     parser.add_argument('--pollTime', help='The time between polls', type=int, default=10)
100     parser.add_argument('-v', '--verbose', action='store_true', help='Turn on verbose printing')
101     parser.add_argument('--version', action='version', version='%(prog)s 1.0')
102     args = vars(parser.parse_args())
103     mr_host = args["mrHost"]
104     mr_port = args["mrPort"]
105     mr_topic = args["mrTopic"]
106     sdnr_host = args["sdnrHost"]
107     sdnr_port = args["sdnrPort"]
108     o_ru_to_o_du_map = read_o_ru_to_o_du_map_from_file(args["oRuTooDuMapFile"])
109     pollTime = args["pollTime"]
110
111     if args["verbose"]:
112
113         def verboseprint(*args, **kwargs):
114             print(*args, **kwargs)
115
116     else:
117         verboseprint = lambda *a, **k: None  # do-nothing function
118
119     verboseprint("Using MR address: " + mr_host + ":" + str(mr_port) + " and topic: " + mr_topic)
120     verboseprint("Using SDNR address: " + sdnr_host + ":" + str(sdnr_port))
121     verboseprint("Starting with " + str(pollTime) + " seconds between polls")
122     mr_address = mr_host + ":" + str(mr_port) + MR_PATH.replace("[TOPIC]", mr_topic)
123     sdnr_address = sdnr_host + ":" + str(sdnr_port)
124
125     while True:
126         response = requests.get(mr_address)
127         verboseprint("Polling")
128         messages = response.json()
129         for message in messages:
130             if (is_message_new_link_failure(message)):
131                 handle_link_failure(message, o_ru_to_o_du_map, sdnr_address)
132             elif (is_message_clear_link_failure(message)):
133                 handle_clear_link_failure(message)
134
135         time.sleep(pollTime)