Changed to unmaintained
[appc.git] / appc-config / appc-config-params / provider / src / main / java / org / onap / sdnc / config / params / parser / PropertyDefinitionNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.params.parser;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41 import org.onap.sdnc.config.params.ParamsHandlerConstant;
42 import org.onap.sdnc.config.params.data.Parameter;
43 import org.onap.sdnc.config.params.data.PropertyDefinition;
44
45 public class PropertyDefinitionNode implements SvcLogicJavaPlugin {
46
47
48     private static final EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
49     private static final String STR_PD_CONTENT_MISSING = "Request Param (pdContent) is Missing ..";
50     private static final String STR_JSON_DATA_MISSING = "Request Param (jsonData) is Missing ..";
51     private static final String STR_SYSTEM_NAME_MISSING = "Request Param (systemName) is Missing ..";
52     private static final String STR_MERGE_JSON_DATA_MISSING = "Request Param (mergeJsonData) is Missing ..";
53     private static final String STR_MERGING_DATA_FAILED = "Failed in merging data to template";
54
55     public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
56         throws SvcLogicException {
57         log.info("Received processParamKeys call with params : " + inParams);
58         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
59         try {
60             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
61
62             String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
63             String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
64
65             if (StringUtils.isBlank(pdContent)) {
66                 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
67             }
68
69             if (StringUtils.isBlank(requestParamJson)) {
70                 throw new MissingParameterException(STR_JSON_DATA_MISSING);
71             }
72
73             PropertyDefinition propertyDefinition = parsePDContent(pdContent);
74             if (propertyDefinition != null) {
75                 requestParamJson =
76                     mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
77                 ctx.setAttribute(
78                     responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
79                     requestParamJson);
80             }
81
82             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
83                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
84         } catch (Exception e) {
85             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
86                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
87             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
88                 e.getMessage());
89             log.error(STR_MERGING_DATA_FAILED, e);
90             throw new SvcLogicException(e.getMessage());
91         }
92     }
93
94     public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
95         throws SvcLogicException {
96         log.info("Received processExternalSystemParamKeys call with params : " + inParams);
97         log.debug(
98             "Source sytem name passed in inParams will be ignored!!Source will be obtained from PD block!");
99         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
100         try {
101             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
102
103             String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
104             String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
105             String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME);
106
107             if (StringUtils.isBlank(pdContent)) {
108                 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
109             }
110
111             if (StringUtils.isBlank(requestParamJson)) {
112                 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
113                 log.info("processExternalSystemParamKeys:: "+ STR_JSON_DATA_MISSING);
114             }
115
116             if (StringUtils.isBlank(systemName)) {
117                 throw new MissingParameterException(STR_SYSTEM_NAME_MISSING);
118             }
119
120             tryGetSystemRequestParam(ctx, requestParamJson, pdContent);
121
122             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
123                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
124         } catch (Exception e) {
125             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
126                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
127             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
128                 e.getMessage());
129             log.error(STR_MERGING_DATA_FAILED, e);
130             throw new SvcLogicException(e.getMessage());
131         }
132     }
133
134     private void tryGetSystemRequestParam(SvcLogicContext ctx, String requestParamJson, String pdContent)
135         throws IOException, MissingParameterException {
136         PropertyDefinition propertyDefinition = parsePDContent(pdContent);
137         if (propertyDefinition != null) {
138             getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, ctx);
139         }
140     }
141
142
143     public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx)
144         throws SvcLogicException {
145         log.info("Received mergeJsonData call with params : " + inParams);
146         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
147         try {
148             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
149
150             String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
151             String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
152
153             if (StringUtils.isBlank(requestParamJson)) {
154                 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
155                 Map <String,String> tempMap=new HashMap<> ();
156                 requestParamJson = tempMap.toString();
157                 log.info("mergeJsonData()::"+STR_JSON_DATA_MISSING);
158             }
159
160             if (StringUtils.isBlank(mergeJsonData)) {
161                 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
162             }
163
164             requestParamJson = mergeJson(requestParamJson, mergeJsonData);
165             ctx.setAttribute(
166                 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
167                 requestParamJson);
168             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
169                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
170         } catch (Exception e) {
171             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
172                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
173             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
174                 e.getMessage());
175             log.error(STR_MERGING_DATA_FAILED, e);
176             throw new SvcLogicException(e.getMessage());
177         }
178     }
179
180     private PropertyDefinition parsePDContent(String pdContent) throws IOException {
181         PropertyDefinition propertyDefinition = null;
182         if (StringUtils.isNotBlank(pdContent)) {
183             ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
184             propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
185         }
186         return propertyDefinition;
187     }
188
189     private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
190         String requestParamJson) throws MissingParameterException, IOException {
191
192         if (propertyDefinition == null) {
193             throw new MissingParameterException("PropertyDefinition is Missing ..");
194         }
195
196         if (StringUtils.isBlank(requestParamJson)) {
197             throw new MissingParameterException("Request Param is Missing ..");
198         }
199
200         ObjectMapper mapper = new ObjectMapper();
201         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
202         if (requestParamMap != null) {
203             List<Parameter> parameters = propertyDefinition.getParameters();
204             for (Parameter parameter : parameters) {
205                 if (parameter != null) {
206
207                     log.info("Checking Key " + parameter.getName() + ":: Source :"
208                         + parameter.getSource());
209                     // Add Only non external system keys,If it is not present in request Params
210                     tryAddParam(requestParamMap, parameter);
211                 }
212             }
213             log.info("Processed Request Param " + requestParamJson);
214             return mapper.writeValueAsString(requestParamMap);
215         }
216         return requestParamJson;
217     }
218
219     private void tryAddParam(Map<String, String> requestParamMap, Parameter parameter) {
220         if (!requestParamMap.containsKey(parameter.getName())
221             && StringUtils.isBlank(parameter.getSource())) {
222             log.info("Adding New Key " + parameter.getName());
223             requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
224         }
225     }
226
227     private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
228         String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
229
230         if (propertyDefinition == null) {
231             throw new MissingParameterException("PropertyDefinition is Missing ..");
232         }
233
234         if (StringUtils.isBlank(requestParamJson)) {
235             //throw new MissingParameterException("Request Param is Missing ..");
236             log.info("getSystemRequestParamInfoFromPD() ::: requestParamJson is blank!!!");
237             HashMap paramMap = new HashMap <String, String> ();
238             requestParamJson = paramMap.toString();
239         }
240
241         ObjectMapper mapper = new ObjectMapper();
242         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
243         Map<String, List<String>> systemKeysMap = new HashMap<>();
244         if (requestParamMap != null) {
245             List<Parameter> parameters = propertyDefinition.getParameters();
246
247             List<String> externalSystemKeys = new ArrayList<>();
248             for (Parameter parameter : parameters) {
249                 if (validateParameter(requestParamMap, parameter)) {
250
251                     String source = resolveSourceStr(parameter);
252                     resolveSourceParamValue(mapper, systemKeysMap, parameter, source);
253                     externalSystemKeys.add(parameter.getName());
254                     ctx.setAttribute(source + "." + parameter.getName(),
255                         mapper.writeValueAsString(parameter));
256                 }
257             }
258
259             for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
260                 String systemKeys = entry.getKey() + ".keys";
261                 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
262             }
263         }
264     }
265
266     private void resolveSourceParamValue(ObjectMapper mapper, Map<String, List<String>> systemKeysMap,
267         Parameter parameter, String source) throws JsonProcessingException {
268         if (systemKeysMap.containsKey(source)) {
269             log.info("Adding New System Key " + parameter.getName() + ":"
270                 + mapper.writeValueAsString(parameter));
271             List<String> l = systemKeysMap.get(source);
272             if (null != l) {
273                 l.add(parameter.getName());
274                 systemKeysMap.put(source, l);
275             }
276         } else {
277             log.info("Creating/Adding New System Key " + parameter.getName() + ":"
278                 + mapper.writeValueAsString(parameter));
279             List<String> l = new ArrayList<>();
280             l.add(parameter.getName());
281             systemKeysMap.put(source, l);
282         }
283     }
284
285     private String resolveSourceStr(Parameter parameter) {
286         String source = parameter.getSource();
287         if (StringUtils.equalsIgnoreCase(source, "A&AI")) {
288             source = "AAI";
289         }
290         source = StringUtils.upperCase(source);
291         return source;
292     }
293
294     private boolean validateParameter(Map<String, String> requestParamMap, Parameter parameter) {
295         return parameter != null && !requestParamMap.containsKey(parameter.getName())
296             && StringUtils.isNotBlank(parameter.getSource())
297             && !StringUtils.equalsIgnoreCase(parameter.getSource(), "Manual");
298     }
299
300     private String mergeJson(String requestParamJson, String systemParamJson) throws IOException {
301         ObjectMapper mapper = new ObjectMapper();
302         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
303         if (requestParamMap != null) {
304             Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class);
305             if (systemParamMap != null) {
306                 for (Entry<String, String> entry : systemParamMap.entrySet()) {
307                     log.trace("Megging System Key Values " + entry.getKey());
308                     requestParamMap.put(entry.getKey(), entry.getValue());
309                 }
310             }
311             log.info("Processed Request Param " + requestParamJson);
312             return mapper.writeValueAsString(requestParamMap);
313         }
314         return requestParamJson;
315     }
316
317     public void validateParams(Map<String, String> inParams, SvcLogicContext ctx)
318         throws SvcLogicException {
319         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
320         String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
321         String configParams =
322             inParams.get(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
323         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
324         log.info("Processed pdContent Param " + pdContent);
325         log.info("Processed config Param " + configParams);
326
327         try {
328             if (StringUtils.isBlank(pdContent)) {
329                 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
330             }
331
332             if (StringUtils.isBlank(configParams)) {
333                 //throw new MissingParameterException("Request Param (configParams) is Missing ..");
334                 Map <String,String> tempMap=new HashMap<> ();
335                 configParams = tempMap.toString();
336                 log.info("validateParams():: Request Param (configParams) is Missing ..");
337                 
338             }
339             PropertyDefinition propertyDefinition = parsePDContent(pdContent);
340             ObjectMapper mapper = new ObjectMapper();
341             Map<String, String> requestParamMap = mapper.readValue(configParams, HashMap.class);
342             List<Parameter> parameters = propertyDefinition.getParameters();
343             Map<String, String> missingKeys = new HashMap<>();
344             for (Parameter parameter : parameters) {
345                 if (parameter != null && parameter.isRequired()) {
346                     resolveParameterValue(requestParamMap, missingKeys, parameter);
347                 }
348             }
349             if (missingKeys.size() > 0) {
350
351                 String requiredFields = mapper.writeValueAsString(missingKeys);
352                 log.info(" Below mentioned keys and respective  source type are mandatory");
353                 log.info(requiredFields);
354
355                 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
356                     ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
357                 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
358                     ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
359                 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
360                     "Missing Mandatory Keys and source are" + requiredFields);
361                 throw new SvcLogicException(
362                     " Missing  Mandatory Keys and source are" + requiredFields);
363             } else {
364                 log.info("success ");
365                 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
366                     ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
367             }
368
369         } catch (Exception e) {
370             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
371                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
372             ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
373                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
374             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
375                 e.getMessage());
376             log.error("Failed to validate params", e);
377             throw new SvcLogicException(e.getMessage());
378         }
379     }
380
381     private void resolveParameterValue(Map<String, String> requestParamMap, Map<String, String> missingKeys,
382         Parameter parameter) {
383         if (!requestParamMap.containsKey(parameter.getName())) {
384             missingKeys.put(parameter.getName(), parameter.getSource());
385         } else {
386             if ((requestParamMap.get(parameter.getName()) == null)
387                 || (requestParamMap.get(parameter.getName()).isEmpty())) {
388                 missingKeys.put(parameter.getName(), parameter.getSource());
389             }
390         }
391     }
392 }