Add Direct call DCAE. 81/30481/1
authorDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Tue, 6 Feb 2018 15:19:19 +0000 (16:19 +0100)
committerDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Tue, 6 Feb 2018 15:19:19 +0000 (16:19 +0100)
Rework the logs to add info, and add a method for DCAE direct call

Issue-ID: CLAMP-85,CLAMP-90
Change-Id: Id75c89e1788a0ed829582a67013570003f5bf204
Signed-off-by: Determe, Sebastien (sd378r) <sd378r@intl.att.com>
src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java

index 53d17aa..94ebaca 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================\r
  * ONAP CLAMP\r
  * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights\r
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights\r
  *                             reserved.\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
@@ -26,12 +26,16 @@ package org.onap.clamp.clds.client;
 import com.att.eelf.configuration.EELFLogger;\r
 import com.att.eelf.configuration.EELFManager;\r
 import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.node.ObjectNode;\r
 \r
 import java.io.IOException;\r
 import java.security.GeneralSecurityException;\r
 import java.util.Date;\r
 import java.util.List;\r
 \r
+import javax.ws.rs.BadRequestException;\r
+\r
 import org.apache.commons.codec.DecoderException;\r
 import org.json.simple.JSONArray;\r
 import org.json.simple.JSONObject;\r
@@ -48,23 +52,20 @@ import org.onap.clamp.clds.util.LoggingUtils;
 import org.springframework.beans.factory.annotation.Autowired;\r
 \r
 /**\r
- * \r
  * This class implements the communication with DCAE for the service inventory.\r
- *\r
- * \r
- * \r
  */\r
 public class DcaeInventoryServices {\r
-    protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);\r
-    protected static final EELFLogger auditLogger   = EELFManager.getInstance().getAuditLogger();\r
+\r
+    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
+    private static final String DCAE_INVENTORY_URL = "DCAE_INVENTORY_URL";\r
     @Autowired\r
-    private RefProp                   refProp;\r
+    private RefProp refProp;\r
     @Autowired\r
-    private CldsDao                   cldsDao;\r
+    private CldsDao cldsDao;\r
 \r
     /**\r
-     * \r
      * Set the event inventory.\r
      * \r
      * @param cldsModel\r
@@ -89,13 +90,7 @@ public class DcaeInventoryServices {
             artifactName = artifactName + ".yml";\r
         }\r
         try {\r
-            /*\r
-             * \r
-             * Below are the properties required for calling the dcae inventory\r
-             * \r
-             * url call\r
-             * \r
-             */\r
+            // Below are the properties required for calling the dcae inventory\r
             ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false,\r
                     "{}", cldsModel.getPropText());\r
             Global global = prop.getGlobal();\r
@@ -105,14 +100,19 @@ public class DcaeInventoryServices {
             if (resourceUuidList != null && !resourceUuidList.isEmpty()) {\r
                 resourceUuid = resourceUuidList.get(0);\r
             }\r
-            /* Invemtory service url is called in this method */\r
+            /* Inventory service url is called in this method */\r
             isDcaeInfoAvailable = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid);\r
             /* set dcae events */\r
             dcaeEvent.setArtifactName(artifactName);\r
             dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);\r
+            LoggingUtils.setResponseContext("0", "Set inventory success", this.getClass().getName());\r
         } catch (JsonProcessingException e) {\r
+               LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName());\r
+               LoggingUtils.setErrorContext("900", "Set inventory error");\r
             logger.error("Error during JSON decoding", e);\r
         } catch (IOException ex) {\r
+               LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName());\r
+               LoggingUtils.setErrorContext("900", "Set inventory error");\r
             logger.error("Error during DCAE communication", ex);\r
         } finally {\r
             LoggingUtils.setTimeContext(startTime, new Date());\r
@@ -135,7 +135,8 @@ public class DcaeInventoryServices {
             if (jsonObj.get("typeName") != null) {\r
                 cldsModel.setTypeName(jsonObj.get("typeName").toString());\r
             }\r
-            if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)) {\r
+            if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)\r
+                    || cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) {\r
                 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),\r
                         CldsEvent.ACTION_STATE_RECEIVED, null);\r
             }\r
@@ -159,7 +160,6 @@ public class DcaeInventoryServices {
      *             In case of issues with the stream\r
      * @throws ParseException\r
      *             In case of issues with the Json parsing\r
-     * \r
      */\r
     public String getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
             throws IOException, ParseException {\r
@@ -167,7 +167,7 @@ public class DcaeInventoryServices {
         LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
         String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="\r
                 + artifactName;\r
-        String fullUrl = refProp.getStringValue("DCAE_INVENTORY_URL") + "/dcae-service-types" + queryString;\r
+        String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString;\r
         logger.info("Dcae Inventory Service full url - " + fullUrl);\r
         String daceInventoryResponse = null;\r
         String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);\r
@@ -184,8 +184,76 @@ public class DcaeInventoryServices {
             daceInventoryResponse = dcaeServiceType0.toString();\r
             logger.info(daceInventoryResponse);\r
         }\r
+        LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());\r
         LoggingUtils.setTimeContext(startTime, new Date());\r
         metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);\r
         return daceInventoryResponse;\r
     }\r
+\r
+    /**\r
+     * Inserts a new DCAEServiceType or updates an existing instance. If the\r
+     * typeName is same second time(already exists) then the\r
+     * DCAEServiceTypeRequest is updated\r
+     * \r
+     * @param blueprintTemplate\r
+     *            blueprint content\r
+     * @param owner\r
+     *            owner of the data\r
+     * @param typeName\r
+     *            The type/artifact Name\r
+     * @param typeVersion\r
+     *            type version\r
+     * @param asdcServiceId\r
+     *            The service UUID\r
+     * @param asdcResourceId\r
+     *            The vf UUID\r
+     * @return The DCAE inventory type id\r
+     */\r
+    public String createUpdateDCAEServiceType(String blueprintTemplate, String owner, String typeName, int typeVersion,\r
+            String asdcServiceId, String asdcResourceId) {\r
+        Date startTime = new Date();\r
+        LoggingUtils.setTargetContext("DCAE", "createDCAEServiceType");\r
+        String typeId = null;\r
+        try {\r
+            ObjectMapper mapper = new ObjectMapper();\r
+            ObjectNode dcaeServiceTypeRequest = mapper.createObjectNode();\r
+            dcaeServiceTypeRequest.put("blueprintTemplate", blueprintTemplate);\r
+            dcaeServiceTypeRequest.put("owner", owner);\r
+            dcaeServiceTypeRequest.put("typeName", typeName);\r
+            dcaeServiceTypeRequest.put("typeVersion", typeVersion);\r
+            dcaeServiceTypeRequest.put("asdcServiceId", asdcServiceId);\r
+            dcaeServiceTypeRequest.put("asdcResourceId", asdcResourceId);\r
+            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
+                    "application/json");\r
+            // If the DCAEServiceTypeRequest is created successfully it will\r
+            // return a json object responce containing a node for newly created\r
+            // "typeId"\r
+            // The newly generated DCAEServiceTypeRequest can then be accessed\r
+            // via URL: https://<DCAE_INVENTORY_URL>/dcae-service-types/<typeId>\r
+            JSONParser parser = new JSONParser();\r
+            Object obj0 = parser.parse(responseStr);\r
+            JSONObject jsonObj = (JSONObject) obj0;\r
+            typeId = (String) jsonObj.get("typeId"); // need to save this\r
+                                                            // as\r
+                                                            // service_type_id\r
+                                                            // in model table\r
+        } catch (IOException | ParseException e) {\r
+            logger.error("Exception occurred during createupdateDCAEServiceType Operation with DCAE", e);\r
+            throw new BadRequestException("Exception occurred during createupdateDCAEServiceType Operation with DCAE",\r
+                    e);\r
+        } finally {\r
+               if(typeId != null) {\r
+                       LoggingUtils.setResponseContext("0", "Create update DCAE ServiceType success", this.getClass().getName());\r
+               } else {\r
+                       LoggingUtils.setResponseContext("900", "Create update DCAE ServiceType failed", this.getClass().getName());\r
+                LoggingUtils.setErrorContext("900", "Create update DCAE ServiceType error");\r
+               }\r
+            LoggingUtils.setTimeContext(startTime, new Date());\r
+            metricsLogger.info("createupdateDCAEServiceType complete");\r
+        }\r
+        return typeId;\r
+    }\r
 }\r