-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathaccumulator-server.py
executable file
·612 lines (490 loc) · 16.4 KB
/
accumulator-server.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/bin/env python3
# -*- coding: latin-1 -*-
# Copyright 2013 Telefonica Investigacion y Desarrollo, S.A.U
#
# This file is part of Orion Context Broker.
#
# Orion Context Broker is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Orion Context Broker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/.
#
# For those usages not covered by this license please contact with
# iot_support at tid dot es
# MQTT functionality based in old accumulator-mqtt.py script, by Burak
# Requires paho-mqtt module:
#
# paho-mqtt==1.6.1
__author__ = 'fermin'
# This program stores everything it receives by HTTP in a given URL (pased as argument),
# Then return the accumulated data upon receiving 'GET <host>:<port>/dump'. It is aimet
# at harness test for subscription scenarios (so accumulator-server.py plays the role
# of a subscribed application)
#
# Known issues:
#
# * Curl users: use -H "Expect:" (default "Expect: 100-continue" used by curl has been problematic
# in the past)
# * Curl users: use -H "Content-Type: application/xml" for XML payload (the default:
# "Content-Type: application/x-www-form-urlencoded" has been problematic in the pass)
# * This script requires at least Flask 2.0.2, which comes with Werkzeug 2.0.2.
from flask import Flask, request, Response
from getopt import getopt, GetoptError
from datetime import datetime
from math import trunc
from time import sleep
import sys
import os
import atexit
import string
import signal
import json
import paho.mqtt.client as mqtt
import threading
def usage_and_exit(msg):
"""
Print usage message and exit"
:param msg: optional error message to print
"""
if msg != '':
print(msg + "\n")
usage()
sys.exit(1)
def usage():
"""
Print usage message
"""
print(f"Usage: {os.path.basename(__file__)} --host <host> --port <port> --mqttHost " +
"<host> --mqttPort <port> --mqttTopic <topic> --url <server url> --pretty-print " +
"--https --key <key_file> --cert <cert_file> -v -u")
print("""\nParameters:
--host <host>: host to use database to use (default is '0.0.0.0')"
--port <port>: port to use (default is 1028)"
--mqttHost <host>: mqtt broker host to use (if not defined MQTT listening is not enabled)"
--mqttPort <port>: mqtt broker port to use (default is 1883)"
--mqttTopic <topic>: mqtt topic to use (default is accumulate_mqtt)"
--url <server url>: server URL to use (default is /accumulate)"
--pretty-print: pretty print mode"
--https: start in https"
--key <key file>: (only used if https is enabled)"
--cert <cert file>: (only used if https is enabled)"
-v: verbose mode"
-u: print this usage message
"""
)
# This function is registered to be called upon termination
def all_done():
os.unlink(pidfile)
# Default arguments
port = 1028
host = '0.0.0.0'
mqtt_host = None
mqtt_port = 1883
mqtt_topic = 'accumulate_mqtt'
server_url = '/accumulate'
verbose = 0
pretty = False
https = False
key_file = None
cert_file = None
# TODO: Improve cli argument parsing with click
# https://click.palletsprojects.com/en/8.0.x/
# This will avoid the 'complex' for statement at L137
try:
opts, args = getopt(
sys.argv[1:],
'vu', [
'host=', 'port=',
'mqttHost=',
'mqttPort=',
'mqttTopic=',
'url=',
'pretty-print',
'https',
'key=',
'cert='
]
)
except GetoptError:
usage_and_exit('wrong parameter')
for opt, arg in opts:
if opt == '-u':
usage()
sys.exit(0)
elif opt == '--mqttHost':
mqtt_host = arg
elif opt == '--host':
host = arg
elif opt == '--url':
server_url = arg
elif opt == '--mqttTopic':
mqtt_topic = arg
elif opt == '--port':
try:
port = int(arg)
except ValueError:
usage_and_exit('port parameter must be an integer')
elif opt == '--mqttPort':
try:
mqtt_port = int(arg)
except ValueError:
usage_and_exit('mqttPort parameter must be an integer')
elif opt == '-v':
verbose = 1
elif opt == '--pretty-print':
pretty = True
elif opt == '--https':
https = True
elif opt == '--key':
key_file = arg
elif opt == '--cert':
cert_file = arg
else:
usage_and_exit()
if https:
if key_file is None or cert_file is None:
print("if --https is used then you have to provide --key and --cert")
sys.exit(1)
if verbose:
print("verbose mode is on")
print("port: " + str(port))
print("host: " + str(host))
print("mqtt_port: " + str(mqtt_port))
print("mqtt_host: " + str(mqtt_host))
print("mqtt_topic: " + str(mqtt_topic))
print("server_url: " + str(server_url))
print("pretty: " + str(pretty))
print("https: " + str(https))
if https:
print("key file: " + key_file)
print("cert file: " + cert_file)
pid = str(os.getpid())
pidfile = "/tmp/accumulator." + str(port) + ".pid"
#
# If an accumulator process is already running, it is killed.
# First using SIGTERM, then SIGINT and finally SIGKILL
# The exception handling is needed as this process dies in case
# a kill is issued on a non-running process ...
#
if os.path.isfile(pidfile):
oldpid = open(pidfile, 'r').read()
opid = int(oldpid)
print(f"PID file {pidfile} already exists, killing the process {oldpid}")
try:
oldstderr = sys.stderr
sys.stderr = open("/dev/null", "w")
os.kill(opid, signal.SIGTERM)
sleep(0.1)
os.kill(opid, signal.SIGINT)
sleep(0.1)
os.kill(opid, signal.SIGKILL)
sys.stderr = oldstderr
except Exception:
print(f"Process {opid} killed")
#
# Creating the pidfile of the currently running process
#
open(pidfile, 'w').write(pid)
#
# Making the function all_done being executed on exit of this process.
# all_done removes the pidfile
#
atexit.register(all_done)
app = Flask(__name__)
@app.route("/noresponse", methods=['POST'])
def noresponse():
sleep(10)
return Response(status=200)
@app.route("/noresponse/updateContext", methods=['POST'])
def unoresponse():
sleep(10)
return Response(status=200)
@app.route("/noresponse/queryContext", methods=['POST'])
def qnoresponse():
sleep(10)
return Response(status=200)
# This response has been designed to test the #2360 case, but is general enough to be
# used in other future cases (e.e. #3363)
@app.route("/badresponse/queryContext", methods=['POST'])
def bad_response():
r = Response(status=404)
r.data = '{"name":"ENTITY_NOT_FOUND","message":"The entity with the requested id [qa_name_01] was not found."}'
return r
# This response has been designed for 3068_ngsi_v2_based_forwarding/query_cpr_fail_but_local_results.test,
# but is general enough to be used in other future cases
@app.route("/badresponse/op/query", methods=['POST'])
def bad_response_device_not_found():
r = Response(status=404)
r.data = '{"name":"DEVICE_NOT_FOUND","message":"No device was found with id:E."}'
return r
# From:
# https://stackoverflow.com/questions/14902299/json-loads-allows-duplicate-keys-in-a-dictionary-overwriting-the-first-value
def dict_raise_on_duplicates(ordered_pairs):
"""Reject duplicate keys."""
d = {}
for k, v in ordered_pairs:
if k in d:
raise ValueError(f"duplicate key: {(k,)}")
else:
d[k] = v
return d
def sort_headers(headers):
"""
Sort headers in a predefined order. It seems that from the Python2 version of this
script (which used Flask==1.0.2) and the Python3 version (which uses Flask==2.0.2)
the order of the headers has changed. We sort to avoid change a lot of .test
expectations with no gain.
:param headers: the headers list to sort
"""
sorted = []
headers_order = [
'Fiware-Servicepath',
'Content-Length',
'X-Auth-Token',
'User-Agent',
'Ngsiv2-Attrsformat',
'Host',
'Accept',
'Fiware-Service',
'Content-Type',
'Fiware-Correlator',
]
# headers is a generator object, not exactly a list (i.e. it doesn't have remove method)
headers_list = list(headers)
for h in headers_order:
if h in headers_list:
sorted.append(h)
headers_list.remove(h)
# Remaining headers are added at the end of sorted array in the same order
for h in headers_list:
sorted.append(h)
return sorted
def record_request(request):
"""
Common function used by several route methods to save request content
:param request: the request to save
"""
global ac, t0, times
s = ''
# First request? Then, set reference datetime. Otherwise, add the
# timedelta to the list
if (t0 == ''):
t0 = datetime.now()
times.append(0)
else:
delta = datetime.now() - t0
t = delta.total_seconds()
times.append(trunc(round(t)))
# times.append(t)
# Store verb and URL
#
# We have found that request.url can be problematic in some distributions (e.g. Debian 8.2)
# when used with IPv6, so we use request.scheme, request.host and request.path to compose
# the URL "manually"
#
# request.url = request.scheme + '://' + request.host + request.path
#
s += request.method + ' ' + request.scheme + '://' + request.host + request.path
# Check for query params
params = ''
for k in request.args:
if (params == ''):
params = k + '=' + request.args[k]
else:
params += '&' + k + '=' + request.args[k]
if (params == ''):
s += '\n'
else:
s += '?' + params + '\n'
# Store headers (according to pre-defined order)
for h in sort_headers(request.headers.keys()):
s += h + ': ' + request.headers[h] + '\n'
# Store payload
if ((request.data is not None) and (len(request.data) != 0)):
s += '\n'
if pretty:
try:
raw = json.loads(request.data, object_pairs_hook=dict_raise_on_duplicates)
s += json.dumps(raw, indent=4, sort_keys=True)
s += '\n'
except ValueError as e:
s += str(e)
else:
s += request.data.decode("utf-8")
# Separator
s += '=======================================\n'
# Accumulate
ac += s
if verbose:
print(s)
def send_continue(request):
"""
Inspect request header in order to look if we have to continue or not
:param request: the request to look
:return: true if we have to continue, false otherwise
"""
for h in request.headers.keys():
if ((h == 'Expect') and (request.headers[h] == '100-continue')):
return True
return False
@app.route("/v1/updateContext", methods=['POST'])
@app.route("/v1/queryContext", methods=['POST'])
@app.route("/v2/op/query", methods=['POST'])
@app.route("/v2/op/update", methods=['POST'])
@app.route("/v2/entities", methods=['GET'])
@app.route(server_url, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
def record():
# Store request
record_request(request)
if send_continue(request):
return Response(status=100)
else:
return Response(status=200)
@app.route("/bug2871/updateContext", methods=['POST'])
def record_2871():
# Store request
record_request(request)
if send_continue(request):
return Response(status=100)
else:
# Ad hoc response related with issue #2871, see https://github.com/telefonicaid/fiware-orion/issues/2871
r = Response(status=200)
r.data = '''
{
"contextResponses": [
{
"contextElement": {
"attributes": [
{
"name": "turn",
"type": "string",
"value": ""
}
],
"id": "entity1",
"isPattern": false,
"type": "device"
},
"statusCode": {
"code": 200,
"reasonPhrase": "OK"
}
}
]
}
'''
return r
# Next 6 ones are for testing subscription status and failure logic. They are used by test
# 1126_GET_v2_subscriptions/lastsuccesscode_and_lastfailurereason.test
@app.route("/giveme200", methods=['POST'])
def giveme200():
return Response(status=200)
@app.route("/giveme400", methods=['POST'])
def giveme400():
return Response(status=400)
@app.route("/giveme404", methods=['POST'])
def giveme404():
return Response(status=404)
@app.route("/giveme500", methods=['POST'])
def giveme500():
return Response(status=500)
@app.route("/givemeDelay", methods=['POST'])
def givemeDelay():
sleep(60)
return Response(status=200)
@app.route("/waitForever", methods=['POST'])
def waitForever():
sleep(6000000000) # Arround 20 years.. close enough to "forever" :)
return Response(status=200)
@app.route('/dump', methods=['GET'])
def dump():
return ac
@app.route('/times', methods=['GET'])
def giveTimes():
return ', '.join(map(str, times)) + '\n'
@app.route('/number', methods=['GET'])
def number():
return str(len(times)) + '\n'
@app.route('/reset', methods=['POST'])
def reset():
global ac, t0, times
ac = ''
t0 = ''
times = []
return Response(status=200)
@app.route('/pid', methods=['GET'])
def getPid():
return str(os.getpid())
# This is the accumulation string
ac = ''
t0 = ''
times = []
lock = threading.Lock()
# The callback for when the client receives a CONNACK response from the MQTT broker
def on_connect(client, userdata, flags, rc):
print("MQTT broker connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(mqtt_topic)
# The callback for when a PUBLISH message is received from the MQTT broker
def on_message(client, userdata, msg):
global ac, t0, times, lock
lock.acquire()
s = ''
# FIXME: this is common code in record_request(). Unify.
# First notification? Then, set reference datetime. Otherwise, add the
# timedelta to the list
if (t0 == ''):
t0 = datetime.now()
times.append(0)
else:
delta = datetime.now() - t0
t = delta.total_seconds()
times.append(trunc(round(t)))
s += 'MQTT message at topic ' + msg.topic + ':'
s += '\n'
message = msg.payload.decode("utf-8")
if pretty:
raw = json.loads(message)
s += json.dumps(raw, indent=4, sort_keys=True)
s += '\n'
else:
s += str(message)
# Separator
s += '=======================================\n'
# Accumulate
ac += s
if verbose:
print(s)
lock.release()
ac = ''
t0 = ''
times = []
if __name__ == '__main__':
if mqtt_host is not None:
# Initialize the MQTT Client and set callback methods
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_host, mqtt_port, 60)
# Enable the processing of MQTT network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_start()
# Note that using debug=True breaks the procedure to write the PID into a file. In particular
# makes the calle os.path.isfile(pidfile) return True, even if the file doesn't exist. Thus,
# use debug=True below with care :)
if (https):
context = (cert_file, key_file)
app.run(host=host, port=port, debug=False, ssl_context=context)
else:
app.run(host=host, port=port)