summaryrefslogtreecommitdiffstats
path: root/test/mocks/datafilecollector-testharness/mr-sim/mr-sim.py
blob: ef46535f5719d1dfbcef2d46fa0b874391705db7 (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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import argparse
import os
from werkzeug import secure_filename
from flask import Flask, render_template, request
from time import sleep
import sys
import json
from flask import Flask
app = Flask(__name__)

#Server info
HOST_IP = "0.0.0.0"
HOST_PORT = 2222

#Test function to check server running
@app.route('/',
    methods=['GET'])
def index():
    return 'Hello world'

#Returns number of polls
@app.route('/ctr_requests',
    methods=['GET'])
def counter_requests():
    global ctr_requests
    return str(ctr_requests)

#Returns number of replies
@app.route('/ctr_responses',
    methods=['GET'])
def counter_responses():
    global ctr_responses
    return str(ctr_responses)

#Returns number of unique files
@app.route('/ctr_unique_files',
    methods=['GET'])
def counter_uniquefiles():
    global fileMap
    return str(len(fileMap))

#Returns tc info
@app.route('/tc_info',
    methods=['GET'])
def testcase_info():
    global tc_num
    return tc_num

#Messages polling function
@app.route(
    "/events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12",
    methods=['GET'])
def MR_reply():
    global ctr_requests
    global args

    ctr_requests = ctr_requests + 1
    print("MR: poll request#: " + str(ctr_requests))

    if args.tc100:
      return tc100("sftp")
    elif args.tc101:
      return tc101("sftp")
    elif args.tc102:
      return tc102("sftp")

    elif args.tc110:
      return tc110("sftp")
    elif args.tc111:
      return tc111("sftp")
    elif args.tc112:
      return tc112("sftp")
    elif args.tc113:
      return tc113("sftp")

    elif args.tc120:
      return tc120("sftp")
    elif args.tc121:
      return tc121("sftp")
    elif args.tc122:
      return tc122("sftp")

    elif args.tc1000:
      return tc1000("sftp")
    elif args.tc1001:
      return tc1001("sftp")

    elif args.tc510:
      return tc510("sftp")      


    elif args.tc200:
      return tc200("ftps")
    elif args.tc201:
      return tc201("ftps")
    elif args.tc202:
      return tc202("ftps")

    elif args.tc210:
      return tc210("ftps")
    elif args.tc211:
      return tc211("ftps")
    elif args.tc212:
      return tc212("ftps")
    elif args.tc213:
      return tc213("ftps")

    elif args.tc220:
      return tc220("ftps")
    elif args.tc221:
      return tc221("ftps")
    elif args.tc222:
      return tc222("ftps")

    elif args.tc2000:
      return tc2000("ftps")
    elif args.tc2001:
      return tc2001("ftps")

    elif args.tc610:
      return tc510("ftps")     


#### Test case functions


def tc100(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 1):
    return buildOkResponse("[]")

  seqNr = (ctr_responses-1)
  msg = getEventHead() + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022) + getEventEnd()
  fileMap[seqNr] = seqNr
  return buildOkResponse("["+msg+"]")

def tc101(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 1):
    return buildOkResponse("[]")  
 
  seqNr = (ctr_responses-1)
  msg = getEventHead() + getEventName("5MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022) + getEventEnd()
  fileMap[seqNr] = seqNr

  return buildOkResponse("["+msg+"]")

def tc102(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 1):
    return buildOkResponse("[]")  

  seqNr = (ctr_responses-1)
  msg = getEventHead() + getEventName("50MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022) + getEventEnd()
  fileMap[seqNr] = seqNr

  return buildOkResponse("["+msg+"]")

def tc110(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 100):
    return buildOkResponse("[]")  
  
  seqNr = (ctr_responses-1)
  msg = getEventHead() + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022) + getEventEnd()
  fileMap[seqNr] = seqNr

  return buildOkResponse("["+msg+"]")

def tc111(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 100):
    return buildOkResponse("[]")  
  
  msg = getEventHead()

  for i in range(100):
    seqNr = i+(ctr_responses-1)
    if i != 0: msg = msg + ","
    msg = msg + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
    fileMap[seqNr] = seqNr

  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

def tc112(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 100):
    return buildOkResponse("[]")  
  
  msg = getEventHead()

  for i in range(100):
    seqNr = i+(ctr_responses-1)
    if i != 0: msg = msg + ","
    msg = msg + getEventName("5MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
    fileMap[seqNr] = seqNr

  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

def tc113(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 1):
    return buildOkResponse("[]")  
  
  msg = ""

  for evts in range(100):  # build 100 evts
    if (evts > 0):
      msg = msg + ","
    msg = msg + getEventHead()
    for i in range(100):   # build 100 files
      seqNr = i+evts+100*(ctr_responses-1)
      if i != 0: msg = msg + ","
      msg = msg + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
      fileMap[seqNr] = seqNr

    msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")


def tc120(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 100):
    return buildOkResponse("[]")  

  if (ctr_responses % 10 == 2):
    return  # Return nothing
  
  if (ctr_responses % 10 == 3):
    return buildOkResponse("") # Return empty message

  if (ctr_responses % 10 == 4):
    return buildOkResponse(getEventHead()) # Return part of a json event

  if (ctr_responses % 10 == 5):
    return buildEmptyResponse(404) # Return empty message with status code

  if (ctr_responses % 10 == 6):
    sleep(60)

  
  msg = getEventHead()

  for i in range(100):
    seqNr = i+(ctr_responses-1)
    if i != 0: msg = msg + ","
    msg = msg + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
    fileMap[seqNr] = seqNr

  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

def tc121(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 100):
    return buildOkResponse("[]")  
  
  msg = getEventHead()

  for i in range(100):
    seqNr = i+(ctr_responses-1)
    if (seqNr%10 == 0):     # Every 10th file is "missing"
      fn = "MissingFile_" + str(seqNr) + ".tar.gz"
    else:
      fn = "1MB_" + str(seqNr) + ".tar.gz"
      fileMap[seqNr] = seqNr

    if i != 0: msg = msg + ","
    msg = msg + getEventName(fn,ftptype,"onap","pano","localhost",1022)
    

  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

def tc122(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 100):
    return buildOkResponse("[]")  
  
  msg = getEventHead()

  for i in range(100):
    fn = "1MB_0.tar.gz"  # All files identical names
    if i != 0: msg = msg + ","
    msg = msg + getEventName(fn,ftptype,"onap","pano","localhost",1022)

  fileMap[0] = 0
  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")


def tc1000(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  msg = getEventHead()

  for i in range(100):
    seqNr = i+(ctr_responses-1)
    if i != 0: msg = msg + ","
    msg = msg + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
    fileMap[seqNr] = seqNr

  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

def tc1001(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  msg = getEventHead()

  for i in range(100):
    seqNr = i+(ctr_responses-1)
    if i != 0: msg = msg + ","
    msg = msg + getEventName("5MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
    fileMap[seqNr] = seqNr

  msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

def tc510(ftptype):
  global ctr_responses
  global ctr_unique_files

  ctr_responses = ctr_responses + 1

  if (ctr_responses > 5):
    return buildOkResponse("[]")  

  msg = ""

  for evts in range(700):  # build events for 5 MEs
    if (evts > 0):
      msg = msg + ","
    msg = msg + getEventHeadNodeName("PNF"+str(evts))
    seqNr = (ctr_responses-1)
    msg = msg + getEventName("1MB_" + str(seqNr) + ".tar.gz",ftptype,"onap","pano","localhost",1022)
    seqNr = seqNr + evts*1000000 #Create unique id for this node and file
    fileMap[seqNr] = seqNr
    msg = msg + getEventEnd()

  return buildOkResponse("["+msg+"]")

#Mapping FTPS TCs
def tc200(ftptype):
  return tc100(ftptype)
def tc201(ftptype):
  return tc101(ftptype)
def tc202(ftptype):
  return tc102(ftptype)

def tc210(ftptype):
  return tc110(ftptype)
def tc211(ftptype):
  return tc111(ftptype)
def tc212(ftptype):
  return tc112(ftptype)
def tc213(ftptype):
  return tc113(ftptype)

def tc220(ftptype):
  return tc120(ftptype)
def tc221(ftptype):
  return tc121(ftptype)
def tc222(ftptype):
  return tc122(ftptype)

def tc2000(ftptype):
  return tc1000(ftptype)
def tc2001(ftptype):
  return tc1001(ftptype)

#### Functions to build json messages and respones ####

# Function to build fixed beginning of an event
def getEventHead():
  return getEventHeadNodeName("oteNB5309")

def getEventHeadNodeName(nodename):
  headStr = """
        {
          "event": {
            "commonEventHeader": {
              "startEpochMicrosec": 8745745764578,
              "eventId": "FileReady_1797490e-10ae-4d48-9ea7-3d7d790b25e1",
              "timeZoneOffset": "UTC+05.30",
              "internalHeaderFields": {
                "collectorTimeStamp": "Tue, 09 18 2018 10:56:52 UTC"
              },
              "priority": "Normal",
              "version": "4.0.1",
              "reportingEntityName": \"""" + nodename + """",
              "sequence": 0,
              "domain": "notification",
              "lastEpochMicrosec": 8745745764578,
              "eventName": "Noti_RnNode-Ericsson_FileReady",
              "vesEventListenerVersion": "7.0.1",
              "sourceName": \"""" + nodename + """"
            },
            "notificationFields": {
              "notificationFieldsVersion": "2.0",
              "changeType": "FileReady",
              "changeIdentifier": "PM_MEAS_FILES",
              "arrayOfNamedHashMap": [
          """ 
  return headStr

# Function to build the variable part of an event
def getEventName(fn,type,user,passwd,ip,port):
    nameStr =        """{
                  "name": \"""" + fn + """",
                  "hashMap": {
                    "fileFormatType": "org.3GPP.32.435#measCollec",
                    "location": \"""" + type + """://""" + user + """:""" + passwd + """@""" + ip + """:""" + str(port) + """/""" + fn + """",
                    "fileFormatVersion": "V10",
                    "compression": "gzip"
                  }
                } """
    return nameStr

# Function to build fixed end of an event
def getEventEnd():
    endStr =  """
              ]
            }
          }
        }
        """
    return endStr

# Function to build an OK reponse from a message string
def buildOkResponse(msg):
  response = app.response_class(
      response=str.encode(msg),
      status=200,
      mimetype='application/json')
  return response

# Function to build an empty message with status
def buildEmptyResponse(status_code):
  response = app.response_class(
      response=str.encode(""),
      status=status_code,
      mimetype='application/json')
  return response


if __name__ == "__main__":
  
    #Counters
    ctr_responses = 0
    ctr_requests = 0
    ctr_unique_files = 0

    #Keeps all reponded file names
    fileMap = {}

    tc_num = "Not set"
    tc_help = "Not set"

    parser = argparse.ArgumentParser()

#SFTP TCs with single ME 
    parser.add_argument(
        '--tc100',
        action='store_true',
        help='TC100 - One ME, SFTP, 1 1MB file, 1 event')
    parser.add_argument(
        '--tc101',
        action='store_true',
        help='TC101 - One ME, SFTP, 1 5MB file, 1 event')
    parser.add_argument(
        '--tc102',
        action='store_true',
        help='TC102 - One ME, SFTP, 1 50MB file, 1 event')

    parser.add_argument(
        '--tc110',
        action='store_true',
        help='TC110 - One ME, SFTP, 1MB files, 1 file per event, 100 events, 1 event per poll.')
    parser.add_argument(
        '--tc111',
        action='store_true',
        help='TC111 - One ME, SFTP, 1MB files, 100 files per event, 100 events, 1 event per poll.')
    parser.add_argument(
        '--tc112',
        action='store_true',
        help='TC112 - One ME, SFTP, 5MB files, 100 files per event, 100 events, 1 event per poll.')
    parser.add_argument(
        '--tc113',
        action='store_true',
        help='TC113 - One ME, SFTP, 1MB files, 100 files per event, 100 events. All events in one poll.')

    parser.add_argument(
        '--tc120',
        action='store_true',
        help='TC120 - One ME, SFTP, 1MB files, 100 files per event, 100 events, 1 event per poll. 10% of replies each: no response, empty message, slow response, 404-error, malformed json')
    parser.add_argument(
        '--tc121',
        action='store_true',
        help='TC121 - One ME, SFTP, 1MB files, 100 files per event, 100 events, 1 event per poll. 10% missing files')
    parser.add_argument(
        '--tc122',
        action='store_true',
        help='TC122 - One ME, SFTP, 1MB files, 100 files per event, 100 events. 1 event per poll. All files with identical name. ')

    parser.add_argument(
        '--tc1000',
        action='store_true',
        help='TC1000 - One ME, SFTP, 1MB files, 100 files per event, endless number of events, 1 event per poll')
    parser.add_argument(
        '--tc1001',
        action='store_true',
        help='TC1001 - One ME, SFTP, 5MB files, 100 files per event, endless number of events, 1 event per poll')

# SFTP TCs with multiple MEs
    parser.add_argument(
        '--tc510',
        action='store_true',
        help='TC510 - 5 MEs, SFTP, 1MB files, 1 file per event, 100 events, 1 event per poll.')



# FTPS TCs with single ME
    parser.add_argument(
        '--tc200',
        action='store_true',
        help='TC200 - One ME, FTPS, 1 1MB file, 1 event')
    parser.add_argument(
        '--tc201',
        action='store_true',
        help='TC201 - One ME, FTPS, 1 5MB file, 1 event')
    parser.add_argument(
        '--tc202',
        action='store_true',
        help='TC202 - One ME, FTPS, 1 50MB file, 1 event')

    parser.add_argument(
        '--tc210',
        action='store_true',
        help='TC210 - One ME, FTPS, 1MB files, 1 file per event, 100 events, 1 event per poll.')
    parser.add_argument(
        '--tc211',
        action='store_true',
        help='TC211 - One ME, FTPS, 1MB files, 100 files per event, 100 events, 1 event per poll.')
    parser.add_argument(
        '--tc212',
        action='store_true',
        help='TC212 - One ME, FTPS, 5MB files, 100 files per event, 100 events, 1 event per poll.')
    parser.add_argument(
        '--tc213',
        action='store_true',
        help='TC213 - One ME, FTPS, 1MB files, 100 files per event, 100 events. All events in one poll.')

    parser.add_argument(
        '--tc220',
        action='store_true',
        help='TC220 - One ME, FTPS, 1MB files, 100 files per event, 100 events, 1 event per poll. 10% of replies each: no response, empty message, slow response, 404-error, malformed json')
    parser.add_argument(
        '--tc221',
        action='store_true',
        help='TC221 - One ME, FTPS, 1MB files, 100 files per event, 100 events, 1 event per poll. 10% missing files')
    parser.add_argument(
        '--tc222',
        action='store_true',
        help='TC222 - One ME, FTPS, 1MB files, 100 files per event, 100 events. 1 event per poll. All files with identical name. ')

    parser.add_argument(
        '--tc2000',
        action='store_true',
        help='TC2000 - One ME, FTPS, 1MB files, 100 files per event, endless number of events, 1 event per poll')
    parser.add_argument(
        '--tc2001',
        action='store_true',
        help='TC2001 - One ME, FTPS, 5MB files, 100 files per event, endless number of events, 1 event per poll')    

    parser.add_argument(
        '--tc610',
        action='store_true',
        help='TC510 - 5 MEs, FTPS, 1MB files, 1 file per event, 100 events, 1 event per poll.')

    args = parser.parse_args()

    

    if args.tc100:
        tc_num = "TC# 100"
    elif args.tc101:
        tc_num = "TC# 101"
    elif args.tc102:
        tc_num = "TC# 102"

    elif args.tc110:
        tc_num = "TC# 110"
    elif args.tc111:
        tc_num = "TC# 111"
    elif args.tc112:
        tc_num = "TC# 112"
    elif args.tc113:
        tc_num = "TC# 113"

    elif args.tc120:
        tc_num = "TC# 120"
    elif args.tc121:
        tc_num = "TC# 121"
    elif args.tc122:
        tc_num = "TC# 122"

    elif args.tc1000:
        tc_num = "TC# 1000"
    elif args.tc1001:
        tc_num = "TC# 1001"

    elif args.tc510:
        tc_num = "TC# 510"

    elif args.tc200:
        tc_num = "TC# 200"
    elif args.tc201:
        tc_num = "TC# 201"
    elif args.tc202:
        tc_num = "TC# 202"

    elif args.tc210:
        tc_num = "TC# 210"
    elif args.tc211:
        tc_num = "TC# 211"
    elif args.tc212:
        tc_num = "TC# 212"
    elif args.tc213:
        tc_num = "TC# 213"

    elif args.tc220:
        tc_num = "TC# 220"
    elif args.tc221:
        tc_num = "TC# 221"
    elif args.tc222:
        tc_num = "TC# 222"

    elif args.tc2000:
        tc_num = "TC# 2000"
    elif args.tc2001:
        tc_num = "TC# 2001"

    elif args.tc610:
        tc_num = "TC# 610"

    else:
        print("No TC was defined")
        print("use --help for usage info")
        sys.exit()

    print(tc_num)
 
    app.run(port=HOST_PORT, host=HOST_IP)