Fixed Sonar issues in the onap.clamp.clds.client packages
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / tca / TcaRequestFormatter.java
index 4a07ca3..ae0de07 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -25,10 +27,9 @@ package org.onap.clamp.clds.client.req.tca;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
 
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
 import java.io.IOException;
 import java.util.Map;
 
@@ -38,6 +39,7 @@ import org.onap.clamp.clds.model.properties.ModelProperties;
 import org.onap.clamp.clds.model.properties.Tca;
 import org.onap.clamp.clds.model.properties.TcaItem;
 import org.onap.clamp.clds.model.properties.TcaThreshold;
+import org.onap.clamp.clds.util.JsonUtils;
 import org.yaml.snakeyaml.Yaml;
 
 /**
@@ -69,10 +71,12 @@ public class TcaRequestFormatter {
             String service = modelProperties.getGlobal().getService();
             Tca tca = modelProperties.getType(Tca.class);
             modelProperties.setCurrentModelElementId(tca.getId());
-            ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.policy.template", service);
+            // Always one tcaItem so must be set to id 0
+            modelProperties.setPolicyUniqueId("0");
+            JsonObject rootNode = refProp.getJsonTemplate("tca.policy.template", service ).getAsJsonObject();
             String policyName = modelProperties.getCurrentPolicyScopeAndPolicyName();
-            rootNode.put("policyName", policyName);
-            ((ObjectNode) rootNode.get("content")).replace("tca_policy",
+            rootNode.addProperty("policyName", policyName);
+            rootNode.get("content").getAsJsonObject().add("tca_policy",
                     createPolicyContent(refProp, modelProperties, service, policyName, tca));
             String tcaPolicyReq = rootNode.toString();
             logger.info("tcaPolicyReq=" + tcaPolicyReq);
@@ -102,7 +106,7 @@ public class TcaRequestFormatter {
      *            modelProperties.setCurrentModelElementId will be used
      * @return The Json node containing what should be sent to policy
      */
-    public static JsonNode createPolicyContent(ClampProperties refProp, ModelProperties modelProperties, String service,
+    public static JsonObject createPolicyContent(ClampProperties refProp, ModelProperties modelProperties, String service,
             String policyName, Tca tca) {
         try {
             String serviceToUse = service;
@@ -118,14 +122,12 @@ public class TcaRequestFormatter {
             if (policyNameToUse == null) {
                 policyNameToUse = modelProperties.getCurrentPolicyScopeAndPolicyName();
             }
-            ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", serviceToUse);
-            ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("eventName",
-                    tcaToUse.getTcaItem().getEventName());
-            ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("policyName", policyNameToUse);
-            ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("controlLoopSchemaType",
-                    tcaToUse.getTcaItem().getControlLoopSchemaType());
-            ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("metricsPerEventName").get(0));
-            addThresholds(refProp, serviceToUse, thresholdsParent, tcaToUse.getTcaItem(), modelProperties);
+            JsonObject rootNode = refProp.getJsonTemplate("tca.template", serviceToUse).getAsJsonObject();
+            JsonObject metricsPerEventName = rootNode.get("metricsPerEventName").getAsJsonArray().get(0).getAsJsonObject();
+            metricsPerEventName.addProperty("eventName", tcaToUse.getTcaItem().getEventName());
+            metricsPerEventName.addProperty("policyName", policyNameToUse);
+            metricsPerEventName.addProperty("controlLoopSchemaType",tcaToUse.getTcaItem().getControlLoopSchemaType());
+            addThresholds(refProp, serviceToUse, metricsPerEventName, tcaToUse.getTcaItem(), modelProperties);
             logger.info("tcaPolicyContent=" + rootNode.toString());
             return rootNode;
         } catch (IOException e) {
@@ -152,18 +154,17 @@ public class TcaRequestFormatter {
      *            The Model Properties created from BPMN JSON and BPMN
      *            properties JSON
      */
-    private static void addThresholds(ClampProperties refProp, String service, ObjectNode appendToNode, TcaItem tcaItem,
+    private static void addThresholds(ClampProperties refProp, String service, JsonObject appendToNode, TcaItem tcaItem,
             ModelProperties modelProperties) {
-        ArrayNode tcaNodes = appendToNode.withArray("thresholds");
-        ObjectNode tcaNode;
+        JsonArray tcaNodes = appendToNode.get("thresholds").getAsJsonArray();
         try {
-            tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.thresholds.template", service);
             for (TcaThreshold tcaThreshold : tcaItem.getTcaThresholds()) {
-                tcaNode.put("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId());
-                tcaNode.put("fieldPath", tcaThreshold.getFieldPath());
-                tcaNode.put("thresholdValue", tcaThreshold.getThreshold());
-                tcaNode.put("direction", tcaThreshold.getOperator());
-                tcaNode.put("closedLoopEventStatus", tcaThreshold.getClosedLoopEventStatus());
+                JsonObject tcaNode = refProp.getJsonTemplate("tca.thresholds.template", service).getAsJsonObject();
+                tcaNode.addProperty("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId());
+                tcaNode.addProperty("fieldPath", tcaThreshold.getFieldPath());
+                tcaNode.addProperty("thresholdValue", tcaThreshold.getThreshold());
+                tcaNode.addProperty("direction", tcaThreshold.getOperator());
+                tcaNode.addProperty("closedLoopEventStatus", tcaThreshold.getClosedLoopEventStatus());
                 tcaNodes.add(tcaNode);
             }
         } catch (IOException e) {
@@ -174,7 +175,7 @@ public class TcaRequestFormatter {
     /**
      * This method updates the blueprint that is received in the UI with the TCA
      * Json.
-     * 
+     *
      * @param refProp
      *            * The refProp generally created by Spring, it's an access on
      *            the clds-references.properties file
@@ -186,14 +187,14 @@ public class TcaRequestFormatter {
      */
     public static String updatedBlueprintWithConfiguration(ClampProperties refProp, ModelProperties modelProperties,
             String yamlValue) {
-        String jsonPolicy = ((ObjectNode) createPolicyContent(refProp, modelProperties, null, null, null)).toString();
+        String jsonPolicy = JsonUtils.GSON.toJson(createPolicyContent(refProp, modelProperties, null, null, null));
         logger.info("Yaml that will be updated:" + yamlValue);
         Yaml yaml = new Yaml();
-        Map<String, Object> loadedYaml = (Map<String, Object>) yaml.load(yamlValue);
-        Map<String, Object> nodeTemplates = (Map<String, Object>) loadedYaml.get("node_templates");
-        Map<String, Object> tcaObject = (Map<String, Object>) nodeTemplates.get("tca_tca");
-        Map<String, Object> propsObject = (Map<String, Object>) tcaObject.get("properties");
-        Map<String, Object> appPreferences = (Map<String, Object>) propsObject.get("app_preferences");
+        Map<String, Map> loadedYaml = yaml.load(yamlValue);
+        Map<String, Map> nodeTemplates = loadedYaml.get("node_templates");
+        Map<String, Map> tcaObject = nodeTemplates.get("tca_tca");
+        Map<String, Map> propsObject = tcaObject.get("properties");
+        Map<String, String> appPreferences = propsObject.get("app_preferences");
         appPreferences.put("tca_policy", jsonPolicy);
         String blueprint = yaml.dump(loadedYaml);
         logger.info("Yaml updated:" + blueprint);