aboutsummaryrefslogtreecommitdiffstats
path: root/sanitycheck/dmaap-simulator
diff options
context:
space:
mode:
Diffstat (limited to 'sanitycheck/dmaap-simulator')
-rw-r--r--sanitycheck/dmaap-simulator/Dockerfile7
-rw-r--r--sanitycheck/dmaap-simulator/Makefile23
-rw-r--r--sanitycheck/dmaap-simulator/README.md22
-rw-r--r--sanitycheck/dmaap-simulator/requirements.txt6
-rw-r--r--sanitycheck/dmaap-simulator/simulator.py73
5 files changed, 131 insertions, 0 deletions
diff --git a/sanitycheck/dmaap-simulator/Dockerfile b/sanitycheck/dmaap-simulator/Dockerfile
new file mode 100644
index 0000000..f84cac2
--- /dev/null
+++ b/sanitycheck/dmaap-simulator/Dockerfile
@@ -0,0 +1,7 @@
+FROM python:3
+WORKDIR /application
+COPY ./simulator.py ./
+COPY ./requirements.txt ./
+RUN pip install -r ./requirements.txt
+ENV FLASK_APP=./simulator.py
+CMD ["python", "./simulator.py"]
diff --git a/sanitycheck/dmaap-simulator/Makefile b/sanitycheck/dmaap-simulator/Makefile
new file mode 100644
index 0000000..af8f162
--- /dev/null
+++ b/sanitycheck/dmaap-simulator/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/sanitycheck/dmaap-simulator/README.md b/sanitycheck/dmaap-simulator/README.md
new file mode 100644
index 0000000..de0615a
--- /dev/null
+++ b/sanitycheck/dmaap-simulator/README.md
@@ -0,0 +1,22 @@
+DMaaP simulator
+---------------
+
+### Build an image
+```
+make build
+```
+
+### Start
+```
+make start
+```
+
+### Stop
+```
+make stop
+```
+
+### Get fetched events
+```
+make get-data
+```
diff --git a/sanitycheck/dmaap-simulator/requirements.txt b/sanitycheck/dmaap-simulator/requirements.txt
new file mode 100644
index 0000000..a6d2d3a
--- /dev/null
+++ b/sanitycheck/dmaap-simulator/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
diff --git a/sanitycheck/dmaap-simulator/simulator.py b/sanitycheck/dmaap-simulator/simulator.py
new file mode 100644
index 0000000..6a06266
--- /dev/null
+++ b/sanitycheck/dmaap-simulator/simulator.py
@@ -0,0 +1,73 @@
+import json
+import logging as sys_logging
+
+from flask import Flask, request, logging, Response
+
+app = Flask(__name__)
+
+sys_logging.basicConfig(level=sys_logging.DEBUG)
+logger = logging.create_logger(app)
+events = {}
+
+
+@app.route("/events/<path:topic>", methods=['POST'])
+def event_sec_fault_output(topic):
+ return handle_new_event(topic, request)
+
+
+@app.route("/events", methods=['GET'])
+def get_events():
+ resp = Response(json.dumps(events))
+ resp.headers['Content-Type'] = 'application/json'
+ return resp
+
+
+@app.route("/events/<path:topic>", methods=['GET'])
+def get_events_from_topic(topic):
+ resp = Response(json.dumps(get_events_from_map(topic)))
+ resp.headers['Content-Type'] = 'application/json'
+ return resp
+
+
+def handle_new_event(topic, http_request):
+ receive_events = decode_request_data(http_request.data)
+ for event in receive_events:
+ add_event_to_map(topic, json.loads(event))
+ return {}, 200
+
+
+def decode_request_data(request_data):
+ data = request_data.decode("utf-8")
+ receive_events = data.split("\n")
+ receive_events = receive_events[:-1]
+ logger.info("received events: " + str(receive_events))
+ correct_events = []
+ for event in receive_events:
+ logger.info("received event: " + str(event))
+ correct_events.append(get_correct_json(event))
+ return correct_events
+
+
+def get_correct_json(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
+
+
+def add_event_to_map(topic, event):
+ if events.__contains__(topic):
+ events[topic].append(event)
+ else:
+ events[topic] = [event]
+
+
+def get_events_from_map(topic):
+ if events.__contains__(topic):
+ return events[topic]
+ else:
+ return []
+
+
+if __name__ == "__main__":
+ app.run(host='0.0.0.0', port=3904)