TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 1 | import argparse |
| 2 | import os |
| 3 | from werkzeug import secure_filename |
| 4 | from flask import Flask, render_template, request |
| 5 | from time import sleep |
| 6 | import sys |
| 7 | import json |
| 8 | from flask import Flask |
| 9 | app = Flask(__name__) |
| 10 | |
| 11 | DEFAULT_IP = "localhost" |
| 12 | |
| 13 | |
| 14 | @app.route( |
| 15 | "/events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12", |
| 16 | methods=['GET']) |
| 17 | def MR_reply(): |
| 18 | global mr_counter |
| 19 | global mr_replies |
| 20 | |
| 21 | mr_counter = mr_counter + 1 |
| 22 | print("MR receiver counter: " + str(mr_counter)) |
| 23 | |
| 24 | if mr_replies[mr_counter].sleepMs != 0: |
| 25 | sleep(mr_replies[mr_counter].sleepMs / 1000.0) |
| 26 | print("Sleeping: " + str(mr_replies[mr_counter].sleepMs) + " ms") |
| 27 | |
| 28 | if mr_replies[mr_counter].replytype == 0: |
| 29 | #print (str(mr_replies[mr_counter].jsonreply)) |
| 30 | print("Regular reply") |
| 31 | response = app.response_class( |
| 32 | response=mr_replies[mr_counter].jsonreply, |
| 33 | status=200, |
| 34 | mimetype='application/json') |
| 35 | |
| 36 | return response |
| 37 | |
| 38 | if mr_replies[mr_counter].replytype == 2: |
| 39 | |
| 40 | print("error: 404") |
| 41 | response = app.response_class( |
| 42 | response="", |
| 43 | status=404, |
| 44 | mimetype='application/json') |
| 45 | |
| 46 | return response |
| 47 | |
| 48 | if mr_replies[mr_counter].replytype == 1: |
| 49 | print("do nothing, sink request") |
| 50 | return |
| 51 | |
| 52 | |
| 53 | class Reply: |
| 54 | """An instance of the reply event, which can be configured to behave in a certain way |
| 55 | (delay, error code, reply body""" |
| 56 | |
| 57 | def to_json(self): |
| 58 | return self.jsonreply |
| 59 | |
| 60 | def __init__( |
| 61 | self, |
| 62 | ip=DEFAULT_IP, |
| 63 | file="1MB.tar.gz", |
| 64 | sleepMs=0, |
| 65 | replyType=0, |
| 66 | port=1022, |
| 67 | type="ftps"): |
| 68 | self.sleepMs = sleepMs |
| 69 | self.ip = ip |
| 70 | self.file = file |
| 71 | self.port = port |
| 72 | self.replytype = replyType # 0 for reply, 1 timeout, 2 deny |
| 73 | self.user = "onap" |
| 74 | self.passwd = "pano" |
| 75 | self.type = type |
| 76 | self.jsonreply = str.encode(""" |
| 77 | [{ |
| 78 | "event": { |
| 79 | "commonEventHeader": { |
| 80 | "startEpochMicrosec": 8745745764578, |
| 81 | "eventId": "FileReady_1797490e-10ae-4d48-9ea7-3d7d790b25e1", |
| 82 | "timeZoneOffset": "UTC+05.30", |
| 83 | "internalHeaderFields": { |
| 84 | "collectorTimeStamp": "Tue, 09 18 2018 10:56:52 UTC" |
| 85 | }, |
| 86 | "priority": "Normal", |
| 87 | "version": "4.0.1", |
| 88 | "reportingEntityName": "otenb5309", |
| 89 | "sequence": 0, |
| 90 | "domain": "notification", |
| 91 | "lastEpochMicrosec": 8745745764578, |
| 92 | "eventName": "Noti_RnNode-Ericsson_FileReady", |
| 93 | "vesEventListenerVersion": "7.0.1", |
| 94 | "sourceName": "oteNB5309" |
| 95 | }, |
| 96 | "notificationFields": { |
| 97 | "notificationFieldsVersion": "2.0", |
| 98 | "changeType": "FileReady", |
| 99 | "changeIdentifier": "PM_MEAS_FILES", |
| 100 | "arrayOfNamedHashMap": [ |
| 101 | { |
| 102 | "name": \"""" + |
| 103 | self.file + |
| 104 | """", |
| 105 | "hashMap": { |
| 106 | "fileFormatType": "org.3GPP.32.435#measCollec", |
| 107 | "location": \"""" + |
| 108 | self.type + |
| 109 | """://""" + |
| 110 | self.user + |
| 111 | """:""" + |
| 112 | self.passwd + |
| 113 | """@""" + |
| 114 | self.ip + |
| 115 | """:""" + |
| 116 | str(self.port) + |
| 117 | """/""" + |
| 118 | self.file + |
| 119 | """", |
| 120 | "fileFormatVersion": "V10", |
| 121 | "compression": "gzip" |
| 122 | } |
| 123 | } |
| 124 | ] |
| 125 | } |
| 126 | } |
| 127 | }] |
| 128 | """) |
| 129 | |
| 130 | |
| 131 | def replyFactory( |
| 132 | ip=DEFAULT_IP, |
| 133 | file="1MB.tar.gz", |
| 134 | factoryport=1022, |
| 135 | count=1, |
| 136 | factorytype="ftps"): |
| 137 | aggregatedReply = "" |
| 138 | # first item does not require . |
| 139 | aggregatedReply = Reply(ip, file, port=factoryport).to_json() |
| 140 | for i in range(count - 1): |
| 141 | aggregatedReply = aggregatedReply + b", " + \ |
| 142 | Reply(ip, file, port=factoryport, type=factorytype).to_json() |
| 143 | #print(b"aggregated reply: " + aggregatedReply) |
| 144 | return b"[" + aggregatedReply + b"]" |
| 145 | |
| 146 | |
| 147 | def prepareMrRespArrSftp(): |
| 148 | global mr_replies |
| 149 | |
| 150 | for i in range(400): # prepare 400 regular replies |
| 151 | mr_replies.append( |
| 152 | Reply( |
| 153 | port=1022, |
| 154 | ip="localhost", |
| 155 | type="sftp", |
| 156 | file="1MB.tar.gz")) |
| 157 | #mr_replies[0] is not used |
| 158 | |
| 159 | |
| 160 | def prepareMrRespArrFtps(): |
| 161 | global mr_replies |
| 162 | |
| 163 | for i in range(400): |
| 164 | mr_replies.append( |
| 165 | Reply( |
| 166 | port=21, |
| 167 | ip="localhost", |
| 168 | type="ftps", |
| 169 | file="1MB.tar.gz")) |
| 170 | |
| 171 | |
| 172 | def tc1(): |
| 173 | prepareMrRespArrSftp() |
| 174 | # no mutation needed in this TC |
| 175 | |
| 176 | |
| 177 | def tc2(): |
| 178 | global mr_replies |
| 179 | |
| 180 | for i in range(7): |
| 181 | mr_replies.append( |
| 182 | Reply( |
| 183 | port=1022, |
| 184 | ip="localhost", |
| 185 | type="sftp", |
| 186 | file="1MB.tar.gz")) |
| 187 | |
| 188 | # inserting and empty reply message |
| 189 | mr_replies[1].jsonreply = b"" |
| 190 | mr_replies[2].jsonreply = b"" |
| 191 | |
| 192 | # inserting a 404 error and delay |
| 193 | mr_replies[3].replytype = 2 |
| 194 | mr_replies[3].sleepMs = 2000 |
| 195 | |
| 196 | # inserting and empty reply message |
| 197 | mr_replies[4].jsonreply = b"" |
| 198 | |
| 199 | # sink the message |
| 200 | mr_replies[5].replytype = 1 |
| 201 | |
| 202 | # reply with one proper file finally |
| 203 | mr_replies[6] = Reply( |
| 204 | port=1022, |
| 205 | ip="localhost", |
| 206 | type="sftp", |
| 207 | file="1MB.tar.gz") |
| 208 | |
| 209 | |
| 210 | def tc3(): |
| 211 | prepareMrRespArrFtps() |
| 212 | |
| 213 | |
| 214 | def tc4(): |
| 215 | global mr_replies |
| 216 | |
| 217 | for i in range(7): |
| 218 | mr_replies.append( |
| 219 | Reply( |
| 220 | port=21, |
| 221 | ip="localhost", |
| 222 | type="ftps", |
| 223 | file="1MB.tar.gz")) |
| 224 | |
| 225 | # inserting and empty reply message |
| 226 | mr_replies[1].jsonreply = b"" |
| 227 | mr_replies[2].jsonreply = b"" |
| 228 | |
| 229 | # inserting a 404 error and delay |
| 230 | mr_replies[3].replytype = 2 |
| 231 | mr_replies[3].sleepMs = 2000 |
| 232 | |
| 233 | # inserting and empty reply message |
| 234 | mr_replies[4].jsonreply = b"" |
| 235 | |
| 236 | # sink the message |
| 237 | mr_replies[5].replytype = 1 |
| 238 | |
| 239 | # reply with one proper file finally |
| 240 | mr_replies[6] = Reply( |
| 241 | port=21, |
| 242 | ip="localhost", |
| 243 | type="fftp", |
| 244 | file="1MB.tar.gz") |
| 245 | |
| 246 | |
| 247 | if __name__ == "__main__": |
| 248 | mr_replies = [] |
| 249 | mr_counter = 0 # counting hits reaching MR instance |
| 250 | DR_block_single_req = 0 |
| 251 | |
| 252 | parser = argparse.ArgumentParser() |
| 253 | parser.add_argument( |
| 254 | '--tc1', |
| 255 | action='store_true', |
| 256 | help='TC1: reply all queries with 1-1 files using SFTP') |
| 257 | parser.add_argument( |
| 258 | '--tc2', |
| 259 | action='store_true', |
| 260 | help='TC2: Reply according to error scenarios, then return 1 file finally for SFTP ---NOTE: updated keys required') |
| 261 | parser.add_argument( |
| 262 | '--tc3', |
| 263 | action='store_true', |
| 264 | help='TC3: reply all queries with 1-1 files using FTPS') |
| 265 | parser.add_argument( |
| 266 | '--tc4', |
| 267 | action='store_true', |
| 268 | help='TC4: Reply according to error scenarios, then return 1 file finally for FTPS ---NOTE: updated keys required') |
| 269 | |
| 270 | args = parser.parse_args() |
| 271 | |
| 272 | if args.tc1: |
| 273 | print("TC: #1") |
| 274 | tc1() |
| 275 | elif args.tc2: |
| 276 | print("TC: #2") |
| 277 | tc2() |
| 278 | elif args.tc3: |
| 279 | print("TC: #3") |
| 280 | tc3() |
| 281 | elif args.tc4: |
| 282 | print("TC: #4") |
| 283 | tc4() |
| 284 | |
| 285 | else: |
| 286 | print("No TC was defined") |
| 287 | print("use --help for usage info") |
| 288 | sys.exit() |
| 289 | app.run(port=2222) |