Added tests for dcae dispacher services
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.java
index 1e5deda..b63bb64 100644 (file)
@@ -17,6 +17,7 @@
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  * ============LICENSE_END============================================\r
+ * Modifications copyright (c) 2018 Nokia\r
  * ===================================================================\r
  * \r
  */\r
@@ -60,15 +61,22 @@ public class DcaeInventoryServices {
     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);\r
     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();\r
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
-    public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
-    public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
-    public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
+    private static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
+    private static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
+    private static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
     public static final String DCAE_TYPE_NAME = "typeName";\r
     public static final String DCAE_TYPE_ID = "typeId";\r
+    private final ClampProperties refProp;\r
+    private final CldsDao cldsDao;\r
+    private final DcaeHttpConnectionManager dcaeHttpConnectionManager;\r
+\r
     @Autowired\r
-    private ClampProperties refProp;\r
-    @Autowired\r
-    private CldsDao cldsDao;\r
+    public DcaeInventoryServices(ClampProperties refProp, CldsDao cldsDao, DcaeHttpConnectionManager dcaeHttpConnectionManager) {\r
+        this.refProp = refProp;\r
+        this.cldsDao = cldsDao;\r
+        this.dcaeHttpConnectionManager = dcaeHttpConnectionManager;\r
+    }\r
+\r
 \r
     /**\r
      * Set the event inventory.\r
@@ -126,15 +134,13 @@ public class DcaeInventoryServices {
         if (dcaeResponse != null) {\r
             logger.info("Dcae Response for query on inventory: " + dcaeResponse);\r
             String oldTypeId = cldsModel.getTypeId();\r
-            String newTypeId = "";\r
             if (dcaeResponse.getTypeId() != null) {\r
-                newTypeId = dcaeResponse.getTypeId();\r
                 cldsModel.setTypeId(dcaeResponse.getTypeId());\r
             }\r
             if (dcaeResponse.getTypeName() != null) {\r
                 cldsModel.setTypeName(dcaeResponse.getTypeName());\r
             }\r
-            if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)\r
+            if (oldTypeId == null || !cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_DISTRIBUTE)\r
                     || cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) {\r
                 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),\r
                         CldsEvent.ACTION_STATE_RECEIVED, null);\r
@@ -205,7 +211,7 @@ public class DcaeInventoryServices {
         }\r
         for (int i = 0; i < retryLimit; i++) {\r
             metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");\r
-            String response = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
+            String response = dcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
             int totalCount = getTotalCountFromDcaeInventoryResponse(response);\r
             metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount);\r
             if (totalCount > 0) {\r
@@ -256,7 +262,7 @@ public class DcaeInventoryServices {
             String apiBodyString = dcaeServiceTypeRequest.toString();\r
             logger.info("Dcae api Body String - " + apiBodyString);\r
             String url = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types";\r
-            String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString,\r
+            String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString,\r
                     "application/json");\r
             // If the DCAEServiceTypeRequest is created successfully it will\r
             // return a json object responce containing a node for newly created\r
@@ -288,4 +294,40 @@ public class DcaeInventoryServices {
         }\r
         return typeId;\r
     }\r
+\r
+    /**\r
+     * Method to delete blueprint from dcae inventory if it's exists.\r
+     * \r
+     * @param typeName\r
+     * @param serviceUuid\r
+     * @param resourceUuid\r
+     * @throws InterruptedException\r
+     */\r
+    public void deleteDCAEServiceType(String typeName, String serviceUuid, String resourceUuid)\r
+            throws InterruptedException {\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "deleteDCAEServiceType");\r
+        boolean result = false;\r
+        try {\r
+            DcaeInventoryResponse inventoryResponse = getDcaeInformation(typeName, serviceUuid, resourceUuid);\r
+            if (inventoryResponse != null && inventoryResponse.getTypeId() != null) {\r
+                String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types/"\r
+                        + inventoryResponse.getTypeId();\r
+                dcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "DELETE", null, null);\r
+            }\r
+            result = true;\r
+        } catch (IOException | ParseException e) {\r
+            logger.error("Exception occurred during deleteDCAEServiceType Operation with DCAE", e);\r
+            throw new BadRequestException("Exception occurred during deleteDCAEServiceType Operation with DCAE", e);\r
+        } finally {\r
+            if (result) {\r
+                LoggingUtils.setResponseContext("0", "Delete DCAE ServiceType success", this.getClass().getName());\r
+            } else {\r
+                LoggingUtils.setResponseContext("900", "Delete DCAE ServiceType failed", this.getClass().getName());\r
+                LoggingUtils.setErrorContext("900", "Delete DCAE ServiceType error");\r
+            }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("deleteDCAEServiceType completed");\r
+        }\r
+    }\r
 }\r