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