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