2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdnc.sli;
24 import java.util.HashMap;
26 import java.util.Properties;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 import org.w3c.dom.Text;
39 public class SvcLogicContext {
41 private static final Logger LOG = LoggerFactory
42 .getLogger(SvcLogicContext.class);
44 private HashMap<String, String> attributes;
46 private DOMDataBroker domDataBroker;
48 private String status = "success";
50 public SvcLogicContext()
52 this.attributes = new HashMap<String,String> ();
56 public SvcLogicContext(Properties props)
58 this.attributes = new HashMap<String, String> ();
60 if (props.containsKey("SvcLogic.status"))
62 this.status = props.getProperty("SvcLogic.status");
65 for (Object nameObj : props.keySet())
67 String propName = (String) nameObj;
68 attributes.put(propName, props.getProperty(propName));
74 public DOMDataBroker getDomDataBroker() {
78 public void setDomDataBroker(DOMDataBroker domDataBroker) {
79 this.domDataBroker = domDataBroker;
82 public String getAttribute(String name)
84 if (attributes.containsKey(name))
86 return(attributes.get(name));
94 public void setAttribute(String name, String value)
97 if (attributes.containsKey(name)) {
98 attributes.remove(name);
101 attributes.put(name, value);
105 public Set<String> getAttributeKeySet()
107 return(attributes.keySet());
110 public String getStatus() {
114 public void setStatus(String status) {
115 this.status = status;
118 public Properties toProperties()
120 Properties props = new Properties();
124 props.setProperty("SvcLogic.status", status);
127 for (String attrName : attributes.keySet())
129 String attrVal = attributes.get(attrName);
130 if (attrVal == null) {
131 LOG.warn("attribute " + attrName
132 + "null - setting to empty string");
133 props.setProperty(attrName, "");
135 props.setProperty(attrName, attributes.get(attrName));
142 public void mergeDocument(String pfx, Document doc) {
149 Element root = doc.getDocumentElement();
151 mergeElement(prefix, root, null);
154 public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
156 // In XML, cannot tell the difference between containers and lists.
157 // So, have to treat each element as both (ugly but necessary).
158 // We do this by passing a nodeMap to be used to count instance of each tag,
159 // which will be used to set _length and to set index
161 LOG.trace("mergeElement("+pfx+","+element.getTagName()+","+nodeMap+")");
163 String curTagName = element.getTagName();
164 String prefix = curTagName;
167 prefix = pfx + "." + prefix;
172 if (nodeMap != null) {
173 if (nodeMap.containsKey(curTagName)) {
174 myIdx = nodeMap.get(curTagName).intValue();
177 nodeMap.put(curTagName, new Integer(myIdx+1));
178 this.setAttribute(prefix+"_length", ""+(myIdx+1));
181 NodeList children = element.getChildNodes();
183 int numChildren = children.getLength();
185 Map<String, Integer> childMap = new HashMap<String, Integer>();
186 Map<String, Integer> idxChildMap = new HashMap<String, Integer>();
188 for (int i = 0 ; i < numChildren ; i++) {
189 Node curNode = children.item(i);
191 if (curNode instanceof Text) {
192 Text curText = (Text) curNode;
193 String curTextValue = curText.getTextContent();
194 LOG.trace("Setting ctx variable "+prefix+" = "+curTextValue);
195 this.setAttribute(prefix, curText.getTextContent());
198 } else if (curNode instanceof Element) {
199 mergeElement(prefix, (Element) curNode, childMap);
200 if (nodeMap != null) {
202 mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);
210 public String resolve(String ctxVarName) {
212 if (ctxVarName.indexOf('[') == -1) {
213 // Ctx variable contains no arrays
214 return (this.getAttribute(ctxVarName));
217 // Resolve any array references
218 StringBuffer sbuff = new StringBuffer();
219 String[] ctxVarParts = ctxVarName.split("\\[");
220 sbuff.append(ctxVarParts[0]);
221 for (int i = 1; i < ctxVarParts.length; i++) {
222 if (ctxVarParts[i].startsWith("$")) {
223 int endBracketLoc = ctxVarParts[i].indexOf("]");
224 if (endBracketLoc == -1) {
225 // Missing end bracket ... give up parsing
226 LOG.warn("Variable reference " + ctxVarName
227 + " seems to be missing a ']'");
228 return (this.getAttribute(ctxVarName));
231 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
232 String remainder = ctxVarParts[i].substring(endBracketLoc);
235 sbuff.append(this.getAttribute(idxVarName));
236 sbuff.append(remainder);
239 // Index is not a variable reference
241 sbuff.append(ctxVarParts[i]);
245 return (this.getAttribute(sbuff.toString()));