Add junit coverage to TimedOutException class
[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             }
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             }
157
158             if (StringUtils.isBlank(mergeJsonData)) {
159                 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
160             }
161
162             requestParamJson = mergeJson(requestParamJson, mergeJsonData);
163             ctx.setAttribute(
164                 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
165                 requestParamJson);
166             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
167                 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
168         } catch (Exception e) {
169             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
170                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
171             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
172                 e.getMessage());
173             log.error(STR_MERGING_DATA_FAILED, e);
174             throw new SvcLogicException(e.getMessage());
175         }
176     }
177
178     private PropertyDefinition parsePDContent(String pdContent) throws IOException {
179         PropertyDefinition propertyDefinition = null;
180         if (StringUtils.isNotBlank(pdContent)) {
181             ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
182             propertyDefinition = mapper.readValue(pdContent, PropertyDefinition.class);
183         }
184         return propertyDefinition;
185     }
186
187     private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
188         String requestParamJson) throws MissingParameterException, IOException {
189
190         if (propertyDefinition == null) {
191             throw new MissingParameterException("PropertyDefinition is Missing ..");
192         }
193
194         if (StringUtils.isBlank(requestParamJson)) {
195             throw new MissingParameterException("Request Param is Missing ..");
196         }
197
198         ObjectMapper mapper = new ObjectMapper();
199         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
200         if (requestParamMap != null) {
201             List<Parameter> parameters = propertyDefinition.getParameters();
202             for (Parameter parameter : parameters) {
203                 if (parameter != null) {
204
205                     log.info("Checking Key " + parameter.getName() + ":: Source :"
206                         + parameter.getSource());
207                     // Add Only non external system keys,If it is not present in request Params
208                     tryAddParam(requestParamMap, parameter);
209                 }
210             }
211             log.info("Processed Request Param " + requestParamJson);
212             return mapper.writeValueAsString(requestParamMap);
213         }
214         return requestParamJson;
215     }
216
217     private void tryAddParam(Map<String, String> requestParamMap, Parameter parameter) {
218         if (!requestParamMap.containsKey(parameter.getName())
219             && StringUtils.isBlank(parameter.getSource())) {
220             log.info("Adding New Key " + parameter.getName());
221             requestParamMap.put(parameter.getName(), parameter.getDefaultValue());
222         }
223     }
224
225     private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
226         String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
227
228         if (propertyDefinition == null) {
229             throw new MissingParameterException("PropertyDefinition is Missing ..");
230         }
231
232         if (StringUtils.isBlank(requestParamJson)) {
233             throw new MissingParameterException("Request Param is Missing ..");
234         }
235
236         ObjectMapper mapper = new ObjectMapper();
237         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
238         Map<String, List<String>> systemKeysMap = new HashMap<>();
239         if (requestParamMap != null) {
240             List<Parameter> parameters = propertyDefinition.getParameters();
241
242             List<String> externalSystemKeys = new ArrayList<>();
243             for (Parameter parameter : parameters) {
244                 if (validateParameter(requestParamMap, parameter)) {
245
246                     String source = resolveSourceStr(parameter);
247                     resolveSourceParamValue(mapper, systemKeysMap, parameter, source);
248                     externalSystemKeys.add(parameter.getName());
249                     ctx.setAttribute(source + "." + parameter.getName(),
250                         mapper.writeValueAsString(parameter));
251                 }
252             }
253
254             for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
255                 String systemKeys = entry.getKey() + ".keys";
256                 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
257             }
258         }
259     }
260
261     private void resolveSourceParamValue(ObjectMapper mapper, Map<String, List<String>> systemKeysMap,
262         Parameter parameter, String source) throws JsonProcessingException {
263         if (systemKeysMap.containsKey(source)) {
264             log.info("Adding New System Key " + parameter.getName() + ":"
265                 + mapper.writeValueAsString(parameter));
266             List<String> l = systemKeysMap.get(source);
267             if (null != l) {
268                 l.add(parameter.getName());
269                 systemKeysMap.put(source, l);
270             }
271         } else {
272             log.info("Creating/Adding New System Key " + parameter.getName() + ":"
273                 + mapper.writeValueAsString(parameter));
274             List<String> l = Lists.newArrayList(parameter.getName());
275             systemKeysMap.put(source, l);
276         }
277     }
278
279     private String resolveSourceStr(Parameter parameter) {
280         String source = parameter.getSource();
281         if (StringUtils.equalsIgnoreCase(source, "A&AI")) {
282             source = "AAI";
283         }
284         source = StringUtils.upperCase(source);
285         return source;
286     }
287
288     private boolean validateParameter(Map<String, String> requestParamMap, Parameter parameter) {
289         return parameter != null && !requestParamMap.containsKey(parameter.getName())
290             && StringUtils.isNotBlank(parameter.getSource())
291             && !StringUtils.equalsIgnoreCase(parameter.getSource(), "Manual");
292     }
293
294     private String mergeJson(String requestParamJson, String systemParamJson) throws IOException {
295         ObjectMapper mapper = new ObjectMapper();
296         Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class);
297         if (requestParamMap != null) {
298             Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class);
299             if (systemParamMap != null) {
300                 for (Entry<String, String> entry : systemParamMap.entrySet()) {
301                     log.trace("Megging System Key Values " + entry.getKey());
302                     requestParamMap.put(entry.getKey(), entry.getValue());
303                 }
304             }
305             log.info("Processed Request Param " + requestParamJson);
306             return mapper.writeValueAsString(requestParamMap);
307         }
308         return requestParamJson;
309     }
310
311     public void validateParams(Map<String, String> inParams, SvcLogicContext ctx)
312         throws SvcLogicException {
313         String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX);
314         String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
315         String configParams =
316             inParams.get(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER);
317         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
318         log.info("Processed pdContent Param " + pdContent);
319         log.info("Processed config Param " + configParams);
320
321         try {
322             if (StringUtils.isBlank(pdContent)) {
323                 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
324             }
325
326             if (StringUtils.isBlank(configParams)) {
327                 throw new MissingParameterException("Request Param (configParams) is Missing ..");
328             }
329             PropertyDefinition propertyDefinition = parsePDContent(pdContent);
330             ObjectMapper mapper = new ObjectMapper();
331             Map<String, String> requestParamMap = mapper.readValue(configParams, HashMap.class);
332             List<Parameter> parameters = propertyDefinition.getParameters();
333             Map<String, String> missingKeys = new HashMap<>();
334             for (Parameter parameter : parameters) {
335                 if (parameter != null && parameter.isRequired()) {
336                     resolveParameterValue(requestParamMap, missingKeys, parameter);
337                 }
338             }
339             if (missingKeys.size() > 0) {
340
341                 String requiredFields = mapper.writeValueAsString(missingKeys);
342                 log.info(" Below mentioned keys and respective  source type are mandatory");
343                 log.info(requiredFields);
344
345                 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
346                     ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
347                 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
348                     ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
349                 ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
350                     "Missing Mandatory Keys and source are" + requiredFields);
351                 throw new SvcLogicException(
352                     " Missing  Mandatory Keys and source are" + requiredFields);
353             } else {
354                 log.info("success ");
355                 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
356                     ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
357             }
358
359         } catch (Exception e) {
360             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
361                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
362             ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
363                 ParamsHandlerConstant.OUTPUT_STATUS_FAILURE);
364             ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE,
365                 e.getMessage());
366             log.error("Failed to validate params", e);
367             throw new SvcLogicException(e.getMessage());
368         }
369     }
370
371     private void resolveParameterValue(Map<String, String> requestParamMap, Map<String, String> missingKeys,
372         Parameter parameter) {
373         if (!requestParamMap.containsKey(parameter.getName())) {
374             missingKeys.put(parameter.getName(), parameter.getSource());
375         } else {
376             if ((requestParamMap.get(parameter.getName()) == null)
377                 || (requestParamMap.get(parameter.getName()).isEmpty())) {
378                 missingKeys.put(parameter.getName(), parameter.getSource());
379             }
380         }
381     }
382 }