From: elinuxhenrik Date: Fri, 7 May 2021 14:38:56 +0000 (+0200) Subject: Adapt use case to run in Docker X-Git-Tag: 2.2.0~47 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F20%2F6020%2F1;p=nonrtric.git Adapt use case to run in Docker Change-Id: Ie6a1edc6962d9df27a172b8e148d9485c5ca98bb Isue-ID: NONRTRIC-495 Signed-off-by: elinuxhenrik --- diff --git a/test/usecases/linkfailure/app/Dockerfile b/test/usecases/linkfailure/app/Dockerfile new file mode 100644 index 00000000..d5d6c2cd --- /dev/null +++ b/test/usecases/linkfailure/app/Dockerfile @@ -0,0 +1,27 @@ +# ============LICENSE_START=============================================== +# Copyright (C) 2021 Nordix Foundation. All rights reserved. +# ======================================================================== +# 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. +# ============LICENSE_END================================================= +# + + +FROM python:3.8-slim-buster + +COPY . /usr/src/app/ + +WORKDIR /usr/src/app + +RUN pip install -r requirements.txt + +CMD [ "python3", "main.py" ] diff --git a/test/usecases/linkfailure/app/main.py b/test/usecases/linkfailure/app/main.py index 517e4dfa..394af8d5 100644 --- a/test/usecases/linkfailure/app/main.py +++ b/test/usecases/linkfailure/app/main.py @@ -18,8 +18,9 @@ import argparse import ast -import requests import json +import os +import requests import time MR_PATH = "/events/[TOPIC]/users/test/" @@ -101,14 +102,26 @@ if __name__ == '__main__': parser.add_argument('--version', action='version', version='%(prog)s 1.0') args = vars(parser.parse_args()) mr_host = args["mrHost"] + if os.getenv("MR-HOST") is not None: + mr_host = os.getenv("MR-HOST") + print("Using MR Host from os: " + mr_host) mr_port = args["mrPort"] + if os.getenv("MR-PORT") is not None: + mr_port = os.getenv("MR-PORT") + print("Using MR Port from os: " + mr_port) mr_topic = args["mrTopic"] sdnr_host = args["sdnrHost"] + if os.getenv("SDNR-HOST") is not None: + sdnr_host = os.getenv("SDNR-HOST") + print("Using SNDR Host from os: " + sdnr_host) sdnr_port = args["sdnrPort"] + if os.getenv("SDNR-PORT") is not None: + sdnr_port = os.getenv("SDNR-PORT") + print("Using SNDR Host from os: " + sdnr_port) o_ru_to_o_du_map = read_o_ru_to_o_du_map_from_file(args["oRuTooDuMapFile"]) pollTime = args["pollTime"] - if args["verbose"]: + if os.getenv("VERBOSE") is not None or args["verbose"]: def verboseprint(*args, **kwargs): print(*args, **kwargs) @@ -123,13 +136,16 @@ if __name__ == '__main__': sdnr_address = sdnr_host + ":" + str(sdnr_port) while True: - response = requests.get(mr_address) - verboseprint("Polling") - 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) - elif (is_message_clear_link_failure(message)): - handle_clear_link_failure(message) + try: + verboseprint("Polling") + response = requests.get(mr_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) + elif (is_message_clear_link_failure(message)): + handle_clear_link_failure(message) + except Exception as inst: + print(inst) time.sleep(pollTime) diff --git a/test/usecases/linkfailure/app/o-ru-to-o-du-map.txt b/test/usecases/linkfailure/app/o-ru-to-o-du-map.txt index 62128836..c9ec8eae 100644 --- a/test/usecases/linkfailure/app/o-ru-to-o-du-map.txt +++ b/test/usecases/linkfailure/app/o-ru-to-o-du-map.txt @@ -1,12 +1,13 @@ { - "O-RAN-RU-00": "O-RAN-DU-0", - "O-RAN-RU-01": "O-RAN-DU-1", - "O-RAN-RU-02": "O-RAN-DU-2", - "O-RAN-RU-03": "O-RAN-DU-3", - "O-RAN-RU-04": "O-RAN-DU-4", - "O-RAN-RU-05": "O-RAN-DU-5", - "O-RAN-RU-06": "O-RAN-DU-6", - "O-RAN-RU-07": "O-RAN-DU-7", - "O-RAN-RU-08": "O-RAN-DU-8", - "O-RAN-RU-09": "O-RAN-DU-9" + "ERICSSON-O-RU-11220": "HCL-O-DU-1122", + "ERICSSON-O-RU-11221": "HCL-O-DU-1122", + "ERICSSON-O-RU-11222": "HCL-O-DU-1122", + "ERICSSON-O-RU-11223": "HCL-O-DU-1122", + "ERICSSON-O-RU-11223": "HCL-O-DU-1122", + "ERICSSON-O-RU-11224": "HCL-O-DU-1123", + "ERICSSON-O-RU-11225": "HCL-O-DU-1123", + "ERICSSON-O-RU-11226": "HCL-O-DU-1123", + "ERICSSON-O-RU-11227": "HCL-O-DU-1124", + "ERICSSON-O-RU-11228": "HCL-O-DU-1125", + "ERICSSON-O-RU-11229": "HCL-O-DU-1125", } \ No newline at end of file diff --git a/test/usecases/linkfailure/simulators/Dockerfile-message-generator b/test/usecases/linkfailure/simulators/Dockerfile-message-generator new file mode 100644 index 00000000..139b7911 --- /dev/null +++ b/test/usecases/linkfailure/simulators/Dockerfile-message-generator @@ -0,0 +1,27 @@ +# ============LICENSE_START=============================================== +# Copyright (C) 2021 Nordix Foundation. All rights reserved. +# ======================================================================== +# 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. +# ============LICENSE_END================================================= +# + + +FROM python:3.8-slim-buster + +COPY . /usr/src/app/ + +WORKDIR /usr/src/app + +RUN pip install -r requirements.txt + +CMD [ "python3", "message_generator.py" ] diff --git a/test/usecases/linkfailure/simulators/Dockerfile-sdnr-sim b/test/usecases/linkfailure/simulators/Dockerfile-sdnr-sim new file mode 100644 index 00000000..103c97c0 --- /dev/null +++ b/test/usecases/linkfailure/simulators/Dockerfile-sdnr-sim @@ -0,0 +1,27 @@ +# ============LICENSE_START=============================================== +# Copyright (C) 2021 Nordix Foundation. All rights reserved. +# ======================================================================== +# 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. +# ============LICENSE_END================================================= +# + + +FROM python:3.8-slim-buster + +COPY . /usr/src/app/ + +WORKDIR /usr/src/app + +RUN pip install -r requirements.txt + +CMD [ "python3", "sdnr_simulator.py" ] diff --git a/test/usecases/linkfailure/simulators/message_generator.py b/test/usecases/linkfailure/simulators/message_generator.py index f6b4e25b..1b0676ca 100644 --- a/test/usecases/linkfailure/simulators/message_generator.py +++ b/test/usecases/linkfailure/simulators/message_generator.py @@ -16,16 +16,19 @@ # ============LICENSE_END================================================= # +import json +import os +import random import requests import time -import random -import json # Randomly, between 0 and 10 seconds sends a "CUS Link Failure" alarm event to the Message Router. The ID of the O-RU is also # randomly generated between 0 and 9. # When the modulo of the ID is 1, a "heartbeat" message will also be sent to MR. -MR_PATH = "http://localhost:3904/events/unauthenticated.SEC_FAULT_OUTPUT" +mr_host = "http://localhost" +mr_port = "3904" +MR_PATH = "/events/unauthenticated.SEC_FAULT_OUTPUT" linkFailureMessage = { "event": { @@ -87,17 +90,27 @@ heartBeatMessage = { } } -while True: - random_time = int(10 * random.random()) - if (random_time % 3 == 1): - print("Sent heart beat") - requests.post(MR_PATH, json=heartBeatMessage); +if __name__ == "__main__": + if os.getenv("MR-HOST") is not None: + mr_host = os.getenv("MR-HOST") + print("Using MR Host from os: " + mr_host) + if os.getenv("MR-PORT") is not None: + mr_port = os.getenv("MR-PORT") + print("Using MR Port from os: " + mr_port) + + mr_url = mr_host + ":" + mr_port + MR_PATH + print(mr_url) + while True: + random_time = int(10 * random.random()) + if (random_time % 3 == 1): + print("Sent heart beat") + requests.post(mr_url, json=heartBeatMessage); - o_ru_id = "O-RAN-RU-0" + str(random_time) - print("Sent link failure for O-RAN-RU: " + o_ru_id) - msg_as_json = json.loads(json.dumps(linkFailureMessage)) - msg_as_json["event"]["commonEventHeader"]["sourceName"] = o_ru_id - requests.post(MR_PATH, json=msg_as_json); + o_ru_id = "ERICSSON-O-RU-1122" + str(random_time) + print("Sent link failure for O-RAN-RU: " + o_ru_id) + msg_as_json = json.loads(json.dumps(linkFailureMessage)) + msg_as_json["event"]["commonEventHeader"]["sourceName"] = o_ru_id + requests.post(mr_url, json=msg_as_json); - time.sleep(random_time) + time.sleep(random_time) diff --git a/test/usecases/linkfailure/simulators/sdnc_simulator.py b/test/usecases/linkfailure/simulators/sdnr_simulator.py similarity index 89% rename from test/usecases/linkfailure/simulators/sdnc_simulator.py rename to test/usecases/linkfailure/simulators/sdnr_simulator.py index dd41732e..885a5808 100644 --- a/test/usecases/linkfailure/simulators/sdnc_simulator.py +++ b/test/usecases/linkfailure/simulators/sdnr_simulator.py @@ -19,6 +19,7 @@ from flask import Flask from flask import Response import json +import os import random import requests import threading @@ -29,7 +30,9 @@ import time # "CUS Link Failure" alarm event to MR. app = Flask(__name__) -MR_PATH = "http://localhost:3904/events/unauthenticated.SEC_FAULT_OUTPUT" +mr_host = "http://localhost" +mr_port = "3904" +MR_PATH = "/events/unauthenticated.SEC_FAULT_OUTPUT" # Server info HOST_IP = "::" @@ -89,7 +92,7 @@ class AlarmClearThread (threading.Thread): msg_as_json = json.loads(json.dumps(linkFailureMessage)) msg_as_json["event"]["commonEventHeader"]["sourceName"] = self.o_ru_id print("Sedning alarm clear for O-RU: " + self.o_ru_id) - requests.post(MR_PATH, json=msg_as_json); + requests.post(mr_host + ":" + mr_port + MR_PATH, json=msg_as_json); # I'm alive function @@ -111,4 +114,11 @@ def sendrequest(o_du_id, o_ru_id): if __name__ == "__main__": + if os.getenv("MR-HOST") is not None: + mr_host = os.getenv("MR-HOST") + print("Using MR Host from os: " + mr_host) + if os.getenv("MR-PORT") is not None: + mr_port = os.getenv("MR-PORT") + print("Using MR Port from os: " + mr_port) + app.run(port=HOST_PORT, host=HOST_IP)