aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/datafilecollector-testharness/mr-sim/mr-sim.py
blob: c37ae698ab6d50177478646cce7b8c08a2c722f5 (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
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__)

DEFAULT_IP = "localhost"


@app.route(
    "/events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12",
    methods=['GET'])
def MR_reply():
    global mr_counter
    global mr_replies

    mr_counter = mr_counter + 1
    print("MR receiver counter: " + str(mr_counter))

    if mr_replies[mr_counter].sleepMs != 0:
        sleep(mr_replies[mr_counter].sleepMs / 1000.0)
        print("Sleeping: " + str(mr_replies[mr_counter].sleepMs) + " ms")

    if mr_replies[mr_counter].replytype == 0:
        #print (str(mr_replies[mr_counter].jsonreply))
        print("Regular reply")
        response = app.response_class(
            response=mr_replies[mr_counter].jsonreply,
            status=200,
            mimetype='application/json')

        return response

    if mr_replies[mr_counter].replytype == 2:

        print("error: 404")
        response = app.response_class(
            response="",
            status=404,
            mimetype='application/json')

        return response

    if mr_replies[mr_counter].replytype == 1:
        print("do nothing, sink request")
        return


class Reply:
    """An instance of the reply event, which can be configured to behave in a certain way
    (delay, error code, reply body"""

    def to_json(self):
        return self.jsonreply

    def __init__(
            self,
            ip=DEFAULT_IP,
            file="1MB.tar.gz",
            sleepMs=0,
            replyType=0,
            port=1022,
            type="ftps"):
        self.sleepMs = sleepMs
        self.ip = ip
        self.file = file
        self.port = port
        self.replytype = replyType  # 0 for reply, 1 timeout, 2 deny
        self.user = "onap"
        self.passwd = "pano"
        self.type = type
        self.jsonreply = str.encode("""
        [{
          "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": "otenb5309",
              "sequence": 0,
              "domain": "notification",
              "lastEpochMicrosec": 8745745764578,
              "eventName": "Noti_RnNode-Ericsson_FileReady",
              "vesEventListenerVersion": "7.0.1",
              "sourceName": "oteNB5309"
            },
            "notificationFields": {
              "notificationFieldsVersion": "2.0",
              "changeType": "FileReady",
              "changeIdentifier": "PM_MEAS_FILES",
              "arrayOfNamedHashMap": [
                {
                  "name": \"""" +
                                    self.file +
                                    """",
                  "hashMap": {
                    "fileFormatType": "org.3GPP.32.435#measCollec",
                    "location": \"""" +
                                    self.type +
                                    """://""" +
                                    self.user +
                                    """:""" +
                                    self.passwd +
                                    """@""" +
                                    self.ip +
                                    """:""" +
                                    str(self.port) +
                                    """/""" +
                                    self.file +
                                    """",
                    "fileFormatVersion": "V10",
                    "compression": "gzip"
                  }
                }
              ]
            }
          }
        }]
        """)


def replyFactory(
        ip=DEFAULT_IP,
        file="1MB.tar.gz",
        factoryport=1022,
        count=1,
        factorytype="ftps"):
    aggregatedReply = ""
    # first item does not require .
    aggregatedReply = Reply(ip, file, port=factoryport).to_json()
    for i in range(count - 1):
        aggregatedReply = aggregatedReply + b", " + \
            Reply(ip, file, port=factoryport, type=factorytype).to_json()
    #print(b"aggregated reply: " + aggregatedReply)
    return b"[" + aggregatedReply + b"]"


def prepareMrRespArrSftp():
    global mr_replies

    for i in range(400):  # prepare 400 regular replies
        mr_replies.append(
            Reply(
                port=1022,
                ip="localhost",
                type="sftp",
                file="1MB.tar.gz"))
    #mr_replies[0] is not used


def prepareMrRespArrFtps():
    global mr_replies

    for i in range(400):
        mr_replies.append(
            Reply(
                port=21,
                ip="localhost",
                type="ftps",
                file="1MB.tar.gz"))


def tc1():
    prepareMrRespArrSftp()
    # no mutation needed in this TC


def tc2():
    global mr_replies

    for i in range(7):
        mr_replies.append(
            Reply(
                port=1022,
                ip="localhost",
                type="sftp",
                file="1MB.tar.gz"))

    # inserting and empty reply message
    mr_replies[1].jsonreply = b""
    mr_replies[2].jsonreply = b""

    # inserting a 404 error and delay
    mr_replies[3].replytype = 2
    mr_replies[3].sleepMs = 2000

    # inserting and empty reply message
    mr_replies[4].jsonreply = b""

    # sink the message
    mr_replies[5].replytype = 1

    # reply with one proper file finally
    mr_replies[6] = Reply(
        port=1022,
        ip="localhost",
        type="sftp",
        file="1MB.tar.gz")


def tc3():
    prepareMrRespArrFtps()


def tc4():
    global mr_replies

    for i in range(7):
        mr_replies.append(
            Reply(
                port=21,
                ip="localhost",
                type="ftps",
                file="1MB.tar.gz"))

    # inserting and empty reply message
    mr_replies[1].jsonreply = b""
    mr_replies[2].jsonreply = b""

    # inserting a 404 error and delay
    mr_replies[3].replytype = 2
    mr_replies[3].sleepMs = 2000

    # inserting and empty reply message
    mr_replies[4].jsonreply = b""

    # sink the message
    mr_replies[5].replytype = 1

    # reply with one proper file finally
    mr_replies[6] = Reply(
        port=21,
        ip="localhost",
        type="fftp",
        file="1MB.tar.gz")


if __name__ == "__main__":
    mr_replies = []
    mr_counter = 0  # counting hits reaching MR instance
    DR_block_single_req = 0

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--tc1',
        action='store_true',
        help='TC1: reply all queries with 1-1 files using SFTP')
    parser.add_argument(
        '--tc2',
        action='store_true',
        help='TC2: Reply according to error scenarios, then return 1 file finally for SFTP ---NOTE: updated keys required')
    parser.add_argument(
        '--tc3',
        action='store_true',
        help='TC3: reply all queries with 1-1 files using FTPS')
    parser.add_argument(
        '--tc4',
        action='store_true',
        help='TC4: Reply according to error scenarios, then return 1 file finally for FTPS ---NOTE: updated keys required')

    args = parser.parse_args()

    if args.tc1:
        print("TC: #1")
        tc1()
    elif args.tc2:
        print("TC: #2")
        tc2()
    elif args.tc3:
        print("TC: #3")
        tc3()
    elif args.tc4:
        print("TC: #4")
        tc4()

    else:
        print("No TC was defined")
        print("use --help for usage info")
        sys.exit()
    app.run(port=2222)