More license header updates to appc-config files
[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 com.google.common.collect.Lists;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
42 import org.onap.sdnc.config.params.ParamsHandlerConstant;
43 import org.onap.sdnc.config.params.data.Parameter;
44 import org.onap.sdnc.config.params.data.PropertyDefinition;
45
46 public class PropertyDefinitionNode implements SvcLogicJavaPlugin {
47
48
49     private static final EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class);
50     private static final String STR_PD_CONTENT_MISSING = "Request Param (pdContent) is Missing ..";
51     private static final String STR_JSON_DATA_MISSING = "Request Param (jsonData) is Missing ..";
52     private static final String STR_SYSTEM_NAME_MISSING = "Request Param (systemName) is Missing ..";
53     private static final String STR_MERGE_JSON_DATA_MISSING = "Request Param (mergeJsonData) is Missing ..";
54     private static final String STR_MERGING_DATA_FAILED = "Failed in merging data to template";
55
56     public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
57         throws SvcLogicException {
58         log.info("Received processParamKeys call with params : " + inParams);
59         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
60         try {
61             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
62
63             String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
64             String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
65
66             if (StringUtils.isBlank(pdContent)) {
67                 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
68             }
69
70             if (StringUtils.isBlank(requestParamJson)) {
71                 throw new MissingParameterException(STR_JSON_DATA_MISSING);
72             }
73
74             PropertyDefinition propertyDefinition = parsePDContent(pdContent);
75             if (propertyDefinition != null) {
76                 requestParamJson =
77                     mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
78                 ctx.setAttribute(
79                     responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
80                     requestParamJson);
81             }
82
83             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
84                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
85         } catch (Exception e) {
86             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
87                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
88             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
89                 e.getMessage());
90             log.error(STR_MERGING_DATA_FAILED, e);
91             throw new SvcLogicException(e.getMessage());
92         }
93     }
94
95     public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
96         throws SvcLogicException {
97         log.info("Received processExternalSystemParamKeys call with params : " + inParams);
98         log.debug(
99             "Source sytem name passed in inParams will be ignored!!Source will be obtained from PD block!");
100         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
101         try {
102             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
103
104             String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
105             String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
106             String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME);
107
108             if (StringUtils.isBlank(pdContent)) {
109                 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
110             }
111
112             if (StringUtils.isBlank(requestParamJson)) {
113                 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
114                 log.info("processExternalSystemParamKeys:: "+ STR_JSON_DATA_MISSING);
115             }
116
117             if (StringUtils.isBlank(systemName)) {
118                 throw new MissingParameterException(STR_SYSTEM_NAME_MISSING);
119             }
120
121             tryGetSystemRequestParam(ctx, requestParamJson, pdContent);
122
123             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
124                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
125         } catch (Exception e) {
126             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
127                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
128             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
129                 e.getMessage());
130             log.error(STR_MERGING_DATA_FAILED, e);
131             throw new SvcLogicException(e.getMessage());
132         }
133     }
134
135     private void tryGetSystemRequestParam(SvcLogicContext ctx, String requestParamJson, String pdContent)
136         throws IOException, MissingParameterException {
137         PropertyDefinition propertyDefinition = parsePDContent(pdContent);
138         if (propertyDefinition != null) {
139             getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, ctx);
140         }
141     }
142
143
144     public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx)
145         throws SvcLogicException {
146         log.info("Received mergeJsonData call with params : " + inParams);
147         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
148         try {
149             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
150
151             String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
152             String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
153
154             if (StringUtils.isBlank(requestParamJson)) {
155                 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
156                 Map <String,String> tempMap=new HashMap<String, String> ();
157                 requestParamJson = tempMap.toString();
158                 log.info("mergeJsonData()::"+STR_JSON_DATA_MISSING);
159             }
160
161             if (StringUtils.isBlank(mergeJsonData)) {
162                 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
163             }
164
165             requestParamJson = mergeJson(requestParamJson, mergeJsonData);
166             ctx.setAttribute(
167                 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
168                 requestParamJson);
169             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
170                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
171         } catch (Exception e) {
172             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
173                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
174             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
175                 e.getMessage());
176             log.error(STR_MERGING_DATA_FAILED, e);
177             throw new SvcLogicException(e.getMessage());
178         }
179     }
180
181     private PropertyDefinition parsePDContent(String pdContent) throws IOException {
182         PropertyDefinition propertyDefinition = null;
183         if (StringUtils.isNotBlank(pdContent)) {
184             ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
185             propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
186         }
187         return propertyDefinition;
188     }
189
190     private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
191         String requestParamJson) throws MissingParameterException, IOException {
192
193         if (propertyDefinition == null) {
194             throw new MissingParameterException("PropertyDefinition is Missing ..");
195         }
196
197         if (StringUtils.isBlank(requestParamJson)) {
198             throw new MissingParameterException("Request Param is Missing ..");
199         }
200
201         ObjectMapper mapper = new ObjectMapper();
202         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
203         if (requestParamMap != null) {
204             List<Parameter> parameters = propertyDefinition.getParameters();
205             for (Parameter parameter : parameters) {
206                 if (parameter != null) {
207
208                     log.info("Checking Key " + parameter.getName() + ":: Source :"
209                         + parameter.getSource());
210                     // Add Only non external system keys,If it is not present in request Params
211                     tryAddParam(requestParamMap, parameter);
212                 }
213             }
214             log.info("Processed Request Param " + requestParamJson);
215             return mapper.writeValueAsString(requestParamMap);
216         }
217         return requestParamJson;
218     }
219
220     private void tryAddParam(Map<String, String> requestParamMap, Parameter parameter) {
221         if (!requestParamMap.containsKey(parameter.getName())
222             && StringUtils.isBlank(parameter.getSource())) {
223             log.info("Adding New Key " + parameter.getName());
224             requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
225         }
226     }
227
228     private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
229         String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
230
231         if (propertyDefinition == null) {
232             throw new MissingParameterException("PropertyDefinition is Missing ..");
233         }
234
235         if (StringUtils.isBlank(requestParamJson)) {
236             //throw new MissingParameterException("Request Param is Missing ..");
237             log.info("getSystemRequestParamInfoFromPD() ::: requestParamJson is blank!!!");
238             HashMap paramMap = new HashMap <String, String> ();
239             requestParamJson = paramMap.toString();
240         }
241
242         ObjectMapper mapper = new ObjectMapper();
243         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
244         Map<String, List<String>> systemKeysMap = new HashMap<>();
245         if (requestParamMap != null) {
246             List<Parameter> parameters = propertyDefinition.getParameters();
247
248             List<String> externalSystemKeys = new ArrayList<>();
249             for (Parameter parameter : parameters) {
250                 if (validateParameter(requestParamMap, parameter)) {
251
252                     String source = resolveSourceStr(parameter);
253                     resolveSourceParamValue(mapper, systemKeysMap, parameter, source);
254                     externalSystemKeys.add(parameter.getName());
255                     ctx.setAttribute(source + "." + parameter.getName(),
256                         mapper.writeValueAsString(parameter));
257                 }
258             }
259
260             for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
261                 String systemKeys = entry.getKey() + ".keys";
262                 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
263             }
264         }
265     }
266
267     private void resolveSourceParamValue(ObjectMapper mapper, Map<String, List<String>> systemKeysMap,
268         Parameter parameter, String source) throws JsonProcessingException {
269         if (systemKeysMap.containsKey(source)) {
270             log.info("Adding New System Key " + parameter.getName() + ":"
271                 + mapper.writeValueAsString(parameter));
272             List<String> l = systemKeysMap.get(source);
273             if (null != l) {
274                 l.add(parameter.getName());
275                 systemKeysMap.put(source, l);
276             }
277         } else {
278             log.info("Creating/Adding New System Key " + parameter.getName() + ":"
279                 + mapper.writeValueAsString(parameter));
280             List<String> l = Lists.newArrayList(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<String, String> ();
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 }