X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2Freq%2FTcaRequestFormatter.java;h=3646e5744333bbc4b615a54468ea69b2e190675c;hb=79f28833eae2bca5a8f6486391b2a3dc9cc179b2;hp=3fbf8739792f2a6db9911a531d0a461dcadd618b;hpb=ab5106ac26f2cbe846c3b1cf91f91f13da8b15fe;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java b/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java index 3fbf8739..3646e574 100644 --- a/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java +++ b/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java @@ -25,6 +25,7 @@ package org.onap.clamp.clds.client.req; 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.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; @@ -50,7 +51,6 @@ public class TcaRequestFormatter { * Hide the default constructor. */ private TcaRequestFormatter() { - } /** @@ -66,20 +66,14 @@ public class TcaRequestFormatter { public static String createPolicyJson(RefProp refProp, ModelProperties modelProperties) { try { String service = modelProperties.getGlobal().getService(); - Tca tca = modelProperties.getType(Tca.class); modelProperties.setCurrentModelElementId(tca.getId()); - ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", service); - ((ObjectNode) rootNode.get("cdap-tca-hi-lo_policy").get("metricsPerEventName").get(0)).put("policyName", - modelProperties.getCurrentPolicyScopeAndPolicyName()); - ((ObjectNode) rootNode.get("cdap-tca-hi-lo_policy").get("metricsPerEventName").get(0)).put("eventName", - tca.getTcaItem().getEventName()); - - ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("cdap-tca-hi-lo_policy").get("metricsPerEventName") - .get(0)); - - addThresholds(refProp, service, thresholdsParent, tca.getTcaItem(), modelProperties); - + ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.policy.template", service); + String policyName = modelProperties.getCurrentPolicyScopeAndPolicyName(); + rootNode.put("policyName", policyName); + rootNode.put("description", "MicroService vCPE Policy"); + ((ObjectNode) rootNode.get("content")).replace("tca_policy", + createPolicyContent(refProp, modelProperties, service, policyName, tca)); String tcaPolicyReq = rootNode.toString(); logger.info("tcaPolicyReq=" + tcaPolicyReq); return tcaPolicyReq; @@ -88,6 +82,44 @@ public class TcaRequestFormatter { } } + /** + * Format Tca Policy Content JSON + * + * @param refProp + * The refProp generally created by Spring, it's an access on the + * clds-references.properties file + * @param modelProperties + * The Model Prop created from BPMN JSON and BPMN properties JSON + * @return The Json string containing that should be sent to policy + */ + public static JsonNode createPolicyContent(RefProp refProp, ModelProperties modelProperties, String service, + String policyName, Tca tca) { + try { + if (null == service) { + service = modelProperties.getGlobal().getService(); + } + if (null == tca) { + tca = modelProperties.getType(Tca.class); + modelProperties.setCurrentModelElementId(tca.getId()); + } + if (null == policyName) { + policyName = modelProperties.getCurrentPolicyScopeAndPolicyName(); + } + ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", service); + ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("eventName", tca.getTcaItem().getEventName()); + ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("policyName", policyName); + ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("controlLoopSchemaType", + tca.getTcaItem().getControlLoopSchemaType()); + ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("metricsPerEventName").get(0)); + addThresholds(refProp, service, thresholdsParent, tca.getTcaItem(), modelProperties); + logger.info("tcaPolicyContent=" + rootNode.toString()); + return rootNode; + } catch (Exception e) { + throw new TcaRequestFormatterException("Exception caught when attempting to create the policy content JSON", + e); + } + } + /** * Add threshold values to the existing policy JSON. * @@ -111,9 +143,7 @@ public class TcaRequestFormatter { try { ArrayNode tcaNodes = appendToNode.withArray("thresholds"); ObjectNode tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.thresholds.template", service); - for (TcaThreshold tcaThreshold : tcaItem.getTcaThresholds()) { - tcaNode.put("controlLoopSchema", tcaThreshold.getControlLoopSchema()); tcaNode.put("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId()); tcaNode.put("fieldPath", tcaThreshold.getFieldPath()); tcaNode.put("thresholdValue", tcaThreshold.getThreshold()); @@ -142,22 +172,30 @@ public class TcaRequestFormatter { public static String updatedBlueprintWithConfiguration(RefProp refProp, ModelProperties modelProperties, String yamlValue) { try { - String jsonPolicy = createPolicyJson(refProp, modelProperties); - + String jsonPolicy = ((ObjectNode) createPolicyContent(refProp, modelProperties, null, null, null)) + .toString(); logger.info("Yaml that will be updated:" + yamlValue); Yaml yaml = new Yaml(); - Map loadedYaml = (Map) yaml.load(yamlValue); - Map nodeTemplates = (Map) loadedYaml.get("node_templates"); + // add policy_0 section in blueprint + /* + * Map policyObject = new HashMap + * (); Map policyIdObject = new HashMap (); String policyPrefix = + * refProp.getStringValue("tca.policyid.prefix"); + * policyIdObject.put("policy_id", policyPrefix + + * modelProperties.getCurrentPolicyScopeAndPolicyName()); + * policyObject.put("type", "dcae.nodes.policy"); + * policyObject.put("properties", policyIdObject); + * nodeTemplates.put("policy_0", policyObject); + */ Map tcaObject = (Map) nodeTemplates.get("tca_tca"); Map propsObject = (Map) tcaObject.get("properties"); Map appPreferences = (Map) propsObject.get("app_preferences"); appPreferences.put("tca_policy", jsonPolicy); - String blueprint = yaml.dump(loadedYaml); logger.info("Yaml updated:" + blueprint); - return blueprint; } catch (Exception e) { throw new TcaRequestFormatterException("Exception caught when attempting to update the blueprint", e);