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;
29 import com.google.gson.*;
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.getLogger(SvcLogicContext.class);
43 private HashMap<String, String> attributes;
45 private String status = SvcLogicConstants.SUCCESS;
47 public SvcLogicContext()
49 this.attributes = new HashMap<> ();
53 public SvcLogicContext(Properties props)
55 this.attributes = new HashMap<> ();
57 if (props.containsKey(CommonConstants.SERVICE_LOGIC_STATUS))
59 this.status = props.getProperty(CommonConstants.SERVICE_LOGIC_STATUS);
62 for (Object nameObj : props.keySet())
64 String propName = (String) nameObj;
65 attributes.put(propName, props.getProperty(propName));
69 public String getAttribute(String name)
71 if (attributes.containsKey(name))
73 return attributes.get(name);
81 public void setAttribute(String name, String value)
84 if (attributes.containsKey(name)) {
85 attributes.remove(name);
88 attributes.put(name, value);
92 public Set<String> getAttributeKeySet()
94 return attributes.keySet();
96 public Boolean isSuccess() {
97 return status.equals(SvcLogicConstants.SUCCESS);
101 public String getStatus() {
106 public void setStatus(String status) {
107 this.status = status;
110 public void markFailed() {
111 this.status = SvcLogicConstants.FAILURE;
114 public void markSuccess() {
115 this.status = SvcLogicConstants.SUCCESS;
118 public Properties toProperties()
120 Properties props = new Properties();
124 props.setProperty(CommonConstants.SERVICE_LOGIC_STATUS, status);
129 for (Map.Entry<String, String> entry : attributes.entrySet())
131 attrName = entry.getKey();
132 attrVal = entry.getValue();
133 if (attrVal == null) {
134 LOG.warn("attribute {} value is null - setting to empty string", attrName);
135 props.setProperty(attrName, "");
137 props.setProperty(attrName, attrVal);
144 public void mergeDocument(String pfx, Document doc) {
151 Element root = doc.getDocumentElement();
153 mergeElement(prefix, root, null);
156 public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {
158 // In XML, cannot tell the difference between containers and lists.
159 // So, have to treat each element as both (ugly but necessary).
160 // We do this by passing a nodeMap to be used to count instance of each tag,
161 // which will be used to set _length and to set index
163 LOG.trace("mergeElement({},{},{})", pfx, element.getTagName(), nodeMap);
165 String curTagName = element.getTagName();
166 String prefix = curTagName;
169 prefix = pfx + "." + prefix;
174 if (nodeMap != null) {
175 if (nodeMap.containsKey(curTagName)) {
176 myIdx = nodeMap.get(curTagName);
179 nodeMap.put(curTagName, myIdx+1);
180 this.setAttribute(prefix+"_length", Integer.toString(myIdx+1));
183 NodeList children = element.getChildNodes();
185 int numChildren = children.getLength();
187 Map<String, Integer> childMap = new HashMap<>();
188 Map<String, Integer> idxChildMap = new HashMap<>();
190 for (int i = 0 ; i < numChildren ; i++) {
191 Node curNode = children.item(i);
193 if (curNode instanceof Text) {
194 Text curText = (Text) curNode;
195 String curTextValue = curText.getTextContent();
196 LOG.trace("Setting ctx variable {} = {}", prefix, curTextValue);
197 this.setAttribute(prefix, curText.getTextContent());
200 } else if (curNode instanceof Element) {
201 mergeElement(prefix, (Element) curNode, childMap);
202 if (nodeMap != null) {
204 mergeElement(prefix+"["+myIdx+"]", (Element)curNode, idxChildMap);
212 public void mergeJson(String pfx, String jsonString) {
213 JsonParser jp = new JsonParser();
214 JsonElement element = jp.parse(jsonString);
216 mergeJsonObject(element.getAsJsonObject(), pfx+".");
219 public void mergeJsonObject(JsonObject jsonObject, String pfx) {
220 for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
221 if (entry.getValue().isJsonObject()) {
222 mergeJsonObject(entry.getValue().getAsJsonObject(), pfx + entry.getKey() + ".");
223 } else if (entry.getValue().isJsonArray()) {
224 JsonArray array = entry.getValue().getAsJsonArray();
225 this.setAttribute(pfx + entry.getKey() + "_length", String.valueOf(array.size()));
226 Integer arrayIdx = 0;
227 for (JsonElement element : array) {
228 if (element.isJsonObject()) {
229 mergeJsonObject(element.getAsJsonObject(), pfx + entry.getKey() + "[" + arrayIdx + "].");
234 if (entry.getValue() instanceof JsonNull) {
235 LOG.debug("Skipping parameter {} with null value",entry.getKey());
238 this.setAttribute(pfx + entry.getKey(), entry.getValue().getAsString());
244 public String resolve(String ctxVarName) {
246 if (ctxVarName.indexOf('[') == -1) {
247 // Ctx variable contains no arrays
248 return getAttribute(ctxVarName);
251 // Resolve any array references
252 StringBuilder sbuff = new StringBuilder();
253 String[] ctxVarParts = ctxVarName.split("\\[");
254 sbuff.append(ctxVarParts[0]);
255 for (int i = 1; i < ctxVarParts.length; i++) {
256 if (ctxVarParts[i].startsWith("$")) {
257 int endBracketLoc = ctxVarParts[i].indexOf(']');
258 if (endBracketLoc == -1) {
259 // Missing end bracket ... give up parsing
260 LOG.warn("Variable reference {} seems to be missing a ']'", ctxVarName);
261 return getAttribute(ctxVarName);
264 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
265 String remainder = ctxVarParts[i].substring(endBracketLoc);
268 sbuff.append(this.getAttribute(idxVarName));
269 sbuff.append(remainder);
272 // Index is not a variable reference
274 sbuff.append(ctxVarParts[i]);
278 return getAttribute(sbuff.toString());