diff options
author | 2018-02-26 10:52:09 +0100 | |
---|---|---|
committer | 2018-02-26 19:10:26 +0000 | |
commit | 0c712bb997cb5e5482a322d3deef107bd30f5d8e (patch) | |
tree | 1b66b22fe261881d64ba91c864e311cfe513d804 /appc-config/appc-config-params/provider/src | |
parent | a050062ff4a19586d52a045e79f2f065477adec0 (diff) |
PropertyDefinitionNode sonar fixes
Change-Id: Ibc198788f20a8555628d75c785ebcc60908b39db
Issue-ID: APPC-664
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Diffstat (limited to 'appc-config/appc-config-params/provider/src')
2 files changed, 171 insertions, 149 deletions
diff --git a/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/MissingParameterException.java b/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/MissingParameterException.java new file mode 100644 index 000000000..14d54341c --- /dev/null +++ b/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/MissingParameterException.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdnc.config.params.parser; + +public class MissingParameterException extends Exception{ + + public MissingParameterException(String message) { + super(message); + } +} diff --git a/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/PropertyDefinitionNode.java b/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/PropertyDefinitionNode.java index aafebda17..a9707b6a2 100644 --- a/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/PropertyDefinitionNode.java +++ b/appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/parser/PropertyDefinitionNode.java @@ -24,11 +24,18 @@ package org.onap.sdnc.config.params.parser; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.google.common.collect.Lists; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; @@ -36,21 +43,19 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; import org.onap.sdnc.config.params.ParamsHandlerConstant; import org.onap.sdnc.config.params.data.Parameter; import org.onap.sdnc.config.params.data.PropertyDefinition; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; public class PropertyDefinitionNode implements SvcLogicJavaPlugin { - private static final EELFLogger log = - EELFManager.getInstance().getLogger(PropertyDefinitionNode.class); + private static final EELFLogger log = EELFManager.getInstance().getLogger(PropertyDefinitionNode.class); + private static final String STR_PD_CONTENT_MISSING = "Request Param (pdContent) is Missing .."; + private static final String STR_JSON_DATA_MISSING = "Request Param (jsonData) is Missing .."; + private static final String STR_SYSTEM_NAME_MISSING = "Request Param (systemName) is Missing .."; + private static final String STR_MERGE_JSON_DATA_MISSING = "Request Param (mergeJsonData) is Missing .."; + private static final String STR_MERGING_DATA_FAILED = "Failed in merging data to template"; public void processMissingParamKeys(Map<String, String> inParams, SvcLogicContext ctx) - throws SvcLogicException { + throws SvcLogicException { log.info("Received processParamKeys call with params : " + inParams); String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX); try { @@ -60,39 +65,39 @@ public class PropertyDefinitionNode implements SvcLogicJavaPlugin { String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT); if (StringUtils.isBlank(pdContent)) { - throw new Exception("Request Param (pdContent) is Missing .."); + throw new MissingParameterException(STR_PD_CONTENT_MISSING); } if (StringUtils.isBlank(requestParamJson)) { - throw new Exception("Request Param (jsonData) is Missing .."); + throw new MissingParameterException(STR_JSON_DATA_MISSING); } PropertyDefinition propertyDefinition = parsePDContent(pdContent); if (propertyDefinition != null) { requestParamJson = - mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson); + mergeMissingRequestParamFromPD(propertyDefinition, requestParamJson); ctx.setAttribute( - responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, - requestParamJson); + responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, + requestParamJson); } ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); + ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); } catch (Exception e) { ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE, - e.getMessage()); - log.error("Failed in merging data to template " + e.getMessage()); + e.getMessage()); + log.error(STR_MERGING_DATA_FAILED, e); throw new SvcLogicException(e.getMessage()); } } public void processExternalSystemParamKeys(Map<String, String> inParams, SvcLogicContext ctx) - throws SvcLogicException { + throws SvcLogicException { log.info("Received processExternalSystemParamKeys call with params : " + inParams); log.debug( - "Source sytem name passed in inParams will be ignored!!Source will be obtained from PD block!"); + "Source sytem name passed in inParams will be ignored!!Source will be obtained from PD block!"); String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX); try { responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : ""; @@ -101,39 +106,43 @@ public class PropertyDefinitionNode implements SvcLogicJavaPlugin { String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT); String systemName = inParams.get(ParamsHandlerConstant.INPUT_PARAM_SYSTEM_NAME); - if (StringUtils.isBlank(pdContent)) { - throw new Exception("Request Param (pdContent) is Missing .."); + throw new MissingParameterException(STR_PD_CONTENT_MISSING); } if (StringUtils.isBlank(requestParamJson)) { - throw new Exception("Request Param (jsonData) is Missing .."); + throw new MissingParameterException(STR_JSON_DATA_MISSING); } if (StringUtils.isBlank(systemName)) { - throw new Exception("Request Param (systemName) is Missing .."); + throw new MissingParameterException(STR_SYSTEM_NAME_MISSING); } - PropertyDefinition propertyDefinition = parsePDContent(pdContent); - if (propertyDefinition != null) { - getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, ctx); - } + tryGetSystemRequestParam(ctx, requestParamJson, pdContent); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); + ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); } catch (Exception e) { ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE, - e.getMessage()); - log.error("Failed in merging data to template " + e.getMessage()); + e.getMessage()); + log.error(STR_MERGING_DATA_FAILED, e); throw new SvcLogicException(e.getMessage()); } } + private void tryGetSystemRequestParam(SvcLogicContext ctx, String requestParamJson, String pdContent) + throws IOException, MissingParameterException { + PropertyDefinition propertyDefinition = parsePDContent(pdContent); + if (propertyDefinition != null) { + getSystemRequestParamInfoFromPD(propertyDefinition, requestParamJson, ctx); + } + } + public void mergeJsonData(Map<String, String> inParams, SvcLogicContext ctx) - throws SvcLogicException { + throws SvcLogicException { log.info("Received mergeJsonData call with params : " + inParams); String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX); try { @@ -143,34 +152,30 @@ public class PropertyDefinitionNode implements SvcLogicJavaPlugin { String mergeJsonData = inParams.get(ParamsHandlerConstant.INPUT_PARAM_MERGE__JSON_DATA); if (StringUtils.isBlank(requestParamJson)) { - throw new Exception("Request Param (jsonData) is Missing .."); + throw new MissingParameterException(STR_JSON_DATA_MISSING); } if (StringUtils.isBlank(mergeJsonData)) { - throw new Exception("Request Param (mergeJsonData) is Missing .."); + throw new MissingParameterException(STR_MERGE_JSON_DATA_MISSING); } requestParamJson = mergeJson(requestParamJson, mergeJsonData); ctx.setAttribute( - responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, - requestParamJson); + responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER, + requestParamJson); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); + ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); } catch (Exception e) { ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE, - e.getMessage()); - log.error("Failed in merging data to template " + e.getMessage()); + e.getMessage()); + log.error(STR_MERGING_DATA_FAILED, e); throw new SvcLogicException(e.getMessage()); } } - - /* */ - - private PropertyDefinition parsePDContent(String pdContent) - throws JsonParseException, JsonMappingException, IOException { + private PropertyDefinition parsePDContent(String pdContent) throws IOException { PropertyDefinition propertyDefinition = null; if (StringUtils.isNotBlank(pdContent)) { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); @@ -179,16 +184,15 @@ public class PropertyDefinitionNode implements SvcLogicJavaPlugin { return propertyDefinition; } - private String mergeMissingRequestParamFromPD(PropertyDefinition propertyDefinition, - String requestParamJson) throws Exception { + String requestParamJson) throws MissingParameterException, IOException { if (propertyDefinition == null) { - throw new Exception("PropertyDefinition is Missing .."); + throw new MissingParameterException("PropertyDefinition is Missing .."); } if (StringUtils.isBlank(requestParamJson)) { - throw new Exception("Request Param is Missing .."); + throw new MissingParameterException("Request Param is Missing .."); } ObjectMapper mapper = new ObjectMapper(); @@ -199,188 +203,180 @@ public class PropertyDefinitionNode implements SvcLogicJavaPlugin { if (parameter != null) { log.info("Checking Key " + parameter.getName() + ":: Source :" - + parameter.getSource()); + + parameter.getSource()); // Add Only non external system keys,If it is not present in request Params - if (!requestParamMap.containsKey(parameter.getName()) - && StringUtils.isBlank(parameter.getSource())) { - log.info("Adding New Key " + parameter.getName()); - requestParamMap.put(parameter.getName(), parameter.getDefaultValue()); - } + tryAddParam(requestParamMap, parameter); } } - requestParamJson = mapper.writeValueAsString(requestParamMap); log.info("Processed Request Param " + requestParamJson); + return mapper.writeValueAsString(requestParamMap); } - return requestParamJson; } + private void tryAddParam(Map<String, String> requestParamMap, Parameter parameter) { + if (!requestParamMap.containsKey(parameter.getName()) + && StringUtils.isBlank(parameter.getSource())) { + log.info("Adding New Key " + parameter.getName()); + requestParamMap.put(parameter.getName(), parameter.getDefaultValue()); + } + } + private void getSystemRequestParamInfoFromPD(PropertyDefinition propertyDefinition, - String requestParamJson, SvcLogicContext ctx) throws Exception { + String requestParamJson, SvcLogicContext ctx) throws MissingParameterException, IOException { if (propertyDefinition == null) { - throw new Exception("PropertyDefinition is Missing .."); + throw new MissingParameterException("PropertyDefinition is Missing .."); } if (StringUtils.isBlank(requestParamJson)) { - throw new Exception("Request Param is Missing .."); + throw new MissingParameterException("Request Param is Missing .."); } ObjectMapper mapper = new ObjectMapper(); Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class); - Map<String, List<String>> systemKeysMap = new HashMap<String, List<String>>(); + Map<String, List<String>> systemKeysMap = new HashMap<>(); if (requestParamMap != null) { List<Parameter> parameters = propertyDefinition.getParameters(); - List<String> externalSystemKeys = new ArrayList<String>(); + List<String> externalSystemKeys = new ArrayList<>(); for (Parameter parameter : parameters) { - if (parameter != null) { - if (!requestParamMap.containsKey(parameter.getName()) - && StringUtils.isNotBlank(parameter.getSource()) - && !StringUtils.equalsIgnoreCase(parameter.getSource(), "Manual")) { - String source = parameter.getSource(); - if (StringUtils.equalsIgnoreCase(source, "A&AI")) - source = "AAI"; - source = StringUtils.upperCase(source); - if (systemKeysMap.containsKey(source)) { - log.info("Adding New System Key " + parameter.getName() + ":" - + mapper.writeValueAsString(parameter)); - List l = systemKeysMap.get(source); - if (null != l) { - l.add(parameter.getName()); - systemKeysMap.put(source, l); - } - } else { - log.info("Creating/Adding New System Key " + parameter.getName() + ":" - + mapper.writeValueAsString(parameter)); - List<String> l = new ArrayList<String>(); - if (null != l) { - l.add(parameter.getName()); - systemKeysMap.put(source, l); - } - } - - externalSystemKeys.add(parameter.getName()); - ctx.setAttribute(source + "." + parameter.getName(), - mapper.writeValueAsString(parameter)); - } + if (validateParameter(requestParamMap, parameter)) { + + String source = resolveSourceStr(parameter); + resolveSourceParamValue(mapper, systemKeysMap, parameter, source); + externalSystemKeys.add(parameter.getName()); + ctx.setAttribute(source + "." + parameter.getName(), + mapper.writeValueAsString(parameter)); } } - for (String sysName : systemKeysMap.keySet()) { - String systemKeys = sysName + ".keys"; - ctx.setAttribute(systemKeys, mapper.writeValueAsString(systemKeysMap.get(sysName))); + + for (Entry<String, List<String>> entry : systemKeysMap.entrySet()) { + String systemKeys = entry.getKey() + ".keys"; + ctx.setAttribute(systemKeys, mapper.writeValueAsString(entry.getValue())); } - /* - * String systemKeys = systemName+".keys"; ctx.setAttribute(systemKeys, - * mapper.writeValueAsString(externalSystemKeys)); - */ + } + } + private void resolveSourceParamValue(ObjectMapper mapper, Map<String, List<String>> systemKeysMap, + Parameter parameter, String source) throws JsonProcessingException { + if (systemKeysMap.containsKey(source)) { + log.info("Adding New System Key " + parameter.getName() + ":" + + mapper.writeValueAsString(parameter)); + List<String> l = systemKeysMap.get(source); + if (null != l) { + l.add(parameter.getName()); + systemKeysMap.put(source, l); + } + } else { + log.info("Creating/Adding New System Key " + parameter.getName() + ":" + + mapper.writeValueAsString(parameter)); + List<String> l = Lists.newArrayList(parameter.getName()); + systemKeysMap.put(source, l); } } + private String resolveSourceStr(Parameter parameter) { + String source = parameter.getSource(); + if (StringUtils.equalsIgnoreCase(source, "A&AI")) { + source = "AAI"; + } + source = StringUtils.upperCase(source); + return source; + } + + private boolean validateParameter(Map<String, String> requestParamMap, Parameter parameter) { + return parameter != null && !requestParamMap.containsKey(parameter.getName()) + && StringUtils.isNotBlank(parameter.getSource()) + && !StringUtils.equalsIgnoreCase(parameter.getSource(), "Manual"); + } - private String mergeJson(String requestParamJson, String systemParamJson) throws Exception { + private String mergeJson(String requestParamJson, String systemParamJson) throws IOException { ObjectMapper mapper = new ObjectMapper(); Map<String, String> requestParamMap = mapper.readValue(requestParamJson, HashMap.class); if (requestParamMap != null) { Map<String, String> systemParamMap = mapper.readValue(systemParamJson, HashMap.class); if (systemParamMap != null) { - for (String systemParamKey : systemParamMap.keySet()) { - log.trace("Megging System Key Values " + systemParamKey); - requestParamMap.put(systemParamKey, systemParamMap.get(systemParamKey)); + for (Entry<String, String> entry : systemParamMap.entrySet()) { + log.trace("Megging System Key Values " + entry.getKey()); + requestParamMap.put(entry.getKey(), entry.getValue()); } } - requestParamJson = mapper.writeValueAsString(requestParamMap); log.info("Processed Request Param " + requestParamJson); + return mapper.writeValueAsString(requestParamMap); } - return requestParamJson; } - public void validateParams(Map<String, String> inParams, SvcLogicContext ctx) - throws SvcLogicException - - { + throws SvcLogicException { String responsePrefix = inParams.get(ParamsHandlerConstant.INPUT_PARAM_RESPONSE_PRIFIX); String pdContent = inParams.get(ParamsHandlerConstant.INPUT_PARAM_PD_CONTENT); String configParams = - inParams.get(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER); + inParams.get(ParamsHandlerConstant.OUTPUT_PARAM_CONFIGURATION_PARAMETER); responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : ""; log.info("Processed pdContent Param " + pdContent); log.info("Processed config Param " + configParams); try { if (StringUtils.isBlank(pdContent)) { - throw new Exception("Request Param (pdContent) is Missing .."); + throw new MissingParameterException(STR_PD_CONTENT_MISSING); } if (StringUtils.isBlank(configParams)) { - throw new Exception("Request Param (configParams) is Missing .."); + throw new MissingParameterException("Request Param (configParams) is Missing .."); } PropertyDefinition propertyDefinition = parsePDContent(pdContent); ObjectMapper mapper = new ObjectMapper(); Map<String, String> requestParamMap = mapper.readValue(configParams, HashMap.class); List<Parameter> parameters = propertyDefinition.getParameters(); - Map<String, String> missingKeys = new HashMap<String, String>(); + Map<String, String> missingKeys = new HashMap<>(); for (Parameter parameter : parameters) { - if (parameter != null) { - if (parameter.isRequired()) { - if (!requestParamMap.containsKey(parameter.getName())) { - missingKeys.put(parameter.getName(), parameter.getSource()); - } else { - if ((requestParamMap.get(parameter.getName()) == null) - || (requestParamMap.get(parameter.getName()).equals(""))) - - { - missingKeys.put(parameter.getName(), parameter.getSource()); - } - - } - - } - + if (parameter != null && parameter.isRequired()) { + resolveParameterValue(requestParamMap, missingKeys, parameter); } - } - - if (missingKeys != null && missingKeys.size() > 0) { + if (missingKeys.size() > 0) { String requiredFields = mapper.writeValueAsString(missingKeys); log.info(" Below mentioned keys and respective source type are mandatory"); log.info(requiredFields); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE, - "Missing Mandatory Keys and source are" + requiredFields); + "Missing Mandatory Keys and source are" + requiredFields); throw new SvcLogicException( - " Missing Mandatory Keys and source are" + requiredFields); - - + " Missing Mandatory Keys and source are" + requiredFields); } else { log.info("success "); ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); - + ParamsHandlerConstant.OUTPUT_STATUS_SUCCESS); } } catch (Exception e) { - ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(ParamsHandlerConstant.OUTPUT_PARAM_STATUS, - ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); + ParamsHandlerConstant.OUTPUT_STATUS_FAILURE); ctx.setAttribute(responsePrefix + ParamsHandlerConstant.OUTPUT_PARAM_ERROR_MESSAGE, - e.getMessage()); - log.error(e.getMessage()); + e.getMessage()); + log.error("Failed to validate params", e); throw new SvcLogicException(e.getMessage()); } - } - - + private void resolveParameterValue(Map<String, String> requestParamMap, Map<String, String> missingKeys, + Parameter parameter) { + if (!requestParamMap.containsKey(parameter.getName())) { + missingKeys.put(parameter.getName(), parameter.getSource()); + } else { + if ((requestParamMap.get(parameter.getName()) == null) + || (requestParamMap.get(parameter.getName()).isEmpty())) { + missingKeys.put(parameter.getName(), parameter.getSource()); + } + } + } } |