aboutsummaryrefslogtreecommitdiffstats
path: root/sanitycheck/dmaap-simulator/simulator.py
blob: 136afb854dce4536d1e3738962fcaf7e22593d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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", methods=['DELETE'])
def clear_events():
    events.clear()
    return {}, 200


@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)