Fix frontend docker
[clamp.git] / src / test / resources / http-cache / third_party_proxy.py
index 0db977b..5ec9f46 100755 (executable)
@@ -43,7 +43,7 @@ parser.add_argument("--temp",     "-t", default=tempfile.mkdtemp(), type=str, he
 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")
+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()
 
 
@@ -127,10 +127,10 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
             with open(cached_file_content, 'w') as f:
                 f.write(jsonGenerated)
         return True
-     elif self.path.startswith("/dcae-operationstatus") and http_type == "GET":
+     elif self.path.startswith("/dcae-operationstatus/install") and http_type == "GET":
         if not _file_available:
-            print "self.path start with /dcae-operationstatus, generating response json..."
-            jsonGenerated =  "{\"operationType\": \"operationType1\", \"status\": \"succeeded\"}"
+            print "self.path start with /dcae-operationstatus/install, generating response json..."
+            jsonGenerated =  "{\"operationType\": \"install\", \"status\": \"succeeded\"}"
             print "jsonGenerated: " + jsonGenerated
     
             try:
@@ -145,24 +145,29 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
             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":
+     elif self.path.startswith("/dcae-operationstatus/uninstall") and http_type == "GET":
         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 "self.path start with /dcae-operationstatus/uninstall, generating response json..."
+            jsonGenerated =  "{\"operationType\": \"uninstall\", \"status\": \"succeeded\"}"
             print "jsonGenerated: " + jsonGenerated
     
-            os.makedirs(cached_file_folder, 0777)
+            try:
+                os.makedirs(cached_file_folder, 0777)
+            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-deployments/") and (http_type == "PUT" or http_type == "DELETE"):
+        return True
+     elif self.path.startswith("/sdc/v1/catalog/services/") and http_type == "POST":
         if not _file_available:
-            print "self.path start with /dcae-deployments/, generating response json..."
-            #jsondata = json.loads(self.data_string)
-            jsonGenerated = "{\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus\",\"test2\":\"test2\"}}"
+            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, 0777)
@@ -171,6 +176,30 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
             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\"}}"
+            print "jsonGenerated: " + jsonGenerated
+            if not os.path.exists(cached_file_folder):
+                os.makedirs(cached_file_folder, 0777)
+            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\"}}"
+            print "jsonGenerated: " + jsonGenerated
+            if not os.path.exists(cached_file_folder):
+                os.makedirs(cached_file_folder, 0777)
+            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):
@@ -180,7 +209,7 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
         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":
+     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, 0777)
@@ -189,8 +218,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
         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/ ..."
+     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, 0777)
     
@@ -199,6 +228,15 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
         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, 0777)
+        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/api/v1/policytypes/") 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\"}"
@@ -241,7 +279,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
 
             if not HOST:
                 self.send_response(404)
-                return "404 Not found"
+                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 !!!"
 
             url = '%s%s' % (HOST, self.path)
             response = requests.get(url, auth=AUTH, headers=HEADERS, stream=True)
@@ -254,6 +294,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
                 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,))
@@ -292,7 +334,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
         
             if not HOST:
                 self.send_response(404)
-                return "404 Not found"
+                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,))
 
@@ -308,6 +352,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
                 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,))
@@ -339,7 +385,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
         if not _file_available:
             if not HOST:
                 self.send_response(404)
-                return "404 Not found"
+                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,))
 
@@ -355,6 +403,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
                 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,))
@@ -389,7 +439,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
         if not _file_available:
             if not HOST:
                 self.send_response(404)
-                return "404 Not found"
+                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,))
 
@@ -405,6 +457,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
                 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,))