2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.client.req.tca;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.node.ArrayNode;
30 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import java.io.IOException;
35 import org.onap.clamp.clds.config.ClampProperties;
36 import org.onap.clamp.clds.exception.TcaRequestFormatterException;
37 import org.onap.clamp.clds.model.properties.ModelProperties;
38 import org.onap.clamp.clds.model.properties.Tca;
39 import org.onap.clamp.clds.model.properties.TcaItem;
40 import org.onap.clamp.clds.model.properties.TcaThreshold;
41 import org.yaml.snakeyaml.Yaml;
44 * Construct the requests for TCA policy and SDC.
46 public class TcaRequestFormatter {
48 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(TcaRequestFormatter.class);
49 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
52 * Hide the default constructor.
54 private TcaRequestFormatter() {
58 * Format Tca Policy JSON request.
61 * The refProp generally created by Spring, it's an access on the
62 * clds-references.properties file
63 * @param modelProperties
64 * The Model Prop created from BPMN JSON and BPMN properties JSON
65 * @return The Json string containing that should be sent to policy
67 public static String createPolicyJson(ClampProperties refProp, ModelProperties modelProperties) {
69 String service = modelProperties.getGlobal().getService();
70 Tca tca = modelProperties.getType(Tca.class);
71 modelProperties.setCurrentModelElementId(tca.getId());
72 // Always one tcaItem so must be set to id 0
73 modelProperties.setPolicyUniqueId("0");
74 ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.policy.template", service);
75 String policyName = modelProperties.getCurrentPolicyScopeAndPolicyName();
76 rootNode.put("policyName", policyName);
77 ((ObjectNode) rootNode.get("content")).replace("tca_policy",
78 createPolicyContent(refProp, modelProperties, service, policyName, tca));
79 String tcaPolicyReq = rootNode.toString();
80 logger.info("tcaPolicyReq=" + tcaPolicyReq);
82 } catch (IOException e) {
83 throw new TcaRequestFormatterException("Exception caught when attempting to create the policy JSON", e);
88 * Format Tca Policy Content JSON
91 * The refProp generally created by Spring, it's an access on the
92 * clds-references.properties file
93 * @param modelProperties
94 * The Model Prop created from BPMN JSON and BPMN properties JSON
96 * The service ID, if not specified getGlobal.getService will be
99 * The policyName, if not specified the
100 * modelProperties.getCurrentPolicyScopeAndPolicyName will be
103 * The Tca object, if not specified the
104 * modelProperties.setCurrentModelElementId will be used
105 * @return The Json node containing what should be sent to policy
107 public static JsonNode createPolicyContent(ClampProperties refProp, ModelProperties modelProperties, String service,
108 String policyName, Tca tca) {
110 String serviceToUse = service;
111 String policyNameToUse = policyName;
113 if (serviceToUse == null) {
114 serviceToUse = modelProperties.getGlobal().getService();
116 if (tcaToUse == null) {
117 tcaToUse = modelProperties.getType(Tca.class);
118 modelProperties.setCurrentModelElementId(tcaToUse.getId());
120 if (policyNameToUse == null) {
121 policyNameToUse = modelProperties.getCurrentPolicyScopeAndPolicyName();
123 ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", serviceToUse);
124 ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("eventName",
125 tcaToUse.getTcaItem().getEventName());
126 ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("policyName", policyNameToUse);
127 ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("controlLoopSchemaType",
128 tcaToUse.getTcaItem().getControlLoopSchemaType());
129 ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("metricsPerEventName").get(0));
130 addThresholds(refProp, serviceToUse, thresholdsParent, tcaToUse.getTcaItem(), modelProperties);
131 logger.info("tcaPolicyContent=" + rootNode.toString());
133 } catch (IOException e) {
134 throw new TcaRequestFormatterException("Exception caught when attempting to create the policy content JSON",
140 * Add threshold values to the existing policy JSON.
143 * The refProp generally created by Spring, it's an access on the
144 * clds-references.properties file
146 * The Service value extracted from Global section of the Bpmn
148 * @param appendToNode
149 * The JSON structure from where the thresholds section must be
152 * The TCA item contained in the Tca object
153 * @param modelProperties
154 * The Model Properties created from BPMN JSON and BPMN
157 private static void addThresholds(ClampProperties refProp, String service, ObjectNode appendToNode, TcaItem tcaItem,
158 ModelProperties modelProperties) {
159 ArrayNode tcaNodes = appendToNode.withArray("thresholds");
162 tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.thresholds.template", service);
163 for (TcaThreshold tcaThreshold : tcaItem.getTcaThresholds()) {
164 tcaNode.put("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId());
165 tcaNode.put("fieldPath", tcaThreshold.getFieldPath());
166 tcaNode.put("thresholdValue", tcaThreshold.getThreshold());
167 tcaNode.put("direction", tcaThreshold.getOperator());
168 tcaNode.put("closedLoopEventStatus", tcaThreshold.getClosedLoopEventStatus());
169 tcaNodes.add(tcaNode);
171 } catch (IOException e) {
172 throw new TcaRequestFormatterException("Exception caught when attempting to create the thresholds JSON", e);
177 * This method updates the blueprint that is received in the UI with the TCA
181 * * The refProp generally created by Spring, it's an access on
182 * the clds-references.properties file
183 * @param modelProperties
184 * The Model Prop created from BPMN JSON and BPMN properties JSON
186 * The yaml string received from the UI
187 * @return The updated YAML as a string
189 public static String updatedBlueprintWithConfiguration(ClampProperties refProp, ModelProperties modelProperties,
191 String jsonPolicy = ((ObjectNode) createPolicyContent(refProp, modelProperties, null, null, null)).toString();
192 logger.info("Yaml that will be updated:" + yamlValue);
193 Yaml yaml = new Yaml();
194 Map<String, Object> loadedYaml = (Map<String, Object>) yaml.load(yamlValue);
195 Map<String, Object> nodeTemplates = (Map<String, Object>) loadedYaml.get("node_templates");
196 Map<String, Object> tcaObject = (Map<String, Object>) nodeTemplates.get("tca_tca");
197 Map<String, Object> propsObject = (Map<String, Object>) tcaObject.get("properties");
198 Map<String, Object> appPreferences = (Map<String, Object>) propsObject.get("app_preferences");
199 appPreferences.put("tca_policy", jsonPolicy);
200 String blueprint = yaml.dump(loadedYaml);
201 logger.info("Yaml updated:" + blueprint);