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.onap.ccsdk.sli.core.sli;
24 import java.util.HashMap;
26 import java.util.Properties;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 import org.w3c.dom.Text;
37 public class SvcLogicContext {
39 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicContext.class);
41 private HashMap<String, String> attributes;
43 private String status = "success";
45 public SvcLogicContext()
47 this.attributes = new HashMap<> ();
51 public SvcLogicContext(Properties props)
53 this.attributes = new HashMap<> ();
55 if (props.containsKey(CommonConstants.SERVICE_LOGIC_STATUS))
57 this.status = props.getProperty(CommonConstants.SERVICE_LOGIC_STATUS);
60 for (Object nameObj : props.keySet())
62 String propName = (String) nameObj;
63 attributes.put(propName, props.getProperty(propName));
67 public String getAttribute(String name)
69 if (attributes.containsKey(name))
71 return attributes.get(name);
79 public void setAttribute(String name, String value)
82 if (attributes.containsKey(name)) {
83 attributes.remove(name);
86 attributes.put(name, value);
90 public Set<String> getAttributeKeySet()
92 return attributes.keySet();
95 public String getStatus() {
99 public void setStatus(String status) {
100 this.status = status;
103 public Properties toProperties()
105 Properties props = new Properties();
109 props.setProperty(CommonConstants.SERVICE_LOGIC_STATUS, status);
114 for (Map.Entry<String, String> entry : attributes.entrySet())
116 attrName = entry.getKey();
117 attrVal = entry.getValue();
118 if (attrVal == null) {
119 LOG.warn("attribute {} value is null - setting to empty string", attrName);
120 props.setProperty(attrName, "");
122 props.setProperty(attrName, attrVal);
129 public void mergeDocument(String pfx, Document doc) {
136 Element root = doc.getDocumentElement();
138 mergeElement(prefix, root, null);
141 public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
143 // In XML, cannot tell the difference between containers and lists.
144 // So, have to treat each element as both (ugly but necessary).
145 // We do this by passing a nodeMap to be used to count instance of each tag,
146 // which will be used to set _length and to set index
148 LOG.trace("mergeElement({},{},{})", pfx, element.getTagName(), nodeMap);
150 String curTagName = element.getTagName();
151 String prefix = curTagName;
154 prefix = pfx + "." + prefix;
159 if (nodeMap != null) {
160 if (nodeMap.containsKey(curTagName)) {
161 myIdx = nodeMap.get(curTagName);
164 nodeMap.put(curTagName, myIdx+1);
165 this.setAttribute(prefix+"_length", Integer.toString(myIdx+1));
168 NodeList children = element.getChildNodes();
170 int numChildren = children.getLength();
172 Map<String, Integer> childMap = new HashMap<>();
173 Map<String, Integer> idxChildMap = new HashMap<>();
175 for (int i = 0 ; i < numChildren ; i++) {
176 Node curNode = children.item(i);
178 if (curNode instanceof Text) {
179 Text curText = (Text) curNode;
180 String curTextValue = curText.getTextContent();
181 LOG.trace("Setting ctx variable {} = {}", prefix, curTextValue);
182 this.setAttribute(prefix, curText.getTextContent());
185 } else if (curNode instanceof Element) {
186 mergeElement(prefix, (Element) curNode, childMap);
187 if (nodeMap != null) {
189 mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);
197 public String resolve(String ctxVarName) {
199 if (ctxVarName.indexOf('[') == -1) {
200 // Ctx variable contains no arrays
201 return getAttribute(ctxVarName);
204 // Resolve any array references
205 StringBuilder sbuff = new StringBuilder();
206 String[] ctxVarParts = ctxVarName.split("\\[");
207 sbuff.append(ctxVarParts[0]);
208 for (int i = 1; i < ctxVarParts.length; i++) {
209 if (ctxVarParts[i].startsWith("$")) {
210 int endBracketLoc = ctxVarParts[i].indexOf(']');
211 if (endBracketLoc == -1) {
212 // Missing end bracket ... give up parsing
213 LOG.warn("Variable reference {} seems to be missing a ']'", ctxVarName);
214 return getAttribute(ctxVarName);
217 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
218 String remainder = ctxVarParts[i].substring(endBracketLoc);
221 sbuff.append(this.getAttribute(idxVarName));
222 sbuff.append(remainder);
225 // Index is not a variable reference
227 sbuff.append(ctxVarParts[i]);
231 return getAttribute(sbuff.toString());