2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.params.parser;
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;
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;
45 public class PropertyDefinitionNode implements SvcLogicJavaPlugin {
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";
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);
60 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
62 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
63 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
65 if (StringUtils.isBlank(pdContent)) {
66 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
69 if (StringUtils.isBlank(requestParamJson)) {
70 throw new MissingParameterException(STR_JSON_DATA_MISSING);
73 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
74 if (propertyDefinition != null) {
76 mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
78 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
89 log.error(STR_MERGING_DATA_FAILED, e);
90 throw new SvcLogicException(e.getMessage());
94 public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
95 throws SvcLogicException {
96 log.info("Received processExternalSystemParamKeys call with params : " + inParams);
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);
101 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
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);
107 if (StringUtils.isBlank(pdContent)) {
108 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
111 if (StringUtils.isBlank(requestParamJson)) {
112 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
113 log.info("processExternalSystemParamKeys:: "+ STR_JSON_DATA_MISSING);
116 if (StringUtils.isBlank(systemName)) {
117 throw new MissingParameterException(STR_SYSTEM_NAME_MISSING);
120 tryGetSystemRequestParam(ctx, requestParamJson, pdContent);
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,
129 log.error(STR_MERGING_DATA_FAILED, e);
130 throw new SvcLogicException(e.getMessage());
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);
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);
148 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
150 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
151 String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
153 if (StringUtils.isBlank(requestParamJson)) {
154 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
155 Map <String,String> tempMap=new HashMap<String, String> ();
156 requestParamJson = tempMap.toString();
157 log.info("mergeJsonData()::"+STR_JSON_DATA_MISSING);
160 if (StringUtils.isBlank(mergeJsonData)) {
161 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
164 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
166 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
175 log.error(STR_MERGING_DATA_FAILED, e);
176 throw new SvcLogicException(e.getMessage());
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);
186 return propertyDefinition;
189 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
190 String requestParamJson) throws MissingParameterException, IOException {
192 if (propertyDefinition == null) {
193 throw new MissingParameterException("PropertyDefinition is Missing ..");
196 if (StringUtils.isBlank(requestParamJson)) {
197 throw new MissingParameterException("Request Param is Missing ..");
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) {
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);
213 log.info("Processed Request Param " + requestParamJson);
214 return mapper.writeValueAsString(requestParamMap);
216 return requestParamJson;
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());
227 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
228 String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
230 if (propertyDefinition == null) {
231 throw new MissingParameterException("PropertyDefinition is Missing ..");
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();
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();
247 List<String> externalSystemKeys = new ArrayList<>();
248 for (Parameter parameter : parameters) {
249 if (validateParameter(requestParamMap, parameter)) {
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));
259 for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
260 String systemKeys = entry.getKey() + ".keys";
261 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
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);
273 l.add(parameter.getName());
274 systemKeysMap.put(source, l);
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);
285 private String resolveSourceStr(Parameter parameter) {
286 String source = parameter.getSource();
287 if (StringUtils.equalsIgnoreCase(source, "A&AI")) {
290 source = StringUtils.upperCase(source);
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");
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());
311 log.info("Processed Request Param " + requestParamJson);
312 return mapper.writeValueAsString(requestParamMap);
314 return requestParamJson;
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);
328 if (StringUtils.isBlank(pdContent)) {
329 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
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 ..");
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);
349 if (missingKeys.size() > 0) {
351 String requiredFields = mapper.writeValueAsString(missingKeys);
352 log.info(" Below mentioned keys and respective source type are mandatory");
353 log.info(requiredFields);
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);
364 log.info("success ");
365 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
366 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
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,
376 log.error("Failed to validate params", e);
377 throw new SvcLogicException(e.getMessage());
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());
386 if ((requestParamMap.get(parameter.getName()) == null)
387 || (requestParamMap.get(parameter.getName()).isEmpty())) {
388 missingKeys.put(parameter.getName(), parameter.getSource());