Remove the clds-reference.properties
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / tca / TcaRequestFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.client.req.tca;
25
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;
31
32 import java.io.IOException;
33 import java.util.Map;
34
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;
42
43 /**
44  * Construct the requests for TCA policy and SDC.
45  */
46 public class TcaRequestFormatter {
47
48     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(TcaRequestFormatter.class);
49     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
50
51     /**
52      * Hide the default constructor.
53      */
54     private TcaRequestFormatter() {
55     }
56
57     /**
58      * Format Tca Policy JSON request.
59      *
60      * @param refProp
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
66      */
67     public static String createPolicyJson(ClampProperties refProp, ModelProperties modelProperties) {
68         try {
69             String service = modelProperties.getGlobal().getService();
70             Tca tca = modelProperties.getType(Tca.class);
71             modelProperties.setCurrentModelElementId(tca.getId());
72             ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.policy.template", service);
73             String policyName = modelProperties.getCurrentPolicyScopeAndPolicyName();
74             rootNode.put("policyName", policyName);
75             rootNode.put("description", "MicroService vCPE Policy");
76             ((ObjectNode) rootNode.get("content")).replace("tca_policy",
77                     createPolicyContent(refProp, modelProperties, service, policyName, tca));
78             String tcaPolicyReq = rootNode.toString();
79             logger.info("tcaPolicyReq=" + tcaPolicyReq);
80             return tcaPolicyReq;
81         } catch (IOException e) {
82             throw new TcaRequestFormatterException("Exception caught when attempting to create the policy JSON", e);
83         }
84     }
85
86     /**
87      * Format Tca Policy Content JSON
88      *
89      * @param refProp
90      *            The refProp generally created by Spring, it's an access on the
91      *            clds-references.properties file
92      * @param modelProperties
93      *            The Model Prop created from BPMN JSON and BPMN properties JSON
94      * @param service
95      *            The service ID, if not specified getGlobal.getService will be
96      *            used
97      * @param policyName
98      *            The policyName, if not specified the
99      *            modelProperties.getCurrentPolicyScopeAndPolicyName will be
100      *            used
101      * @param tca
102      *            The Tca object, if not specified the
103      *            modelProperties.setCurrentModelElementId will be used
104      * @return The Json node containing what should be sent to policy
105      */
106     public static JsonNode createPolicyContent(ClampProperties refProp, ModelProperties modelProperties, String service,
107             String policyName, Tca tca) {
108         try {
109             String serviceToUse = service;
110             String policyNameToUse = policyName;
111             Tca tcaToUse = tca;
112             if (serviceToUse == null) {
113                 serviceToUse = modelProperties.getGlobal().getService();
114             }
115             if (tcaToUse == null) {
116                 tcaToUse = modelProperties.getType(Tca.class);
117                 modelProperties.setCurrentModelElementId(tcaToUse.getId());
118             }
119             if (policyNameToUse == null) {
120                 policyNameToUse = modelProperties.getCurrentPolicyScopeAndPolicyName();
121             }
122             ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", serviceToUse);
123             ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("eventName",
124                     tcaToUse.getTcaItem().getEventName());
125             ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("policyName", policyNameToUse);
126             ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("controlLoopSchemaType",
127                     tcaToUse.getTcaItem().getControlLoopSchemaType());
128             ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("metricsPerEventName").get(0));
129             addThresholds(refProp, serviceToUse, thresholdsParent, tcaToUse.getTcaItem(), modelProperties);
130             logger.info("tcaPolicyContent=" + rootNode.toString());
131             return rootNode;
132         } catch (IOException e) {
133             throw new TcaRequestFormatterException("Exception caught when attempting to create the policy content JSON",
134                     e);
135         }
136     }
137
138     /**
139      * Add threshold values to the existing policy JSON.
140      *
141      * @param refProp
142      *            The refProp generally created by Spring, it's an access on the
143      *            clds-references.properties file
144      * @param service
145      *            The Service value extracted from Global section of the Bpmn
146      *            Properties JSON
147      * @param appendToNode
148      *            The JSON structure from where the thresholds section must be
149      *            added
150      * @param tcaItem
151      *            The TCA item contained in the Tca object
152      * @param modelProperties
153      *            The Model Properties created from BPMN JSON and BPMN
154      *            properties JSON
155      */
156     private static void addThresholds(ClampProperties refProp, String service, ObjectNode appendToNode, TcaItem tcaItem,
157             ModelProperties modelProperties) {
158         ArrayNode tcaNodes = appendToNode.withArray("thresholds");
159         ObjectNode tcaNode;
160         try {
161             tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.thresholds.template", service);
162             for (TcaThreshold tcaThreshold : tcaItem.getTcaThresholds()) {
163                 tcaNode.put("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId());
164                 tcaNode.put("fieldPath", tcaThreshold.getFieldPath());
165                 tcaNode.put("thresholdValue", tcaThreshold.getThreshold());
166                 tcaNode.put("direction", tcaThreshold.getOperator());
167                 tcaNode.put("closedLoopEventStatus", tcaThreshold.getClosedLoopEventStatus());
168                 tcaNodes.add(tcaNode);
169             }
170         } catch (IOException e) {
171             throw new TcaRequestFormatterException("Exception caught when attempting to create the thresholds JSON", e);
172         }
173     }
174
175     /**
176      * This method updates the blueprint that is received in the UI with the TCA
177      * Json.
178      * 
179      * @param refProp
180      *            * The refProp generally created by Spring, it's an access on
181      *            the clds-references.properties file
182      * @param modelProperties
183      *            The Model Prop created from BPMN JSON and BPMN properties JSON
184      * @param yamlValue
185      *            The yaml string received from the UI
186      * @return The updated YAML as a string
187      */
188     public static String updatedBlueprintWithConfiguration(ClampProperties refProp, ModelProperties modelProperties,
189             String yamlValue) {
190         String jsonPolicy = ((ObjectNode) createPolicyContent(refProp, modelProperties, null, null, null)).toString();
191         logger.info("Yaml that will be updated:" + yamlValue);
192         Yaml yaml = new Yaml();
193         Map<String, Object> loadedYaml = (Map<String, Object>) yaml.load(yamlValue);
194         Map<String, Object> nodeTemplates = (Map<String, Object>) loadedYaml.get("node_templates");
195         Map<String, Object> tcaObject = (Map<String, Object>) nodeTemplates.get("tca_tca");
196         Map<String, Object> propsObject = (Map<String, Object>) tcaObject.get("properties");
197         Map<String, Object> appPreferences = (Map<String, Object>) propsObject.get("app_preferences");
198         appPreferences.put("tca_policy", jsonPolicy);
199         String blueprint = yaml.dump(loadedYaml);
200         logger.info("Yaml updated:" + blueprint);
201         return blueprint;
202     }
203 }