1710 Rebase - Second Attempt
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / AaiUtil.groovy
index af650b4..82d58cf 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * \r
+ *\r
  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
+ *\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
@@ -33,7 +33,7 @@ class AaiUtil {
        public static final String DEFAULT_VERSION_KEY = 'URN_mso_workflow_global_default_aai_version'\r
 \r
        private String aaiNamespace = null;\r
-        \r
+\r
        private AbstractServiceTaskProcessor taskProcessor\r
 \r
        public AaiUtil(AbstractServiceTaskProcessor taskProcessor) {\r
@@ -186,7 +186,7 @@ class AaiUtil {
 \r
                //set namespace\r
                setNamespace(execution)\r
-               \r
+\r
                // Check for flow+resource specific first\r
                def key = "URN_mso_workflow_${processKey}_aai_${resourceName}_uri"\r
                def uri = execution.getVariable(key)\r
@@ -215,7 +215,7 @@ class AaiUtil {
                        (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, 'Internal Error: AAI URI entry for ' + key + ' not defined in the MSO URN properties file')\r
                }\r
        }\r
-       \r
+\r
        /**\r
         * This method can be used for getting the building namespace out of uri.\r
         *  NOTE: A getUri() method needs to be invoked first.\r
@@ -226,7 +226,7 @@ class AaiUtil {
         *\r
         * @return namespace\r
         */\r
-       \r
+\r
        public String getNamespaceFromUri(String uri) {\r
                 if (aaiNamespace == null) {\r
                        throw new Exception('Internal Error: AAI Namespace has not been set yet. A getUri() method needs to be invoked first.')\r
@@ -260,7 +260,7 @@ class AaiUtil {
                   return namespace\r
           }\r
    }\r
-       \r
+\r
        /**\r
         * This is used to extract the version from uri.\r
         *\r
@@ -280,7 +280,7 @@ class AaiUtil {
                }\r
                return savedVersion\r
        }\r
-       \r
+\r
 \r
        /**\r
         * This reusable method can be used for making AAI Get Calls. The url should\r
@@ -291,32 +291,33 @@ class AaiUtil {
         * @param url\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIGetCall(Execution execution, String url){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug(" ======== STARTED Execute AAI Get Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        String uuid = utils.getRequestID()\r
                        taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
                        taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
-                       \r
+\r
                        String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
 \r
                        RESTConfig config = new RESTConfig(url);\r
                        RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
-                       \r
+\r
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       \r
-                       APIResponse apiResponse = client.get()\r
-                       return apiResponse\r
+                       apiResponse = client.get()\r
 \r
+                       taskProcessor.logDebug( "======== COMPLETED Execute AAI Get Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.logDebug("Exception occured while executing AAI Get Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== COMPLETED Execute AAI Get Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
 \r
@@ -330,15 +331,17 @@ class AaiUtil {
         * @param payload\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIPutCall(Execution execution, String url, String payload){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug( " ======== Started Execute AAI Put Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        String uuid = utils.getRequestID()\r
                        taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
                        taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
-                       \r
+\r
                        String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
 \r
                        RESTConfig config = new RESTConfig(url);\r
@@ -346,14 +349,14 @@ class AaiUtil {
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       APIResponse apiResponse = client.httpPut(payload)\r
+                       apiResponse = client.httpPut(payload)\r
 \r
-                       return apiResponse\r
+                       taskProcessor.logDebug( "======== Completed Execute AAI Put Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Put Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== Completed Execute AAI Put Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
        /**\r
@@ -366,10 +369,12 @@ class AaiUtil {
         * @param payload\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIPatchCall(Execution execution, String url, String payload){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug( " ======== Started Execute AAI Patch Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        String uuid = utils.getRequestID()\r
                        taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
@@ -377,20 +382,20 @@ class AaiUtil {
                        taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
 \r
                        String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
-                       \r
+\r
                        RESTConfig config = new RESTConfig(url);\r
                        RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Content-Type", "application/merge-patch+json").addHeader("Accept","application/json");\r
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       APIResponse apiResponse = client.httpPatch(payload)\r
+                       apiResponse = client.httpPatch(payload)\r
 \r
-                       return apiResponse\r
+                       taskProcessor.logDebug( "======== Completed Execute AAI Patch Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Patch Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== Completed Execute AAI Patch Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
 \r
@@ -403,10 +408,12 @@ class AaiUtil {
         * @param url\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIDeleteCall(Execution execution, String url){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        String uuid = utils.getRequestID()\r
                        taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
@@ -419,15 +426,14 @@ class AaiUtil {
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       APIResponse apiResponse = client.delete()\r
-\r
-                       return apiResponse\r
+                       apiResponse = client.delete()\r
 \r
+                       taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
        /**\r
@@ -440,10 +446,12 @@ class AaiUtil {
         * @param payload\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIDeleteCall(Execution execution, String url, String payload, String authHeader){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug( " ======== Started Execute AAI Delete Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        String uuid = utils.getRequestID()\r
                        taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
@@ -456,16 +464,14 @@ class AaiUtil {
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       \r
-                       APIResponse apiResponse = client.httpDelete(payload)\r
-\r
-                       return apiResponse\r
+                       apiResponse = client.httpDelete(payload)\r
 \r
+                       taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Delete Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== Completed Execute AAI Delete Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
        /**\r
@@ -478,10 +484,12 @@ class AaiUtil {
         * @param payload\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIPostCall(Execution execution, String url, String payload){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug( " ======== Started Execute AAI Post Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        String uuid = utils.getRequestID()\r
                        taskProcessor.logDebug( "Generated uuid is: " + uuid, isDebugEnabled)\r
@@ -490,19 +498,18 @@ class AaiUtil {
                        String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
                        RESTConfig config = new RESTConfig(url);\r
                        RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/xml");\r
-                       \r
+\r
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       APIResponse apiResponse = client.httpPost(payload)\r
-\r
-                       return apiResponse\r
+                       apiResponse = client.httpPost(payload)\r
 \r
+                       taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
        /**\r
@@ -518,35 +525,33 @@ class AaiUtil {
         * @param headerValue - the header's value, i.e. addHeader(headerName, headerValue)\r
         *\r
         * @return APIResponse\r
+        *\r
         */\r
        public APIResponse executeAAIPostCall(Execution execution, String url, String payload, String authenticationHeaderValue, String headerName, String headerValue){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
                taskProcessor.logDebug( " ======== Started Execute AAI Post Process ======== ", isDebugEnabled)\r
+               APIResponse apiResponse = null\r
                try{\r
                        taskProcessor.logDebug( "URL to be used is: " + url, isDebugEnabled)\r
 \r
                        String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))\r
-                       \r
+\r
                        RESTConfig config = new RESTConfig(url);\r
                        RESTClient client = new RESTClient(config).addAuthorizationHeader(authenticationHeaderValue).addHeader(headerName, headerValue)\r
                        if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
                                client.addAuthorizationHeader(basicAuthCred)\r
                        }\r
-                       APIResponse apiResponse = client.httpPost(payload)\r
-\r
-                       return apiResponse\r
+                       apiResponse = client.httpPost(payload)\r
 \r
+                       taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
                }catch(Exception e){\r
                        taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)\r
-                       return e\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
-               taskProcessor.logDebug( "======== Completed Execute AAI Post Process ======== ", isDebugEnabled)\r
+               return apiResponse\r
        }\r
 \r
 \r
-       \r
-\r
-\r
        /* Utility to get the Cloud Region from AAI\r
         * Returns String cloud region id, (ie, cloud-region-id)\r
         * @param execution\r
@@ -554,60 +559,50 @@ class AaiUtil {
         * @param backend - "PO" - real region, or "SDNC" - v2.5 (fake region).\r
         */\r
 \r
-       //TODO: We should refactor this method to return WorkflowException instead of Error. Also to throw MSOWorkflowException which the calling flow will then catch.\r
-\r
        public String getAAICloudReqion(Execution execution, String url, String backend, inputCloudRegion){\r
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
-\r
-               try {\r
-                 APIResponse apiResponse = executeAAIGetCall(execution, url)\r
-                 String returnCode = apiResponse.getStatusCode()\r
-                 String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
-                 taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Return code: " + returnCode, isDebugEnabled)\r
-                 execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionReturnCode", returnCode)\r
-                 //taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Response: " + aaiResponseAsString, isDebugEnabled)\r
-                 //execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionResponse", aaiResponseAsString)\r
-                 String regionId = ""\r
-                 if (returnCode == "200") {\r
-                        taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region is Successful.", isDebugEnabled)\r
-                          try {\r
-                          String regionVersion = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-version")\r
-                          taskProcessor.utils.log("DEBUG", "Cloud Region Version from AAI for " + backend + " is: " + regionVersion, isDebugEnabled)\r
-                          if (backend == "PO") {\r
-                                 regionId = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-id")\r
-                          } else { // backend not "PO"\r
-                                 if (regionVersion == "2.5" ) {\r
-                                         regionId = "AAIAIC25"\r
-                                 } else {\r
-                                         regionId = taskProcessor.utils.getNodeText(aaiResponseAsString, "cloud-region-id")\r
-                                 }\r
-                          }\r
-\r
-                          taskProcessor.utils.log("DEBUG", "Cloud Region Id from AAI " + backend + " is: " + regionId, isDebugEnabled)\r
-                          return regionId\r
-\r
-                        } catch (Exception e) {\r
-                                 taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)\r
-                                 return "ERROR"\r
-                        }\r
-                 } else { // not 200\r
-                         if (returnCode == "404") {\r
-                                if (backend == "PO") {\r
-                                         regionId = inputCloudRegion\r
-                                } else  {  // backend not "PO"\r
-                                         regionId = "AAIAIC25"\r
-                                }\r
-                                taskProcessor.utils.log("DEBUG", "Cloud Region value for code='404' of " + backend + " is: " + regionId, isDebugEnabled)\r
-                                 return regionId\r
-                         } else {\r
-                                 taskProcessor.utils.log("ERROR", "Call AAI Cloud Region is NOT Successful.", isDebugEnabled)\r
-                                 return "ERROR"\r
-                         }\r
-                 }\r
+               String regionId = ""\r
+               try{\r
+                       APIResponse apiResponse = executeAAIGetCall(execution, url)\r
+                       String returnCode = apiResponse.getStatusCode()\r
+                       String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
+                       taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region Return code: " + returnCode, isDebugEnabled)\r
+                       execution.setVariable(execution.getVariable("prefix")+"queryCloudRegionReturnCode", returnCode)\r
+\r
+                       if(returnCode == "200"){\r
+                               taskProcessor.utils.log("DEBUG", "Call AAI Cloud Region is Successful.", isDebugEnabled)\r
+\r
+                               String regionVersion = taskProcessor.utils.getNodeText1(aaiResponseAsString, "cloud-region-version")\r
+                               taskProcessor.utils.log("DEBUG", "Cloud Region Version from AAI for " + backend + " is: " + regionVersion, isDebugEnabled)\r
+                               if (backend == "PO") {\r
+                                       regionId = taskProcessor.utils.getNodeText1(aaiResponseAsString, "cloud-region-id")\r
+                               } else { // backend not "PO"\r
+                                       if (regionVersion == "2.5" ) {\r
+                                               regionId = "AAIAIC25"\r
+                                       } else {\r
+                                               regionId = taskProcessor.utils.getNodeText1(aaiResponseAsString, "cloud-region-id")\r
+                                       }\r
+                               }\r
+                               if(regionId == null){\r
+                                       throw new BpmnError("MSOWorkflowException")\r
+                               }\r
+                               taskProcessor.utils.log("DEBUG", "Cloud Region Id from AAI " + backend + " is: " + regionId, isDebugEnabled)\r
+                       }else if (returnCode == "404"){ // not 200\r
+                               if (backend == "PO") {\r
+                                       regionId = inputCloudRegion\r
+                               }else{  // backend not "PO"\r
+                                       regionId = "AAIAIC25"\r
+                               }\r
+                               taskProcessor.utils.log("DEBUG", "Cloud Region value for code='404' of " + backend + " is: " + regionId, isDebugEnabled)\r
+                       }else{\r
+                               taskProcessor.utils.log("ERROR", "Call AAI Cloud Region is NOT Successful.", isDebugEnabled)\r
+                               throw new BpmnError("MSOWorkflowException")\r
+                       }\r
                }catch(Exception e) {\r
-                  taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)\r
-                  return "ERROR"\r
+                       taskProcessor.utils.log("ERROR", "Exception occured while getting the Cloud Reqion. Exception is: \n" + e, isDebugEnabled)\r
+                       throw new BpmnError("MSOWorkflowException")\r
                }\r
+               return regionId\r
        }\r
 \r
        /* returns xml Node with service-type of searchValue */\r