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=========================================================
25 package org.openecomp.sdnc.sli;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.io.Serializable;
30 import java.util.HashMap;
31 import java.util.Iterator;
34 import java.util.TreeMap;
36 import org.apache.commons.lang3.StringEscapeUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.xml.sax.Locator;
42 public class SvcLogicNode implements Serializable {
44 private static final Logger LOG = LoggerFactory
45 .getLogger(SvcLogicExprListener.class);
47 private static final long serialVersionUID = 2L;
49 private String nodeName;
51 private String nodeType;
52 private boolean visited;
53 private SvcLogicGraph graph;
56 private HashMap<String, SvcLogicExpression> attributes;
57 private HashMap<String, SvcLogicNode> outcomes;
58 private HashMap<String, SvcLogicExpression> parameters;
60 public SvcLogicNode(int nodeId, String nodeType, SvcLogicGraph graph)
64 this.nodeType = nodeType;
66 attributes = new HashMap<String, SvcLogicExpression> ();
67 parameters = new HashMap<String, SvcLogicExpression> ();
72 public SvcLogicNode(int nodeId, String nodeType, String nodeName, SvcLogicGraph graph) throws DuplicateValueException
75 this.nodeName = nodeName;
76 this.nodeType = nodeType;
78 attributes = new HashMap<String, SvcLogicExpression> ();
79 parameters = new HashMap<String, SvcLogicExpression> ();
81 graph.setNamedNode(nodeName, this);
85 public int getNodeId()
90 public String getNodeName()
95 public String getNodeType()
100 public SvcLogicGraph getGraph()
105 public int getNumOutcomes()
107 if (outcomes == null)
113 return(outcomes.size());
117 public SvcLogicExpression getAttribute(String name)
119 if (attributes.containsKey(name))
121 return(attributes.get(name));
130 public void setAttribute(String name, String value) throws SvcLogicException
132 setAttribute(name, new SvcLogicAtom("STRING", value));
135 public void setAttribute(String name, SvcLogicExpression value) throws SvcLogicException
137 if (attributes.containsKey(name))
139 throw new DuplicateValueException("Duplicate attribute "+name);
142 attributes.put(name, value);
146 public void mapParameter(String name, String value) throws SvcLogicException
149 if (parameters.containsKey(name))
151 throw new DuplicateValueException("Duplicate parameter "+name);
155 SvcLogicExpression parmValue;
156 if ((value == null) || (value.length() == 0))
158 parmValue = new SvcLogicAtom("STRING", "");
160 else if (value.trim().startsWith("`"))
162 int lastParen = value.lastIndexOf("`");
163 String evalExpr = value.trim().substring(1, lastParen);
164 parmValue = SvcLogicExpressionFactory.parse(evalExpr);
169 if (Character.isDigit(value.charAt(0)))
171 parmValue = new SvcLogicAtom("NUMBER", value);
175 parmValue = new SvcLogicAtom("STRING", value);
178 LOG.debug("Setting parameter "+name+" = "+value+" = "+parmValue.asParsedExpr());
179 parameters.put(name, parmValue);
181 catch (IOException e) {
183 LOG.error("Invalid parameter value expression ("+value+")");
184 throw new SvcLogicException(e.getMessage());
188 public SvcLogicExpression getParameter(String name)
190 if (parameters.containsKey(name))
192 return(parameters.get(name));
200 public boolean isVisited() {
204 public void setVisited(boolean visited, boolean recursive) {
205 this.visited = visited;
209 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = getOutcomeSet();
211 if (outcomeSet == null)
216 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
218 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
219 SvcLogicNode outNode = curOutcome.getValue();
220 outNode.setVisited(visited, recursive);
225 public void addOutcome(String outcomeValue, SvcLogicNode node) throws SvcLogicException
227 if (outcomes == null)
229 outcomes = new HashMap<String, SvcLogicNode>();
232 if (outcomeValue.length() == 0) {
233 outcomeValue = "\"\"";
235 if (outcomes.containsKey(outcomeValue))
237 throw new DuplicateValueException("Duplicate outcome value "+outcomeValue);
240 outcomes.put(outcomeValue, node);
243 public Set<Map.Entry<String, SvcLogicNode>> getOutcomeSet()
245 if (outcomes == null)
250 return(outcomes.entrySet());
254 public Set<Map.Entry<String, SvcLogicExpression>> getParameterSet()
256 if (parameters == null)
261 return(parameters.entrySet());
265 public void printAsGv(PrintStream pstr)
277 StringBuffer sbuff = new StringBuffer();
279 sbuff.append("node");
280 sbuff.append(nodeId);
281 sbuff.append(" [ shape=none, margin=0, label=<<table border=\"0\" cellborder=\"1\" align=\"left\">");
282 sbuff.append("<tr><td colspan=\"2\"><b>");
283 sbuff.append(nodeId);
285 sbuff.append(nodeType);
286 sbuff.append("</b></td></tr><th><td><i>Attribute</i></td><td><i>Value</i></td></th>");
288 if (nodeName.length() > 0)
290 sbuff.append("<tr><td>name</td><td>");
291 sbuff.append(nodeName);
292 sbuff.append("</td></tr>");
295 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
296 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
298 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
299 sbuff.append("<tr><td>");
300 sbuff.append(curAttr.getKey());
301 sbuff.append("</td><td>");
302 sbuff.append(StringEscapeUtils.escapeHtml3(curAttr.getValue().toString()));
303 sbuff.append("</td></tr>");
305 sbuff.append("</table>>];");
307 pstr.println(sbuff.toString());
310 if (outcomes != null)
312 TreeMap<String, SvcLogicNode> sortedOutcomes = new TreeMap<String, SvcLogicNode>(outcomes);
313 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = sortedOutcomes.entrySet();
315 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
317 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
318 String outValue = curOutcome.getKey();
319 SvcLogicNode outNode = curOutcome.getValue();
320 pstr.println("node"+nodeId+" -> node"+outNode.getNodeId()+" [label=\""+outValue+"\"];");
321 outNode.printAsGv(pstr);
326 public void printAsXml(PrintStream pstr, int indentLvl)
333 for (int i = 0 ; i < indentLvl ; i++)
338 pstr.print(this.getNodeType());
340 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
341 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
343 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
345 pstr.print(curAttr.getKey());
347 pstr.print(curAttr.getValue());
351 if (((parameters == null) || (parameters.isEmpty())) &&
352 ((outcomes == null) || outcomes.isEmpty()))
363 // Print parameters (if any)
364 if (parameters != null)
366 Set<Map.Entry<String, SvcLogicExpression>> paramSet = parameters.entrySet();
367 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = paramSet.iterator() ; iter.hasNext();)
369 for (int i = 0 ; i < indentLvl+1 ; i++)
373 pstr.print("<parameter");
374 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
375 pstr.print(" name='");
376 pstr.print(curAttr.getKey());
377 pstr.print("' value='`");
378 pstr.print(curAttr.getValue().toString());
379 pstr.print("`'/>\n");
383 // Print outcomes (if any)
384 if (outcomes != null)
386 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = outcomes.entrySet();
387 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator() ; iter.hasNext();)
389 for (int i = 0 ; i < indentLvl+1 ; i++)
393 pstr.print("<outcome");
394 Map.Entry<String, SvcLogicNode> curAttr = iter.next();
395 pstr.print(" value='");
396 pstr.print(curAttr.getKey());
398 SvcLogicNode outNode = curAttr.getValue();
399 outNode.printAsXml(pstr, indentLvl+2);
400 for (int i = 0 ; i < indentLvl+1 ; i++)
404 pstr.print("</outcome>\n");
408 // Print node end tag
409 for (int i = 0 ; i < indentLvl ; i++)
414 pstr.print(this.getNodeType());
421 public SvcLogicNode getOutcomeValue(String value)
424 if (value.length() == 0) {
427 if (outcomes == null)
432 if (outcomes.containsKey(value))
434 return(outcomes.get(value));
438 StringBuffer keyBuffer = new StringBuffer();
439 keyBuffer.append("{");
440 for (String key : outcomes.keySet()) {
441 keyBuffer.append(" ("+key+")");
443 keyBuffer.append("}");
444 LOG.info("Outcome (" + value + ") not found, keys are " + keyBuffer.toString());
446 if (outcomes.containsKey("Other"))
448 return(outcomes.get("Other"));