diff options
author | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2023-03-13 11:00:21 +0000 |
---|---|---|
committer | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2023-03-14 14:25:16 +0000 |
commit | 870ff702088b89549bc21631eb48443fff0bcd71 (patch) | |
tree | 5d52aca25035644bdc2ac687275ed858ba524e7e /mock-dmaap | |
parent | 98e0e65ba145ab4c85c301118d4a0d02940221ec (diff) |
Migrate mock applications from Orange GitLab
Move from Orange repositories into ONAP one.
Issue-ID: INT-2208
Signed-off-by: Michal Jagiello <michal.jagiello@t-mobile.pl>
Change-Id: I6e165da5144c28a6ff151e02e32f5ae89ce124e3
Diffstat (limited to 'mock-dmaap')
-rw-r--r-- | mock-dmaap/Dockerfile | 7 | ||||
-rw-r--r-- | mock-dmaap/Makefile | 23 | ||||
-rw-r--r-- | mock-dmaap/README.md | 23 | ||||
-rw-r--r-- | mock-dmaap/app/app.py | 98 | ||||
-rw-r--r-- | mock-dmaap/app/event_storage.py | 40 | ||||
-rw-r--r-- | mock-dmaap/app/sdc.py | 38 | ||||
-rw-r--r-- | mock-dmaap/app/ves.py | 46 | ||||
-rw-r--r-- | mock-dmaap/requirements.txt | 6 |
8 files changed, 281 insertions, 0 deletions
diff --git a/mock-dmaap/Dockerfile b/mock-dmaap/Dockerfile new file mode 100644 index 0000000..d17e075 --- /dev/null +++ b/mock-dmaap/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 +COPY . /app +WORKDIR /app +COPY ./requirements.txt ./ +RUN pip install -r ./requirements.txt +ENV FLASK_APP=app/app.py +CMD ["python", "app/app.py"]
\ No newline at end of file diff --git a/mock-dmaap/Makefile b/mock-dmaap/Makefile new file mode 100644 index 0000000..af8f162 --- /dev/null +++ b/mock-dmaap/Makefile @@ -0,0 +1,23 @@ +all: build + +.PHONY: build + +build: + @echo "##### Build dmaap simulator image #####" + docker build . -t dmaap-simulator + @echo "##### DONE #####" + +start: + @echo "##### Start dmaap simulator #####" + docker run -d -p 3904:3904 --name dmaap-simulator dmaap-simulator + @echo "##### DONE #####" + +stop: + @echo "##### Stop dmaap simulator #####" + docker rm -f dmaap-simulator + @echo "##### DONE #####" + +get-data: + @echo "##### Get data fetched by dmaap-simulator #####\n" + curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:3904/events + @echo "\n\n##### DONE #####" diff --git a/mock-dmaap/README.md b/mock-dmaap/README.md new file mode 100644 index 0000000..6759e79 --- /dev/null +++ b/mock-dmaap/README.md @@ -0,0 +1,23 @@ +DMaaP Mock +--------------- + + +### Build an image +``` +make build +``` + +### Start +``` +make start +``` + +### Stop +``` +make stop +``` + +### Get fetched events +``` +make get-data +``` diff --git a/mock-dmaap/app/app.py b/mock-dmaap/app/app.py new file mode 100644 index 0000000..ffaaa63 --- /dev/null +++ b/mock-dmaap/app/app.py @@ -0,0 +1,98 @@ +""" + Copyright 2023 Deutsche Telekom AG, Nokia, Orange + + 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. +""" + +import json +import logging as sys_logging +from event_storage import EventStorage +from sdc import Sdc +from ves import Ves + +from flask import Flask, request, logging, Response + +app = Flask(__name__) + +sys_logging.basicConfig(level=sys_logging.DEBUG) +logger = logging.create_logger(app) +apiKey = {"key": "test_key", "secret": "test_secret"} +event_storage = EventStorage() +sdc = Sdc(logger, event_storage) +ves = Ves(logger, event_storage) + + +@app.route("/apiKeys/create", methods=['POST']) +def create_api_key(): + resp = Response(json.dumps(apiKey)) + return resp + + +@app.route("/apiKeys/<path:key>", methods=['DELETE']) +def delete_api_key(key): + if key == apiKey["key"]: + return {}, 200 + return {"no such key"}, 404 + + +@app.route("/reset", methods=['GET']) +def reset_events(): + event_storage.clear() + return event_storage + + +@app.route("/events", methods=['GET']) +def get_events(): + resp = Response(json.dumps(event_storage)) + resp.headers['Content-Type'] = 'application/json' + return resp + + +@app.route("/events/<path:topic>/<path:consumer_group>/<path:consumer_id>", methods=['GET']) +def get_events_from_topic(topic, consumer_group, consumer_id): + events = event_storage.get_events_from_topic(topic) + resp = Response(json.dumps(events)) + event_storage.delete_events(topic) + resp.headers['Content-Type'] = 'application/json' + return resp + + +@app.route("/events/<path:topic>", methods=['POST']) +def post_msg_to_topic(topic): + receive_msg = request.data.decode("utf-8") + if sdc.is_event_from_sdc(receive_msg): + return sdc.post_msg_to_topic(topic, receive_msg) + else: + return ves.handle_new_event(topic, receive_msg) + + +@app.route("/events/<path:topic>/add", methods=['POST']) +def add_msg_to_topic(topic): + receive_msg = request.data.decode("utf-8") + return sdc.add_msg_to_topic(topic, receive_msg) + + +@app.route("/topics", methods=['GET']) +def get_topics(): + topics = { + 'topics': ['org.onap.dmaap.mr.PNF_REGISTRATION', 'SDC-DISTR-STATUS-TOPIC-AUTO', 'SDC-DISTR-NOTIF-TOPIC-AUTO', + 'org.onap.dmaap.mr.PNF_READY', 'POLICY-PDP-PAP', 'POLICY-NOTIFICATION', + 'unauthenticated.SEC_3GPP_FAULTSUPERVISION_OUTPUT', '__consumer_offsets', + 'org.onap.dmaap.mr.mirrormakeragent']} + resp = Response(json.dumps(topics)) + resp.headers['Content-Type'] = 'application/json' + return resp + + +if __name__ == "__main__": + app.run(host='0.0.0.0', port=3904) diff --git a/mock-dmaap/app/event_storage.py b/mock-dmaap/app/event_storage.py new file mode 100644 index 0000000..a520166 --- /dev/null +++ b/mock-dmaap/app/event_storage.py @@ -0,0 +1,40 @@ +""" + Copyright 2023 Deutsche Telekom AG, Nokia, Orange + + 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. +""" + +class EventStorage: + __events = {} + + def get_events(self): + return self.__events + + def get_events_from_topic(self, topic): + if self.__events.__contains__(topic): + return self.__events[topic] + else: + return [] + + def add(self, topic, event): + if self.__events.__contains__(topic): + self.__events[topic].append(event) + else: + self.__events[topic] = [event] + + def delete_events(self, topic): + if self.__events.__contains__(topic): + self.__events[topic].clear() + + def clear(self): + self.__events.clear() diff --git a/mock-dmaap/app/sdc.py b/mock-dmaap/app/sdc.py new file mode 100644 index 0000000..5317ad2 --- /dev/null +++ b/mock-dmaap/app/sdc.py @@ -0,0 +1,38 @@ +""" + Copyright 2023 Deutsche Telekom AG, Nokia, Orange + + 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. +""" + +class Sdc: + __jsonArtifact = {} + + def __init__(self, logger, events): + self.__logger = logger + self.__events = events + + def post_msg_to_topic(self, topic, receive_msg): + self.__logger.info("received events: " + str(receive_msg)) + return self.__jsonArtifact + + def add_msg_to_topic(self, topic, receive_msg): + self.__jsonArtifact = receive_msg + self.__events.add(topic, receive_msg) + self.__logger.info("received events: " + str(receive_msg)) + return receive_msg + + def is_event_from_sdc(self, event): + if event[:3] == '14.': + return True + else: + return False diff --git a/mock-dmaap/app/ves.py b/mock-dmaap/app/ves.py new file mode 100644 index 0000000..6c25730 --- /dev/null +++ b/mock-dmaap/app/ves.py @@ -0,0 +1,46 @@ +""" + Copyright 2023 Deutsche Telekom AG, Nokia, Orange + + 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. +""" + +import json + + +class Ves: + + def __init__(self, logger, events): + self.__logger = logger + self.__events = events + + def handle_new_event(self, topic, http_request): + receive_events = self.__decode_request_data(http_request) + for event in receive_events: + self.__events.add(topic, json.loads(event)) + return {}, 200 + + def __decode_request_data(self, data): + receive_events = data.split("\n") + receive_events = receive_events[:-1] + self.__logger.info("received events: " + str(receive_events)) + correct_events = [] + for event in receive_events: + self.__logger.info("received event: " + str(event)) + correct_events.append(self.__get_correct_json(event)) + return correct_events + + def __get_correct_json(self, incorrect_json): + json_start_position = incorrect_json.find("{") + correct_json = incorrect_json[json_start_position:] + correct_json = correct_json.replace("\r", "").replace("\t", "").replace(" ", "") + return correct_json diff --git a/mock-dmaap/requirements.txt b/mock-dmaap/requirements.txt new file mode 100644 index 0000000..a6d2d3a --- /dev/null +++ b/mock-dmaap/requirements.txt @@ -0,0 +1,6 @@ +Click==7.0 +Flask==1.1.1 +itsdangerous==1.1.0 +Jinja2==2.10.3 +MarkupSafe==1.1.1 +Werkzeug==0.16.0 |