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 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;
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;
46 public class PropertyDefinitionNode implements SvcLogicJavaPlugin {
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";
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);
61 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
63 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
64 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
66 if (StringUtils.isBlank(pdContent)) {
67 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
70 if (StringUtils.isBlank(requestParamJson)) {
71 throw new MissingParameterException(STR_JSON_DATA_MISSING);
74 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
75 if (propertyDefinition != null) {
77 mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
79 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
90 log.error(STR_MERGING_DATA_FAILED, e);
91 throw new SvcLogicException(e.getMessage());
95 public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
96 throws SvcLogicException {
97 log.info("Received processExternalSystemParamKeys call with params : " + inParams);
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);
102 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
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);
108 if (StringUtils.isBlank(pdContent)) {
109 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
112 if (StringUtils.isBlank(requestParamJson)) {
113 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
114 log.info("processExternalSystemParamKeys:: "+ STR_JSON_DATA_MISSING);
117 if (StringUtils.isBlank(systemName)) {
118 throw new MissingParameterException(STR_SYSTEM_NAME_MISSING);
121 tryGetSystemRequestParam(ctx, requestParamJson, pdContent);
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,
130 log.error(STR_MERGING_DATA_FAILED, e);
131 throw new SvcLogicException(e.getMessage());
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);
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);
149 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
151 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
152 String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
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);
161 if (StringUtils.isBlank(mergeJsonData)) {
162 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
165 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
167 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
176 log.error(STR_MERGING_DATA_FAILED, e);
177 throw new SvcLogicException(e.getMessage());
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);
187 return propertyDefinition;
190 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
191 String requestParamJson) throws MissingParameterException, IOException {
193 if (propertyDefinition == null) {
194 throw new MissingParameterException("PropertyDefinition is Missing ..");
197 if (StringUtils.isBlank(requestParamJson)) {
198 throw new MissingParameterException("Request Param is Missing ..");
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) {
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);
214 log.info("Processed Request Param " + requestParamJson);
215 return mapper.writeValueAsString(requestParamMap);
217 return requestParamJson;
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());
228 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
229 String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
231 if (propertyDefinition == null) {
232 throw new MissingParameterException("PropertyDefinition is Missing ..");
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();
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();
248 List<String> externalSystemKeys = new ArrayList<>();
249 for (Parameter parameter : parameters) {
250 if (validateParameter(requestParamMap, parameter)) {
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));
260 for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
261 String systemKeys = entry.getKey() + ".keys";
262 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
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);
274 l.add(parameter.getName());
275 systemKeysMap.put(source, l);
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);
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());