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);
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);
158 if (StringUtils.isBlank(mergeJsonData)) {
159 throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING);
162 requestParamJson = mergeJson(requestParamJson, mergeJsonData);
164 responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER,
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,
173 log.error(STR_MERGING_DATA_FAILED, e);
174 throw new SvcLogicException(e.getMessage());
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);
184 return propertyDefinition;
187 private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition,
188 String requestParamJson) throws MissingParameterException, IOException {
190 if (propertyDefinition == null) {
191 throw new MissingParameterException("PropertyDefinition is Missing ..");
194 if (StringUtils.isBlank(requestParamJson)) {
195 throw new MissingParameterException("Request Param is Missing ..");
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) {
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);
211 log.info("Processed Request Param " + requestParamJson);
212 return mapper.writeValueAsString(requestParamMap);
214 return requestParamJson;
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());
225 private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition,
226 String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException {
228 if (propertyDefinition == null) {
229 throw new MissingParameterException("PropertyDefinition is Missing ..");
232 if (StringUtils.isBlank(requestParamJson)) {
233 throw new MissingParameterException("Request Param is Missing ..");
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();
242 List<String> externalSystemKeys = new ArrayList<>();
243 for (Parameter parameter : parameters) {
244 if (validateParameter(requestParamMap, parameter)) {
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));
254 for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) {
255 String systemKeys = entry.getKey() + ".keys";
256 ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue()));
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);
268 l.add(parameter.getName());
269 systemKeysMap.put(source, l);
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);
279 private String resolveSourceStr(Parameter parameter) {
280 String source = parameter.getSource();
281 if (StringUtils.equalsIgnoreCase(source, "A&AI")) {
284 source = StringUtils.upperCase(source);
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");
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());
305 log.info("Processed Request Param " + requestParamJson);
306 return mapper.writeValueAsString(requestParamMap);
308 return requestParamJson;
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);
322 if (StringUtils.isBlank(pdContent)) {
323 throw new MissingParameterException(STR_PD_CONTENT_MISSING);
326 if (StringUtils.isBlank(configParams)) {
327 throw new MissingParameterException("Request Param (configParams) is Missing ..");
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);
339 if (missingKeys.size() > 0) {
341 String requiredFields = mapper.writeValueAsString(missingKeys);
342 log.info(" Below mentioned keys and respective source type are mandatory");
343 log.info(requiredFields);
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);
354 log.info("success ");
355 ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS,
356 ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS);
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,
366 log.error("Failed to validate params", e);
367 throw new SvcLogicException(e.getMessage());
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());
376 if ((requestParamMap.get(parameter.getName()) == null)
377 || (requestParamMap.get(parameter.getName()).isEmpty())) {
378 missingKeys.put(parameter.getName(), parameter.getSource());