2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.core.sli;
23 import java.util.HashMap;
25 import java.util.Properties;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35 import org.w3c.dom.Text;
38 public class SvcLogicContext {
40 private static final Logger LOG = LoggerFactory
41 .getLogger(SvcLogicContext.class);
43 private HashMap<String, String> attributes;
45 private DOMDataBroker domDataBroker;
47 private String status = "success";
49 public SvcLogicContext()
51 this.attributes = new HashMap<String,String> ();
55 public SvcLogicContext(Properties props)
57 this.attributes = new HashMap<String, String> ();
59 if (props.containsKey("SvcLogic.status"))
61 this.status = props.getProperty("SvcLogic.status");
64 for (Object nameObj : props.keySet())
66 String propName = (String) nameObj;
67 attributes.put(propName, props.getProperty(propName));
73 public DOMDataBroker getDomDataBroker() {
77 public void setDomDataBroker(DOMDataBroker domDataBroker) {
78 this.domDataBroker = domDataBroker;
81 public String getAttribute(String name)
83 if (attributes.containsKey(name))
85 return(attributes.get(name));
93 public void setAttribute(String name, String value)
96 if (attributes.containsKey(name)) {
97 attributes.remove(name);
100 attributes.put(name, value);
104 public Set<String> getAttributeKeySet()
106 return(attributes.keySet());
109 public String getStatus() {
113 public void setStatus(String status) {
114 this.status = status;
117 public Properties toProperties()
119 Properties props = new Properties();
123 props.setProperty("SvcLogic.status", status);
126 for (String attrName : attributes.keySet())
128 String attrVal = attributes.get(attrName);
129 if (attrVal == null) {
130 LOG.warn("attribute " + attrName
131 + "null - setting to empty string");
132 props.setProperty(attrName, "");
134 props.setProperty(attrName, attributes.get(attrName));
141 public void mergeDocument(String pfx, Document doc) {
148 Element root = doc.getDocumentElement();
150 mergeElement(prefix, root, null);
153 public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
155 // In XML, cannot tell the difference between containers and lists.
156 // So, have to treat each element as both (ugly but necessary).
157 // We do this by passing a nodeMap to be used to count instance of each tag,
158 // which will be used to set _length and to set index
160 LOG.trace("mergeElement("+pfx+","+element.getTagName()+","+nodeMap+")");
162 String curTagName = element.getTagName();
163 String prefix = curTagName;
166 prefix = pfx + "." + prefix;
171 if (nodeMap != null) {
172 if (nodeMap.containsKey(curTagName)) {
173 myIdx = nodeMap.get(curTagName).intValue();
176 nodeMap.put(curTagName, new Integer(myIdx+1));
177 this.setAttribute(prefix+"_length", ""+(myIdx+1));
180 NodeList children = element.getChildNodes();
182 int numChildren = children.getLength();
184 Map<String, Integer> childMap = new HashMap<String, Integer>();
185 Map<String, Integer> idxChildMap = new HashMap<String, Integer>();
187 for (int i = 0 ; i < numChildren ; i++) {
188 Node curNode = children.item(i);
190 if (curNode instanceof Text) {
191 Text curText = (Text) curNode;
192 String curTextValue = curText.getTextContent();
193 LOG.trace("Setting ctx variable "+prefix+" = "+curTextValue);
194 this.setAttribute(prefix, curText.getTextContent());
197 } else if (curNode instanceof Element) {
198 mergeElement(prefix, (Element) curNode, childMap);
199 if (nodeMap != null) {
201 mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);
209 public String resolve(String ctxVarName) {
211 if (ctxVarName.indexOf('[') == -1) {
212 // Ctx variable contains no arrays
213 return (this.getAttribute(ctxVarName));
216 // Resolve any array references
217 StringBuffer sbuff = new StringBuffer();
218 String[] ctxVarParts = ctxVarName.split("\\[");
219 sbuff.append(ctxVarParts[0]);
220 for (int i = 1; i < ctxVarParts.length; i++) {
221 if (ctxVarParts[i].startsWith("$")) {
222 int endBracketLoc = ctxVarParts[i].indexOf("]");
223 if (endBracketLoc == -1) {
224 // Missing end bracket ... give up parsing
225 LOG.warn("Variable reference " + ctxVarName
226 + " seems to be missing a ']'");
227 return (this.getAttribute(ctxVarName));
230 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
231 String remainder = ctxVarParts[i].substring(endBracketLoc);
234 sbuff.append(this.getAttribute(idxVarName));
235 sbuff.append(remainder);
238 // Index is not a variable reference
240 sbuff.append(ctxVarParts[i]);
244 return (this.getAttribute(sbuff.toString()));