diff options
author | Dan Timoney <dtimoney@att.com> | 2017-09-14 18:14:59 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2017-09-14 18:14:59 +0000 |
commit | b26263ebcd02862aee41677eb22cbaf954fa97fd (patch) | |
tree | ac187961aab402a5d23f252ae4c35448403b5b8e | |
parent | 2a63bbef730e9d334a1b45bbebc40c0854b143d6 (diff) | |
parent | 207b29089b970e9ab2e8cd7d65a25cadc9c23074 (diff) |
Merge "Fix few sonar issues"
-rw-r--r-- | sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/CommonConstants.java | 27 | ||||
-rw-r--r-- | sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java | 132 |
2 files changed, 93 insertions, 66 deletions
diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/CommonConstants.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/CommonConstants.java new file mode 100644 index 000000000..d30b2b626 --- /dev/null +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/CommonConstants.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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.ccsdk.sli.core.sli; + +public interface CommonConstants { + + public static final String SERVICE_LOGIC_STATUS = "SvcLogic.status"; +} diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java index eaf57428f..232774396 100644 --- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java @@ -8,9 +8,9 @@ * 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. @@ -38,39 +38,38 @@ import org.w3c.dom.Text; public class SvcLogicContext { - private static final Logger LOG = LoggerFactory - .getLogger(SvcLogicContext.class); - + private static final Logger LOG = LoggerFactory.getLogger(SvcLogicContext.class); + private HashMap<String, String> attributes; - + private DOMDataBroker domDataBroker; - + private String status = "success"; - + public SvcLogicContext() { - this.attributes = new HashMap<String,String> (); - + this.attributes = new HashMap<> (); + } - + public SvcLogicContext(Properties props) { - this.attributes = new HashMap<String, String> (); - - if (props.containsKey("SvcLogic.status")) + this.attributes = new HashMap<> (); + + if (props.containsKey(CommonConstants.SERVICE_LOGIC_STATUS)) { - this.status = props.getProperty("SvcLogic.status"); + this.status = props.getProperty(CommonConstants.SERVICE_LOGIC_STATUS); } - + for (Object nameObj : props.keySet()) { String propName = (String) nameObj; attributes.put(propName, props.getProperty(propName)); } } - - - + + + public DOMDataBroker getDomDataBroker() { return domDataBroker; } @@ -83,14 +82,14 @@ public class SvcLogicContext { { if (attributes.containsKey(name)) { - return(attributes.get(name)); + return attributes.get(name); } else { - return(null); + return null; } } - + public void setAttribute(String name, String value) { if (value == null) { @@ -101,10 +100,10 @@ public class SvcLogicContext { attributes.put(name, value); } } - + public Set<String> getAttributeKeySet() { - return(attributes.keySet()); + return attributes.keySet(); } public String getStatus() { @@ -114,86 +113,88 @@ public class SvcLogicContext { public void setStatus(String status) { this.status = status; } - + public Properties toProperties() { Properties props = new Properties(); - + if (status != null) { - props.setProperty("SvcLogic.status", status); + props.setProperty(CommonConstants.SERVICE_LOGIC_STATUS, status); } - - for (String attrName : attributes.keySet()) + + String attrName; + String attrVal; + for (Map.Entry<String, String> entry : attributes.entrySet()) { - String attrVal = attributes.get(attrName); + attrName = entry.getKey(); + attrVal = entry.getValue(); if (attrVal == null) { - LOG.warn("attribute " + attrName - + "null - setting to empty string"); + LOG.warn("attribute {} value is null - setting to empty string", attrName); props.setProperty(attrName, ""); } else { - props.setProperty(attrName, attributes.get(attrName)); + props.setProperty(attrName, attrVal); } } - - return(props); + + return props; } - + public void mergeDocument(String pfx, Document doc) { String prefix = ""; - + if (pfx != null) { prefix = pfx; } - + Element root = doc.getDocumentElement(); - + mergeElement(prefix, root, null); } - + public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) { - + // In XML, cannot tell the difference between containers and lists. // So, have to treat each element as both (ugly but necessary). - // We do this by passing a nodeMap to be used to count instance of each tag, - // which will be used to set _length and to set index - - LOG.trace("mergeElement("+pfx+","+element.getTagName()+","+nodeMap+")"); + // We do this by passing a nodeMap to be used to count instance of each tag, + // which will be used to set _length and to set index + + LOG.trace("mergeElement({},{},{})", pfx, element.getTagName(), nodeMap); String curTagName = element.getTagName(); String prefix = curTagName; - + if (pfx != null) { prefix = pfx + "." + prefix; } - + int myIdx = 0; - + if (nodeMap != null) { if (nodeMap.containsKey(curTagName)) { - myIdx = nodeMap.get(curTagName).intValue(); + myIdx = nodeMap.get(curTagName); } - nodeMap.put(curTagName, new Integer(myIdx+1)); - this.setAttribute(prefix+"_length", ""+(myIdx+1)); + nodeMap.put(curTagName, myIdx+1); + this.setAttribute(prefix+"_length", Integer.toString(myIdx+1)); } - + NodeList children = element.getChildNodes(); - + int numChildren = children.getLength(); - - Map<String, Integer> childMap = new HashMap<String, Integer>(); - Map<String, Integer> idxChildMap = new HashMap<String, Integer>(); - + + Map<String, Integer> childMap = new HashMap<>(); + Map<String, Integer> idxChildMap = new HashMap<>(); + for (int i = 0 ; i < numChildren ; i++) { Node curNode = children.item(i); - + if (curNode instanceof Text) { Text curText = (Text) curNode; String curTextValue = curText.getTextContent(); - LOG.trace("Setting ctx variable "+prefix+" = "+curTextValue); + LOG.trace("Setting ctx variable {} = {}", prefix, curTextValue); this.setAttribute(prefix, curText.getTextContent()); - + } else if (curNode instanceof Element) { mergeElement(prefix, (Element) curNode, childMap); @@ -204,9 +205,9 @@ public class SvcLogicContext { } } } - + } - + public String resolve(String ctxVarName) { if (ctxVarName.indexOf('[') == -1) { @@ -215,16 +216,15 @@ public class SvcLogicContext { } // Resolve any array references - StringBuffer sbuff = new StringBuffer(); + StringBuilder sbuff = new StringBuilder(); String[] ctxVarParts = ctxVarName.split("\\["); sbuff.append(ctxVarParts[0]); for (int i = 1; i < ctxVarParts.length; i++) { if (ctxVarParts[i].startsWith("$")) { - int endBracketLoc = ctxVarParts[i].indexOf("]"); + int endBracketLoc = ctxVarParts[i].indexOf(']'); if (endBracketLoc == -1) { // Missing end bracket ... give up parsing - LOG.warn("Variable reference " + ctxVarName - + " seems to be missing a ']'"); + LOG.warn("Variable reference {} seems to be missing a ']'", ctxVarName); return (this.getAttribute(ctxVarName)); } |