2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (C) 2018 IBM.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
27 package org.onap.ccsdk.sli.core.sli;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.io.Serializable;
32 import java.util.HashMap;
33 import java.util.Iterator;
36 import java.util.HashSet;
37 import java.util.TreeMap;
39 import org.apache.commons.text.StringEscapeUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class SvcLogicNode implements Serializable {
45 private static final Logger LOG = LoggerFactory
46 .getLogger(SvcLogicExprListener.class);
48 private static final long serialVersionUID = 2L;
50 private String nodeName;
52 private String nodeType;
53 private boolean visited;
54 private SvcLogicGraph graph;
57 private HashMap<String, SvcLogicExpression> attributes;
58 private HashMap<String, SvcLogicNode> outcomes;
59 private HashMap<String, SvcLogicExpression> parameters;
61 public SvcLogicNode(int nodeId, String nodeType, SvcLogicGraph graph)
65 this.nodeType = nodeType;
67 attributes = new HashMap<> ();
68 parameters = new HashMap<> ();
73 public SvcLogicNode(int nodeId, String nodeType, String nodeName, SvcLogicGraph graph) throws DuplicateValueException
76 this.nodeName = nodeName;
77 this.nodeType = nodeType;
79 attributes = new HashMap<> ();
80 parameters = new HashMap<> ();
82 graph.setNamedNode(nodeName, this);
86 public int getNodeId()
91 public String getNodeName()
96 public String getNodeType()
101 public SvcLogicGraph getGraph()
106 public int getNumOutcomes()
108 if (outcomes == null)
114 return outcomes.size();
118 public SvcLogicExpression getAttribute(String name)
120 if (attributes.containsKey(name))
122 return attributes.get(name);
131 public void setAttribute(String name, String value) throws SvcLogicException
133 setAttribute(name, new SvcLogicAtom("STRING", value));
136 public void setAttribute(String name, SvcLogicExpression value) throws SvcLogicException
138 if (attributes.containsKey(name))
140 throw new DuplicateValueException("Duplicate attribute "+name);
143 attributes.put(name, value);
147 public void mapParameter(String name, String value) throws SvcLogicException
150 if (parameters.containsKey(name))
152 throw new DuplicateValueException("Duplicate parameter "+name);
156 SvcLogicExpression parmValue;
157 if (value == null || value.length() == 0)
159 parmValue = new SvcLogicAtom("STRING", "");
161 else if (value.trim().startsWith("`"))
163 int lastParen = value.lastIndexOf("`");
164 String evalExpr = value.trim().substring(1, lastParen);
165 parmValue = SvcLogicExpressionFactory.parse(evalExpr);
170 if (Character.isDigit(value.charAt(0)))
172 parmValue = new SvcLogicAtom("NUMBER", value);
176 parmValue = new SvcLogicAtom("STRING", value);
179 LOG.debug("Setting parameter "+name+" = "+value+" = "+parmValue.asParsedExpr());
180 parameters.put(name, parmValue);
182 catch (IOException e) {
184 LOG.error("Invalid parameter value expression ("+value+")");
185 throw new SvcLogicException(e.getMessage());
189 public SvcLogicExpression getParameter(String name)
191 if (parameters.containsKey(name))
193 return parameters.get(name);
201 public boolean isVisited() {
205 public void setVisited(boolean visited, boolean recursive) {
206 this.visited = visited;
210 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = getOutcomeSet();
212 if (outcomeSet == null)
217 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
219 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
220 SvcLogicNode outNode = curOutcome.getValue();
221 outNode.setVisited(visited, recursive);
226 public void addOutcome(String outcomeValue, SvcLogicNode node) throws SvcLogicException
228 if (outcomes == null)
230 outcomes = new HashMap<>();
233 if (outcomeValue.length() == 0) {
234 outcomeValue = "\"\"";
236 if (outcomes.containsKey(outcomeValue))
238 throw new DuplicateValueException("Duplicate outcome value "+outcomeValue);
241 outcomes.put(outcomeValue, node);
244 public Set<Map.Entry<String, SvcLogicNode>> getOutcomeSet()
246 if (outcomes == null)
248 return new HashSet<>();
251 return outcomes.entrySet();
255 public Set<Map.Entry<String, SvcLogicExpression>> getParameterSet()
257 if (parameters == null)
259 return new HashSet<>();
262 return parameters.entrySet();
266 public void printAsGv(PrintStream pstr)
278 StringBuffer sbuff = new StringBuffer();
280 sbuff.append("node");
281 sbuff.append(nodeId);
282 sbuff.append(" [ shape=none, margin=0, label=<<table border=\"0\" cellborder=\"1\" align=\"left\">");
283 sbuff.append("<tr><td colspan=\"2\"><b>");
284 sbuff.append(nodeId);
286 sbuff.append(nodeType);
287 sbuff.append("</b></td></tr><th><td><i>Attribute</i></td><td><i>Value</i></td></th>");
289 if (nodeName.length() > 0)
291 sbuff.append("<tr><td>name</td><td>");
292 sbuff.append(nodeName);
293 sbuff.append("</td></tr>");
296 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
297 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
299 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
300 sbuff.append("<tr><td>");
301 sbuff.append(curAttr.getKey());
302 sbuff.append("</td><td>");
303 sbuff.append(StringEscapeUtils.escapeHtml3(curAttr.getValue().toString()));
304 sbuff.append("</td></tr>");
306 sbuff.append("</table>>];");
308 pstr.println(sbuff.toString());
311 if (outcomes != null)
313 TreeMap<String, SvcLogicNode> sortedOutcomes = new TreeMap<>(outcomes);
314 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = sortedOutcomes.entrySet();
316 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
318 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
319 String outValue = curOutcome.getKey();
320 SvcLogicNode outNode = curOutcome.getValue();
321 pstr.println("node"+nodeId+" -> node"+outNode.getNodeId()+" [label=\""+outValue+"\"];");
322 outNode.printAsGv(pstr);
327 public void printAsXml(PrintStream pstr, int indentLvl)
334 for (int i = 0 ; i < indentLvl ; i++)
339 pstr.print(this.getNodeType());
341 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
342 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
344 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
346 pstr.print(curAttr.getKey());
348 pstr.print(curAttr.getValue());
352 if ((parameters == null || parameters.isEmpty()) &&
353 (outcomes == null || outcomes.isEmpty()))
364 // Print parameters (if any)
365 if (parameters != null)
367 Set<Map.Entry<String, SvcLogicExpression>> paramSet = parameters.entrySet();
368 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = paramSet.iterator() ; iter.hasNext();)
370 for (int i = 0 ; i < indentLvl+1 ; i++)
374 pstr.print("<parameter");
375 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
376 pstr.print(" name='");
377 pstr.print(curAttr.getKey());
378 pstr.print("' value='`");
379 pstr.print(curAttr.getValue().toString());
380 pstr.print("`'/>\n");
384 // Print outcomes (if any)
385 if (outcomes != null)
387 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = outcomes.entrySet();
388 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator() ; iter.hasNext();)
390 for (int i = 0 ; i < indentLvl+1 ; i++)
394 pstr.print("<outcome");
395 Map.Entry<String, SvcLogicNode> curAttr = iter.next();
396 pstr.print(" value='");
397 pstr.print(curAttr.getKey());
399 SvcLogicNode outNode = curAttr.getValue();
400 outNode.printAsXml(pstr, indentLvl+2);
401 for (int i = 0 ; i < indentLvl+1 ; i++)
405 pstr.print("</outcome>\n");
409 // Print node end tag
410 for (int i = 0 ; i < indentLvl ; i++)
415 pstr.print(this.getNodeType());
422 public SvcLogicNode getOutcomeValue(String value)
425 if (value.length() == 0) {
428 if (outcomes == null)
433 if (outcomes.containsKey(value))
435 return outcomes.get(value);
439 StringBuffer keyBuffer = new StringBuffer();
440 keyBuffer.append("{");
441 for (String key : outcomes.keySet()) {
442 keyBuffer.append(" ("+key+")");
444 keyBuffer.append("}");
445 LOG.info("Outcome (" + value + ") not found, keys are " + keyBuffer.toString());
447 if (outcomes.containsKey("Other"))
449 return outcomes.get("Other");