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
|
#!/usr/bin/env python2
###
# ============LICENSE_START=======================================================
# ONAP CLAMP
# ================================================================================
# Copyright (C) 2018 AT&T Intellectual Property. All rights
# reserved.
# Modifications Copyright (C) 2021 Nordix Foundation.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END============================================
# ===================================================================
#
###
import json
import requests
import os
import errno
import SimpleHTTPServer
import SocketServer
import argparse
import tempfile
import signal
import uuid
import shutil
parser = argparse.ArgumentParser(description="3rd party Cache & Replay")
parser.add_argument("--username", "-u", type=str, help="Set the username for contacting 3rd party - only used for GET")
parser.add_argument("--password", "-p", type=str, help="Set the password for contacting 3rd party - only used for GET")
parser.add_argument("--root", "-r", default=tempfile.mkdtemp(), type=str, help="Root folder for the proxy cache")
parser.add_argument("--temp", "-t", default=tempfile.mkdtemp(), type=str, help="Temp folder for the generated content")
parser.add_argument("--proxy" , type=str, help="Url of the Act as a proxy. If not set, this script only uses the cache and will return a 404 if files aren't found")
parser.add_argument("--port", "-P", type=int, default="8081", help="Port on which the proxy should listen to")
parser.add_argument("--verbose", "-v", type=bool, help="print(more information in case of error")
parser.add_argument("--proxyaddress","-a", type=str, help="Address of this proxy, generally either third-party-proxy:8085 or localhost:8085 depending if started with docker-compose or not")
options = parser.parse_args()
PORT = options.port
HOST = options.proxy
AUTH = (options.username, options.password)
HEADERS = {'X-ECOMP-InstanceID':'CLAMP'}
CACHE_ROOT = str(options.root)
TMP_ROOT = str(options.temp)
PROXY_ADDRESS=str(options.proxyaddress)
def signal_handler(signal_sent, frame):
global httpd
if signal_sent == signal.SIGINT:
print('Got Ctrl-C (SIGINT)')
httpd.socket.close()
httpd.shutdown()
httpd.server_close()
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def print_headers(self):
for header,value in self.headers.items():
print("header: %s : %s" % (header, value))
def check_credentials(self):
pass
def _send_content(self, header_file, content_file):
self.send_response(200)
with open(header_file, 'rb') as f:
headers = json.load(f)
for key,value in headers.items():
if key in ('Transfer-Encoding',):
continue
self.send_header(key, value)
self.end_headers()
with open(content_file,'rb') as f:
fc = f.read()
self.wfile.write(fc)
def _write_cache(self,cached_file_folder, header_file, content_file, response):
os.makedirs(cached_file_folder, 0o777)
with open(content_file, 'w') as f:
f.write(response.raw.read())
with open(header_file, 'w') as f:
json.dump(dict(response.raw.headers), f)
# Entry point of the code
def _get_cached_file_folder_name(self,folder):
cached_file_folder = '%s/%s' % (folder, self.path,)
print("Cached file name before escaping : %s" % cached_file_folder)
cached_file_folder = cached_file_folder.replace('+','').replace('<','<').replace('>','>').replace('?','?').replace('*','*').replace('\\','*').replace(':',':').replace('|','|')
print("Cached file name after escaping (used for cache storage) : %s" % cached_file_folder)
return cached_file_folder
def _get_cached_content_file_name(self,cached_file_folder):
return "%s/.file" % (cached_file_folder,)
def _get_cached_header_file_name(self,cached_file_folder):
return "%s/.header" % (cached_file_folder,)
def _create_cache(self, generated_json, cached_file_folder, cached_file_header, cached_file_content):
print("jsonGenerated: " + generated_json)
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o775)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len(generated_json)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w+') as f:
f.write(generated_json)
def _execute_content_generated_cases(self,http_type):
print("Testing special cases, cache files will be sent to :" +TMP_ROOT)
cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
_file_available = os.path.exists(cached_file_content)
if self.path.startswith("/dcae-service-types?asdcResourceId=") and http_type == "GET":
if not _file_available:
print("self.path start with /dcae-service-types?asdcResourceId=, generating response json...")
uuidGenerated = str(uuid.uuid4())
typeId = "typeId-" + uuidGenerated
typeName = "typeName-" + uuidGenerated
print("typeId generated: " + typeName + " and typeName: "+ typeId)
jsonGenerated = "{\"totalCount\":1, \"items\":[{\"typeId\":\"" + typeId + "\", \"typeName\":\"" + typeName +"\"}]}"
print("jsonGenerated: " + jsonGenerated)
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w') as f:
f.write(jsonGenerated)
return True
elif self.path.startswith("/dcae-operationstatus/install") and http_type == "GET":
if not _file_available:
print("self.path start with /dcae-operationstatus/install, generating response json...")
jsonGenerated = "{\"operationType\": \"install\", \"status\": \"succeeded\"}"
print("jsonGenerated: " + jsonGenerated)
try:
os.makedirs(cached_file_folder, 0o777)
except OSError as e:
if e.errno != errno.EEXIST:
raise
print(cached_file_folder+" already exists")
with open(cached_file_header, 'w') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w') as f:
f.write(jsonGenerated)
return True
elif self.path.startswith("/dcae-operationstatus/uninstall") and http_type == "GET":
if not _file_available:
print("self.path start with /dcae-operationstatus/uninstall, generating response json...")
jsonGenerated = "{\"operationType\": \"uninstall\", \"status\": \"succeeded\"}"
print("jsonGenerated: " + jsonGenerated)
try:
os.makedirs(cached_file_folder, 0o777)
except OSError as e:
if e.errno != errno.EEXIST:
raise
print(cached_file_folder+" already exists")
with open(cached_file_header, 'w') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w') as f:
f.write(jsonGenerated)
return True
elif self.path.startswith("/sdc/v1/catalog/services/") and http_type == "POST":
if not _file_available:
print("self.path start with /sdc/v1/catalog/services/, generating response json...")
jsondata = json.loads(self.data_string)
jsonGenerated = "{\"artifactName\":\"" + jsondata['artifactName'] + "\",\"artifactType\":\"" + jsondata['artifactType'] + "\",\"artifactURL\":\"" + self.path + "\",\"artifactDescription\":\"" + jsondata['description'] + "\",\"artifactChecksum\":\"ZjJlMjVmMWE2M2M1OTM2MDZlODlmNTVmZmYzNjViYzM=\",\"artifactUUID\":\"" + str(uuid.uuid4()) + "\",\"artifactVersion\":\"1\"}"
print("jsonGenerated: " + jsonGenerated)
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w') as f:
f.write(jsonGenerated)
return True
elif self.path.startswith("/dcae-deployments/") and http_type == "PUT":
print("self.path start with /dcae-deployments/ DEPLOY, generating response json...")
#jsondata = json.loads(self.data_string)
jsonGenerated = "{\"operationType\":\"install\",\"status\":\"processing\",\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus/install?test=test\"}}"
print("jsonGenerated: " + jsonGenerated)
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w+') as f:
f.write(jsonGenerated)
return True
elif self.path.startswith("/dcae-deployments/") and http_type == "DELETE":
print("self.path start with /dcae-deployments/ UNDEPLOY, generating response json...")
#jsondata = json.loads(self.data_string)
jsonGenerated = "{\"operationType\":\"uninstall\",\"status\":\"processing\",\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus/uninstall?test=test\"}}"
print("jsonGenerated: " + jsonGenerated)
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w+') as f:
f.write(jsonGenerated)
return True
elif (self.path.startswith("/pdp/api/") and (http_type == "PUT" or http_type == "DELETE")) or (self.path.startswith("/pdp/api/policyEngineImport") and http_type == "POST"):
print("self.path start with /pdp/api/, copying body to response ...")
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}")
with open(cached_file_content, 'w+') as f:
f.write(self.data_string)
return True
elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "POST":
print("self.path start with POST new policy API /pdp/api/, copying body to response ...")
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}")
with open(cached_file_content, 'w+') as f:
f.write(self.data_string)
return True
elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "DELETE":
print("self.path start with DELETE new policy API /policy/api/v1/policytypes/ ...")
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len("")) + "\", \"Content-Type\": \""+str("")+"\"}")
with open(cached_file_content, 'w+') as f:
f.write(self.data_string)
return True
elif self.path.startswith("/policy/pap/v1/pdps/policies") and http_type == "POST":
print("self.path start with POST new policy API /policy/pap/v1/pdps/ ...")
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len("")) + "\", \"Content-Type\": \""+str("")+"\"}")
with open(cached_file_content, 'w+') as f:
f.write(self.data_string)
return True
elif (self.path.startswith("/policy/pap/v1/policies/deployed/")) and http_type == "GET":
print("self.path start with /policy/api/v1/policytypes/, generating response json...")
jsonGenerated = "{\"policyTypeId\": \"onap.policies.controlloop.operational\",\"policyTypeVersion\": \"1.0.0\",\"policyId\": \"OPERATIONAL_z711F_v1_0_ResourceInstanceName1_tca\"}"
print("jsonGenerated: " + jsonGenerated)
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w') as f:
f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w') as f:
f.write(jsonGenerated)
return True
elif self.path.startswith("/dcae-service-types") and http_type == "GET":
if not _file_available:
self.path = "/dcae-service-types"
cached_file_folder = '%s/%s' % (TMP_ROOT, self.path)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
print("self.path start with /dcae-service-types, generating response json...")
response = "{\"links\": {\"previousLink\": {\"title\": \"string\",\"rel\": \"string\",\"uri\": \"string\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"},\"nextLink\": {\"title\": \"string\",\"rel\": \"string\",\"uri\": \"string\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"}},\"totalCount\": 1,\"items\": [{\"owner\": \"testOwner\",\"application\": \"testApplication\",\"component\": \"testComponent\",\"typeName\": \"testTypeName\",\"typeVersion\": 0,\"blueprintTemplate\": \"testBlueprintTemplate\",\"serviceIds\": [\"serviceId1\", \"serviceId2\"],\"vnfTypes\": [\"vnfType1\", \"vnfType2\"],\"serviceLocations\": [\"serviceLocation1\", \"serviceLocation2\"],\"asdcServiceId\": \"testAsdcServiceId\",\"asdcResourceId\": \"0\",\"asdcServiceURL\": \"testAsdcServiceURL\",\"typeId\": \"testtypeId\",\"selfLink\": {\"title\": \"selfLinkTitle\",\"rel\": \"selfLinkRel\",\"uri\": \"selfLinkUri\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"},\"created\": \"2020-01-22T09:38:15.436Z\",\"deactivated\": \"2020-01-22T09:38:15.437Z\"},{\"owner\": \"testOwner2\",\"application\": \"testApplication1\",\"component\": \"testComponent2\",\"typeName\": \"testTypeName2\",\"typeVersion\": 0,\"blueprintTemplate\": \"testBlueprintTemplate2\",\"serviceIds\": [\"serviceId3\", \"serviceId4\"],\"vnfTypes\": [\"vnfType13\", \"vnfType4\"],\"serviceLocations\": [\"serviceLocation3\", \"serviceLocation4\"],\"asdcServiceId\": \"testAsdcServiceId\",\"asdcResourceId\": \"1\",\"asdcServiceURL\": \"testAsdcServiceURL2\",\"typeId\": \"testtypeId2\",\"selfLink\": {\"title\": \"selfLinkTitle\",\"rel\": \"selfLinkRel\",\"uri\": \"selfLinkUri\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"},\"created\": \"2020-01-22T09:38:15.436Z\",\"deactivated\": \"2020-01-22T09:38:15.437Z\"}]}"
print("jsonGenerated: " + response)
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w') as f:
f.write("{\"Content-Length\": \"" + str(len(response)) + "\", \"Content-Type\": \"application/json\"}")
with open(cached_file_content, 'w') as f:
f.write(response)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission/toscaservicetemplate")) and http_type == "GET":
if not _file_available:
cached_file_folder = cached_file_folder.split('bridgeEndpoint')[0]
print(("cached file folder for onap is %s: ", cached_file_folder))
print("self.path start with /onap/controlloop/v2/commission/, generating response json...")
jsonGenerated = "{\"tosca_definitions_version\": \"tosca_simple_yaml_1_1_0\",\"data_types\": {},\"node_types\": {}, \"policy_types\": {}, \"topology_template\": {}, \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\", \"metadata\": {}}"
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission/toscaServiceTemplateSchema")) and http_type == "GET":
if not _file_available:
cached_file_folder = cached_file_folder.split('bridgeEndpoint')[0]
print(("cached file folder for onap is %s: ", cached_file_folder))
print("self.path start with /onap/controlloop/v2/commission/, generating response json...")
jsonGenerated = "{\"tosca_definitions_version\": \"tosca_simple_yaml_1_1_0\",\"data_types\": {},\"node_types\": {}, \"policy_types\": {}, \"topology_template\": {}, \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\", \"metadata\": {}}"
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission/elements")) and http_type == "GET":
if not _file_available:
print("self.path start with /commission/elements Control Loop Elements, generating response json...")
jsonGenerated = "[{\"name\": ,\"org.onap.domain.pmsh.PMSH_DCAEMicroservice\": [{ \"version\": \"1.2.3\", \"derived_from\": null }]}]"
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission")) and http_type == "GET":
if not _file_available:
print("self.path start with /commission control loop definition, generating response json...")
#jsondata = json.loads(self.data_string)
jsonGenerated = "[{\"name\": ,\"org.onap.domain.pmsh.PMSHControlLoopDefinition\": [{ \"version\": \"1.2.3\", \"derived_from\": null }]}]"
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission/getCommonOrInstanceProperties")) and http_type == "GET":
if not _file_available:
print("self.path start with /commission getting common properties, generating response json...")
with open("example/node_template.json", "r") as f:
jsonGenerated = f.read()
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission")) and http_type == "POST":
print("self.path start with POST /onap/controlloop/v2/commission, copying body to response ...")
if not os.path.exists(cached_file_folder):
os.makedirs(cached_file_folder, 0o777)
with open(cached_file_header, 'w+') as f:
f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}")
with open(cached_file_content, 'w+') as f:
f.write(self.data_string)
return True
elif (self.path.startswith("/onap/controlloop/v2/commission")) and http_type == "DELETE":
print("self.path start with /commission Decommissioning, generating response json...")
jsonGenerated = "{\"errorDetails\": null,\"affectedControlLoopDefinitions\": [{ \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\" }]}"
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
elif (self.path.startswith("/onap/controlloop/v2/instantiation")) and http_type == "GET":
print("self.path start with /instantiation Retrieving Instantiation, generating response json...")
jsonGenerated = "{\"errorDetails\": null,\"affectedControlLoopDefinitions\": [{ \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\" }]}"
self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content)
return True
else:
return False
def do_GET(self):
cached_file_folder = ""
cached_file_content =""
cached_file_header=""
print("\n\n\nGot a GET request for %s " % self.path)
self.print_headers()
self.check_credentials()
# Verify if it's a special case
is_special = self._execute_content_generated_cases("GET")
if is_special:
cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
else:
cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
_file_available = os.path.exists(cached_file_content)
if not _file_available:
print("Request for data currently not present in cache: %s" % (cached_file_folder,))
if not HOST:
self.send_response(404)
self.end_headers()
self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!')
print("HOST value is: %s " % (options.proxy))
return "404 Not found, no remote HOST specified on the emulator !!!"
url = '%s%s' % (HOST, self.path)
response = requests.get(url, auth=AUTH, headers=HEADERS, stream=True)
if response.status_code == 200:
self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response)
else:
print('Error when requesting file :')
print('Requested url : %s' % (url,))
print('Status code : %s' % (response.status_code,))
print('Content : %s' % (response.content,))
self.send_response(response.status_code)
self.end_headers()
self.wfile.write('404 Not found, nothing found on the remote server !!!')
return response.content
else:
print("Request for data currently present in cache: %s" % (cached_file_folder,))
print("HOST value is: %s " % (HOST))
self._send_content(cached_file_header, cached_file_content)
if self.path.startswith("/dcae-service-types?asdcResourceId="):
print("DCAE case deleting folder created " + cached_file_folder)
shutil.rmtree(cached_file_folder, ignore_errors=False, onerror=None)
elif self.path.startswith("/dcae-service-types"):
print("DCAE case deleting folder created " + cached_file_folder)
shutil.rmtree(cached_file_folder, ignore_errors=False, onerror=None)
else:
print("NOT in DCAE case deleting folder created " + cached_file_folder)
def do_POST(self):
cached_file_folder = ""
cached_file_content =""
cached_file_header=""
print("\n\n\nGot a POST for %s" % self.path)
self.check_credentials()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
print("data-string:\n %s" % self.data_string)
print("self.headers:\n %s" % self.headers)
is_special = self._execute_content_generated_cases("POST")
if is_special:
cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
else:
cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
_file_available = os.path.exists(cached_file_content)
if not _file_available:
if not HOST:
self.send_response(404)
self.end_headers()
self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!')
return "404 Not found, no remote HOST specified on the emulator !!!"
print("Request for data currently not present in cache: %s" % (cached_file_folder,))
url = '%s%s' % (HOST, self.path)
print("url: %s" % (url,))
response = requests.post(url, data=self.data_string, headers=self.headers, stream=True)
if response.status_code == 200:
self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response)
else:
print('Error when requesting file :')
print('Requested url : %s' % (url,))
print('Status code : %s' % (response.status_code,))
print('Content : %s' % (response.content,))
self.send_response(response.status_code)
self.end_headers()
self.wfile.write('404 Not found, nothing found on the remote server !!!')
return response.content
else:
print("Request for data present in cache: %s" % (cached_file_folder,))
self._send_content(cached_file_header, cached_file_content)
def do_PUT(self):
cached_file_folder = ""
cached_file_content =""
cached_file_header=""
print("\n\n\nGot a PUT for %s " % self.path)
self.check_credentials()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
print("data-string:\n %s" % self.data_string)
print("self.headers:\n %s" % self.headers)
is_special = self._execute_content_generated_cases("PUT")
if is_special:
cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
else:
cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
_file_available = os.path.exists(cached_file_content)
if not _file_available:
if not HOST:
self.send_response(404)
self.end_headers()
self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!')
return "404 Not found, no remote HOST specified on the emulator !!!"
print("Request for data currently not present in cache: %s" % (cached_file_folder,))
url = '%s%s' % (HOST, self.path)
print("url: %s" % (url,))
response = requests.put(url, data=self.data_string, headers=self.headers, stream=True)
if response.status_code == 200:
self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response)
else:
print('Error when requesting file :')
print('Requested url : %s' % (url,))
print('Status code : %s' % (response.status_code,))
print('Content : %s' % (response.content,))
self.send_response(response.status_code)
self.end_headers()
self.wfile.write('404 Not found, nothing found on the remote server !!!')
return response.content
else:
print("Request for data present in cache: %s" % (cached_file_folder,))
self._send_content(cached_file_header, cached_file_content)
def do_DELETE(self):
cached_file_folder = ""
cached_file_content =""
cached_file_header=""
print("\n\n\nGot a DELETE for %s " % self.path)
self.check_credentials()
if self.headers.get('Content-Length') is not None:
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
else:
self.data_string = "empty generated"
print("self.headers:\n %s" % self.headers)
is_special = self._execute_content_generated_cases("DELETE")
if is_special:
cached_file_folder = self._get_cached_file_folder_name(TMP_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
else:
cached_file_folder = self._get_cached_file_folder_name(CACHE_ROOT)
cached_file_content = self._get_cached_content_file_name(cached_file_folder)
cached_file_header = self._get_cached_header_file_name(cached_file_folder)
_file_available = os.path.exists(cached_file_content)
if not _file_available:
if not HOST:
self.send_response(404)
self.end_headers()
self.wfile.write('404 Not found, no remote HOST specified on the emulator !!!')
return "404 Not found, no remote HOST specified on the emulator !!!"
print("Request for data currently not present in cache: %s" % (cached_file_folder,))
url = '%s%s' % (HOST, self.path)
print("url: %s" % (url,))
response = requests.put(url, data=self.data_string, headers=self.headers, stream=True)
if response.status_code == 200:
self._write_cache(cached_file_folder, cached_file_header, cached_file_content, response)
else:
print('Error when requesting file :')
print('Requested url : %s' % (url,))
print('Status code : %s' % (response.status_code,))
print('Content : %s' % (response.content,))
self.send_response(response.status_code)
self.end_headers()
self.wfile.write('404 Not found, nothing found on the remote server !!!')
return response.content
else:
print("Request for data present in cache: %s" % (cached_file_folder,))
self._send_content(cached_file_header, cached_file_content)
# Main code that start the HTTP server
httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
httpd.allow_reuse_address = True
print("Listening on port "+ str(PORT) + "(Press Ctrl+C/Ctrl+Z to stop HTTPD Caching script)")
print("Caching folder " + CACHE_ROOT + ", Tmp folder for generated files " + TMP_ROOT)
signal.signal(signal.SIGINT, signal_handler)
httpd.serve_forever()
|