* 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
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;
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;
/**
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);
* 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;
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) {
* 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) {
/**
* 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
*/
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);