Add login to O-RU Closed Loop use case 51/6351/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 18 Jun 2021 09:02:00 +0000 (11:02 +0200)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 18 Jun 2021 09:04:21 +0000 (11:04 +0200)
Issue-ID: NONRTRIC-495
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: I50966591443646a1e5940d82df51dccd54c386fc

test/usecases/oruclosedlooprecovery/scriptversion/app/main.py
test/usecases/oruclosedlooprecovery/scriptversion/simulators/requirements.txt
test/usecases/oruclosedlooprecovery/scriptversion/simulators/sdnr_simulator.py

index efd45e7..8f5b244 100644 (file)
@@ -61,18 +61,21 @@ def is_message_clear_link_failure(message):
     return link_failure_clear
 
 
-def handle_link_failure(message, o_ru_to_o_du_map, sdnr_address):
+def handle_link_failure(message, o_ru_to_o_du_map, sdnr_address, sdnr_user, sdnr_pwd):
     verboseprint("Got a link failure: ")
     alarm_msg_as_json = json.loads(message)
     event_headers = alarm_msg_as_json["event"]["commonEventHeader"]
     o_ru_id = event_headers["sourceName"]
     verboseprint("O-RU ID: " + o_ru_id)
-    o_du_id = o_ru_to_o_du_map[o_ru_id]
-    verboseprint("O-DU ID: " + o_du_id)
-    unlock_msg = json.loads(json.dumps(UNLOCK_MESSAGE))
-    unlock_msg["o-ran-sc-du-hello-world:du-to-ru-connection"][0]["name"] = o_ru_id
-    send_path = SDNR_PATH.replace("[O-DU-ID]", o_du_id).replace("[O-RU-ID]", o_ru_id)
-    requests.post(sdnr_address + send_path, json=unlock_msg)
+    if o_ru_id in o_ru_to_o_du_map:
+        o_du_id = o_ru_to_o_du_map[o_ru_id]
+        verboseprint("O-DU ID: " + o_du_id)
+        unlock_msg = json.loads(json.dumps(UNLOCK_MESSAGE))
+        unlock_msg["o-ran-sc-du-hello-world:du-to-ru-connection"][0]["name"] = o_ru_id
+        send_path = SDNR_PATH.replace("[O-DU-ID]", o_du_id).replace("[O-RU-ID]", o_ru_id)
+        requests.put(sdnr_address + send_path, auth=(sdnr_user, sdnr_pwd), json=unlock_msg)
+    else:
+        print("ERROR: No mapping for O-RU ID: " + o_ru_id)
 
 
 def handle_clear_link_failure(message):
@@ -90,7 +93,7 @@ def read_o_ru_to_o_du_map_from_file(map_file):
     return dictionary
 
 
-def poll_and_handle_messages(mr_address, sdnr_address):
+def poll_and_handle_messages(mr_address, sdnr_address, sdnr_user, sdnr_pwd):
     while True:
         try:
             verboseprint("Polling")
@@ -98,7 +101,7 @@ def poll_and_handle_messages(mr_address, sdnr_address):
             messages = response.json()
             for message in messages:
                 if (is_message_new_link_failure(message)):
-                    handle_link_failure(message, o_ru_to_o_du_map, sdnr_address)
+                    handle_link_failure(message, o_ru_to_o_du_map, sdnr_address, sdnr_user, sdnr_pwd)
                 elif (is_message_clear_link_failure(message)):
                     handle_clear_link_failure(message)
         except Exception as inst:
@@ -114,6 +117,8 @@ if __name__ == '__main__':
     parser.add_argument('--mrTopic', help='The topic to poll messages from (default: %(default)s)', default="unauthenticated.SEC_FAULT_OUTPUT")
     parser.add_argument('--sdnrHost', help='The URL of the SNDR host (default: %(default)s)', default="http://localhost")
     parser.add_argument('--sdnrPort', help='The port of the SDNR host (default: %(default)d)', type=int, default=9990)
+    parser.add_argument('--sdnrUser', help='Username for SDNR', default="admin")
+    parser.add_argument('--sdnrPwd', help='Password for SDNR', default="Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U")
     parser.add_argument('--oRuTooDuMapFile', help='A file with the mapping between O-RU ID and O-DU ID as a dictionary (default: %(default)s)', default="o-ru-to-o-du-map.txt")
     parser.add_argument('--pollTime', help='The time between polls (default: %(default)d)', type=int, default=10)
     parser.add_argument('-v', '--verbose', action='store_true', help='Turn on verbose printing')
@@ -136,6 +141,14 @@ if __name__ == '__main__':
     if os.getenv("SDNR-PORT") is not None:
         sdnr_port = os.getenv("SDNR-PORT")
         print("Using SNDR Host from os: " + sdnr_port)
+    sdnr_user = args["sdnrUser"]
+    if os.getenv("SDNR-USER") is not None:
+        sdnr_user = os.getenv("SDNR-USER")
+        print("Using SNDR User from os: " + sdnr_user)
+    sdnr_pwd = args["sdnrPwd"]
+    if os.getenv("SDNR-PWD") is not None:
+        sdnr_pwd = os.getenv("SDNR-PWD")
+        print("Using SNDR Password from os: " + sdnr_pwd)
     o_ru_to_o_du_map = read_o_ru_to_o_du_map_from_file(args["oRuTooDuMapFile"])
     pollTime = args["pollTime"]
 
@@ -153,4 +166,4 @@ if __name__ == '__main__':
     mr_address = mr_host + ":" + str(mr_port) + MR_PATH.replace("[TOPIC]", mr_topic)
     sdnr_address = sdnr_host + ":" + str(sdnr_port)
 
-    poll_and_handle_messages(mr_address, sdnr_address)
+    poll_and_handle_messages(mr_address, sdnr_address, sdnr_user, sdnr_pwd)
index 888fad6..535c3ee 100644 (file)
@@ -18,6 +18,7 @@
 
 from flask import Flask
 from flask import Response
+from flask_httpauth import HTTPBasicAuth
 import json
 import os
 import random
@@ -29,6 +30,7 @@ import time
 # Stores the ID of the O-DU and randomly, after between 0 and 10 seconds, sends an Alarm Notification that clears the
 # "CUS Link Failure" alarm event to MR.
 app = Flask(__name__)
+auth = HTTPBasicAuth()
 
 mr_host = "http://localhost"
 mr_port = "3904"
@@ -39,6 +41,9 @@ HOST_IP = "::"
 HOST_PORT = 9990
 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>"
 
+USERNAME = "admin"
+PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U"
+
 FAULT_ID = "28"
 
 linkFailureMessage = {
@@ -104,8 +109,15 @@ def index():
     return 'OK', 200
 
 
+@auth.verify_password
+def verify_password(username, password):
+    if username == USERNAME and password == PASSWORD:
+        return username
+
+
 @app.route(APP_URL,
-    methods=['POST'])
+    methods=['PUT'])
+@auth.login_required
 def sendrequest(o_du_id, o_ru_id):
     print("Got request with O-DU ID: " + o_du_id + " and O-RU ID: " + o_ru_id)
     random_time = int(10 * random.random())