2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.params.parser;
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;
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;
47 public class PropertyDefinitionNode implements SvcLogicJavaPlugin {
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";
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);
62 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
64 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
65 String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT);
67 if (StringUtils.isBlank(pdContent)) {
68 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
71 if (StringUtils.isBlank(requestParamJson)) {
72 throw new MissingParameterException(STR_JSON_DATA_MISSING);
75 PropertyDefinition propertyDefinition = parsePDContent(pdContent);
76 if (propertyDefinition != null) {
78 mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson);
80 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
91 log.error(STR_MERGING_DATA_FAILED, e);
92 throw new SvcLogicException(e.getMessage());
96 public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx)
97 throws SvcLogicException {
98 log.info("Received processExternalSystemParamKeys call with params : " + inParams);
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);
103 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
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);
109 if (StringUtils.isBlank(pdContent)) {
110 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
113 if (StringUtils.isBlank(requestParamJson)) {
114 //throw new MissingParameterException(STR_JSON_DATA_MISSING);
115 log.info("processExternalSystemParamKeys:: "+ STR_JSON_DATA_MISSING);
118 if (StringUtils.isBlank(systemName)) {
119 throw new MissingParameterException(STR_SYSTEM_NAME_MISSING);
122 tryGetSystemRequestParam(ctx, requestParamJson, pdContent);
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,
131 log.error(STR_MERGING_DATA_FAILED, e);
132 throw new SvcLogicException(e.getMessage());
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);
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);
150 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
152 String requestParamJson = inParams.get(ParamsHandlerConstant.INPUT_PARAM_JSON_DATA);
153 String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA);
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);
162 if (StringUtils.isBlank(mergeJsonData)) {
163 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
166 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
168 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
177 log.error(STR_MERGING_DATA_FAILED, e);
178 throw new SvcLogicException(e.getMessage());
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);
188 return propertyDefinition;
191 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
192 String requestParamJson) throws MissingParameterException, IOException {
194 if (propertyDefinition == null) {
195 throw new MissingParameterException("PropertyDefinition is Missing ..");
198 if (StringUtils.isBlank(requestParamJson)) {
199 throw new MissingParameterException("Request Param is Missing ..");
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) {
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);
215 log.info("Processed Request Param " + requestParamJson);
216 return mapper.writeValueAsString(requestParamMap);
218 return requestParamJson;
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());
229 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
230 String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
232 if (propertyDefinition == null) {
233 throw new MissingParameterException("PropertyDefinition is Missing ..");
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();
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();
249 List<String> externalSystemKeys = new ArrayList<>();
250 for (Parameter parameter : parameters) {
251 if (validateParameter(requestParamMap, parameter)) {
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));
261 for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
262 String systemKeys = entry.getKey() + ".keys";
263 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
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);
275 l.add(parameter.getName());
276 systemKeysMap.put(source, l);
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);
286 private String resolveSourceStr(Parameter parameter) {
287 String source = parameter.getSource();
288 if (StringUtils.equalsIgnoreCase(source, "A&AI")) {
291 source = StringUtils.upperCase(source);
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");
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());
312 log.info("Processed Request Param " + requestParamJson);
313 return mapper.writeValueAsString(requestParamMap);
315 return requestParamJson;
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);
329 if (StringUtils.isBlank(pdContent)) {
330 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
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 ..");
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);
350 if (missingKeys.size() > 0) {
352 String requiredFields = mapper.writeValueAsString(missingKeys);
353 log.info(" Below mentioned keys and respective source type are mandatory");
354 log.info(requiredFields);
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);
365 log.info("success ");
366 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
367 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
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,
377 log.error("Failed to validate params", e);
378 throw new SvcLogicException(e.getMessage());
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());
387 if ((requestParamMap.get(parameter.getName()) == null)
388 || (requestParamMap.get(parameter.getName()).isEmpty())) {
389 missingKeys.put(parameter.getName(), parameter.getSource());