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=========================================================
24 package org.onap.ccsdk.sli.core.sli;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.io.Serializable;
29 import java.util.HashMap;
30 import java.util.Iterator;
33 import java.util.TreeMap;
35 import org.apache.commons.lang3.StringEscapeUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.xml.sax.Locator;
41 public class SvcLogicNode implements Serializable {
43 private static final Logger LOG = LoggerFactory
44 .getLogger(SvcLogicExprListener.class);
46 private static final long serialVersionUID = 2L;
48 private String nodeName;
50 private String nodeType;
51 private boolean visited;
52 private SvcLogicGraph graph;
55 private HashMap<String, SvcLogicExpression> attributes;
56 private HashMap<String, SvcLogicNode> outcomes;
57 private HashMap<String, SvcLogicExpression> parameters;
59 public SvcLogicNode(int nodeId, String nodeType, SvcLogicGraph graph)
63 this.nodeType = nodeType;
65 attributes = new HashMap<String, SvcLogicExpression> ();
66 parameters = new HashMap<String, SvcLogicExpression> ();
71 public SvcLogicNode(int nodeId, String nodeType, String nodeName, SvcLogicGraph graph) throws DuplicateValueException
74 this.nodeName = nodeName;
75 this.nodeType = nodeType;
77 attributes = new HashMap<String, SvcLogicExpression> ();
78 parameters = new HashMap<String, SvcLogicExpression> ();
80 graph.setNamedNode(nodeName, this);
84 public int getNodeId()
89 public String getNodeName()
94 public String getNodeType()
99 public SvcLogicGraph getGraph()
104 public int getNumOutcomes()
106 if (outcomes == null)
112 return(outcomes.size());
116 public SvcLogicExpression getAttribute(String name)
118 if (attributes.containsKey(name))
120 return(attributes.get(name));
129 public void setAttribute(String name, String value) throws SvcLogicException
131 setAttribute(name, new SvcLogicAtom("STRING", value));
134 public void setAttribute(String name, SvcLogicExpression value) throws SvcLogicException
136 if (attributes.containsKey(name))
138 throw new DuplicateValueException("Duplicate attribute "+name);
141 attributes.put(name, value);
145 public void mapParameter(String name, String value) throws SvcLogicException
148 if (parameters.containsKey(name))
150 throw new DuplicateValueException("Duplicate parameter "+name);
154 SvcLogicExpression parmValue;
155 if ((value == null) || (value.length() == 0))
157 parmValue = new SvcLogicAtom("STRING", "");
159 else if (value.trim().startsWith("`"))
161 int lastParen = value.lastIndexOf("`");
162 String evalExpr = value.trim().substring(1, lastParen);
163 parmValue = SvcLogicExpressionFactory.parse(evalExpr);
168 if (Character.isDigit(value.charAt(0)))
170 parmValue = new SvcLogicAtom("NUMBER", value);
174 parmValue = new SvcLogicAtom("STRING", value);
177 LOG.debug("Setting parameter "+name+" = "+value+" = "+parmValue.asParsedExpr());
178 parameters.put(name, parmValue);
180 catch (IOException e) {
182 LOG.error("Invalid parameter value expression ("+value+")");
183 throw new SvcLogicException(e.getMessage());
187 public SvcLogicExpression getParameter(String name)
189 if (parameters.containsKey(name))
191 return(parameters.get(name));
199 public boolean isVisited() {
203 public void setVisited(boolean visited, boolean recursive) {
204 this.visited = visited;
208 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = getOutcomeSet();
210 if (outcomeSet == null)
215 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
217 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
218 SvcLogicNode outNode = curOutcome.getValue();
219 outNode.setVisited(visited, recursive);
224 public void addOutcome(String outcomeValue, SvcLogicNode node) throws SvcLogicException
226 if (outcomes == null)
228 outcomes = new HashMap<String, SvcLogicNode>();
231 if (outcomeValue.length() == 0) {
232 outcomeValue = "\"\"";
234 if (outcomes.containsKey(outcomeValue))
236 throw new DuplicateValueException("Duplicate outcome value "+outcomeValue);
239 outcomes.put(outcomeValue, node);
242 public Set<Map.Entry<String, SvcLogicNode>> getOutcomeSet()
244 if (outcomes == null)
249 return(outcomes.entrySet());
253 public Set<Map.Entry<String, SvcLogicExpression>> getParameterSet()
255 if (parameters == null)
260 return(parameters.entrySet());
264 public void printAsGv(PrintStream pstr)
276 StringBuffer sbuff = new StringBuffer();
278 sbuff.append("node");
279 sbuff.append(nodeId);
280 sbuff.append(" [ shape=none, margin=0, label=<<table border=\"0\" cellborder=\"1\" align=\"left\">");
281 sbuff.append("<tr><td colspan=\"2\"><b>");
282 sbuff.append(nodeId);
284 sbuff.append(nodeType);
285 sbuff.append("</b></td></tr><th><td><i>Attribute</i></td><td><i>Value</i></td></th>");
287 if (nodeName.length() > 0)
289 sbuff.append("<tr><td>name</td><td>");
290 sbuff.append(nodeName);
291 sbuff.append("</td></tr>");
294 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
295 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
297 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
298 sbuff.append("<tr><td>");
299 sbuff.append(curAttr.getKey());
300 sbuff.append("</td><td>");
301 sbuff.append(StringEscapeUtils.escapeHtml3(curAttr.getValue().toString()));
302 sbuff.append("</td></tr>");
304 sbuff.append("</table>>];");
306 pstr.println(sbuff.toString());
309 if (outcomes != null)
311 TreeMap<String, SvcLogicNode> sortedOutcomes = new TreeMap<String, SvcLogicNode>(outcomes);
312 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = sortedOutcomes.entrySet();
314 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
316 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
317 String outValue = curOutcome.getKey();
318 SvcLogicNode outNode = curOutcome.getValue();
319 pstr.println("node"+nodeId+" -> node"+outNode.getNodeId()+" [label=\""+outValue+"\"];");
320 outNode.printAsGv(pstr);
325 public void printAsXml(PrintStream pstr, int indentLvl)
332 for (int i = 0 ; i < indentLvl ; i++)
337 pstr.print(this.getNodeType());
339 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
340 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
342 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
344 pstr.print(curAttr.getKey());
346 pstr.print(curAttr.getValue());
350 if (((parameters == null) || (parameters.isEmpty())) &&
351 ((outcomes == null) || outcomes.isEmpty()))
362 // Print parameters (if any)
363 if (parameters != null)
365 Set<Map.Entry<String, SvcLogicExpression>> paramSet = parameters.entrySet();
366 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = paramSet.iterator() ; iter.hasNext();)
368 for (int i = 0 ; i < indentLvl+1 ; i++)
372 pstr.print("<parameter");
373 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
374 pstr.print(" name='");
375 pstr.print(curAttr.getKey());
376 pstr.print("' value='`");
377 pstr.print(curAttr.getValue().toString());
378 pstr.print("`'/>\n");
382 // Print outcomes (if any)
383 if (outcomes != null)
385 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = outcomes.entrySet();
386 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator() ; iter.hasNext();)
388 for (int i = 0 ; i < indentLvl+1 ; i++)
392 pstr.print("<outcome");
393 Map.Entry<String, SvcLogicNode> curAttr = iter.next();
394 pstr.print(" value='");
395 pstr.print(curAttr.getKey());
397 SvcLogicNode outNode = curAttr.getValue();
398 outNode.printAsXml(pstr, indentLvl+2);
399 for (int i = 0 ; i < indentLvl+1 ; i++)
403 pstr.print("</outcome>\n");
407 // Print node end tag
408 for (int i = 0 ; i < indentLvl ; i++)
413 pstr.print(this.getNodeType());
420 public SvcLogicNode getOutcomeValue(String value)
423 if (value.length() == 0) {
426 if (outcomes == null)
431 if (outcomes.containsKey(value))
433 return(outcomes.get(value));
437 StringBuffer keyBuffer = new StringBuffer();
438 keyBuffer.append("{");
439 for (String key : outcomes.keySet()) {
440 keyBuffer.append(" ("+key+")");
442 keyBuffer.append("}");
443 LOG.info("Outcome (" + value + ") not found, keys are " + keyBuffer.toString());
445 if (outcomes.containsKey("Other"))
447 return(outcomes.get("Other"));