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.onap.ccsdk.sli.core.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;
40 public class SvcLogicNode implements Serializable {
42 private static final Logger LOG = LoggerFactory
43 .getLogger(SvcLogicExprListener.class);
45 private static final long serialVersionUID = 2L;
47 private String nodeName;
49 private String nodeType;
50 private boolean visited;
51 private SvcLogicGraph graph;
54 private HashMap<String, SvcLogicExpression> attributes;
55 private HashMap<String, SvcLogicNode> outcomes;
56 private HashMap<String, SvcLogicExpression> parameters;
58 public SvcLogicNode(int nodeId, String nodeType, SvcLogicGraph graph)
62 this.nodeType = nodeType;
64 attributes = new HashMap<String, SvcLogicExpression> ();
65 parameters = new HashMap<String, SvcLogicExpression> ();
70 public SvcLogicNode(int nodeId, String nodeType, String nodeName, SvcLogicGraph graph) throws DuplicateValueException
73 this.nodeName = nodeName;
74 this.nodeType = nodeType;
76 attributes = new HashMap<String, SvcLogicExpression> ();
77 parameters = new HashMap<String, SvcLogicExpression> ();
79 graph.setNamedNode(nodeName, this);
83 public int getNodeId()
88 public String getNodeName()
93 public String getNodeType()
98 public SvcLogicGraph getGraph()
103 public int getNumOutcomes()
105 if (outcomes == null)
111 return outcomes.size();
115 public SvcLogicExpression getAttribute(String name)
117 if (attributes.containsKey(name))
119 return attributes.get(name);
128 public void setAttribute(String name, String value) throws SvcLogicException
130 setAttribute(name, new SvcLogicAtom("STRING", value));
133 public void setAttribute(String name, SvcLogicExpression value) throws SvcLogicException
135 if (attributes.containsKey(name))
137 throw new DuplicateValueException("Duplicate attribute "+name);
140 attributes.put(name, value);
144 public void mapParameter(String name, String value) throws SvcLogicException
147 if (parameters.containsKey(name))
149 throw new DuplicateValueException("Duplicate parameter "+name);
153 SvcLogicExpression parmValue;
154 if (value == null || value.length() == 0)
156 parmValue = new SvcLogicAtom("STRING", "");
158 else if (value.trim().startsWith("`"))
160 int lastParen = value.lastIndexOf("`");
161 String evalExpr = value.trim().substring(1, lastParen);
162 parmValue = SvcLogicExpressionFactory.parse(evalExpr);
167 if (Character.isDigit(value.charAt(0)))
169 parmValue = new SvcLogicAtom("NUMBER", value);
173 parmValue = new SvcLogicAtom("STRING", value);
176 LOG.debug("Setting parameter "+name+" = "+value+" = "+parmValue.asParsedExpr());
177 parameters.put(name, parmValue);
179 catch (IOException e) {
181 LOG.error("Invalid parameter value expression ("+value+")");
182 throw new SvcLogicException(e.getMessage());
186 public SvcLogicExpression getParameter(String name)
188 if (parameters.containsKey(name))
190 return parameters.get(name);
198 public boolean isVisited() {
202 public void setVisited(boolean visited, boolean recursive) {
203 this.visited = visited;
207 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = getOutcomeSet();
209 if (outcomeSet == null)
214 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
216 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
217 SvcLogicNode outNode = curOutcome.getValue();
218 outNode.setVisited(visited, recursive);
223 public void addOutcome(String outcomeValue, SvcLogicNode node) throws SvcLogicException
225 if (outcomes == null)
227 outcomes = new HashMap<String, SvcLogicNode>();
230 if (outcomeValue.length() == 0) {
231 outcomeValue = "\"\"";
233 if (outcomes.containsKey(outcomeValue))
235 throw new DuplicateValueException("Duplicate outcome value "+outcomeValue);
238 outcomes.put(outcomeValue, node);
241 public Set<Map.Entry<String, SvcLogicNode>> getOutcomeSet()
243 if (outcomes == null)
248 return outcomes.entrySet();
252 public Set<Map.Entry<String, SvcLogicExpression>> getParameterSet()
254 if (parameters == null)
259 return parameters.entrySet();
263 public void printAsGv(PrintStream pstr)
275 StringBuffer sbuff = new StringBuffer();
277 sbuff.append("node");
278 sbuff.append(nodeId);
279 sbuff.append(" [ shape=none, margin=0, label=<<table border=\"0\" cellborder=\"1\" align=\"left\">");
280 sbuff.append("<tr><td colspan=\"2\"><b>");
281 sbuff.append(nodeId);
283 sbuff.append(nodeType);
284 sbuff.append("</b></td></tr><th><td><i>Attribute</i></td><td><i>Value</i></td></th>");
286 if (nodeName.length() > 0)
288 sbuff.append("<tr><td>name</td><td>");
289 sbuff.append(nodeName);
290 sbuff.append("</td></tr>");
293 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
294 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
296 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
297 sbuff.append("<tr><td>");
298 sbuff.append(curAttr.getKey());
299 sbuff.append("</td><td>");
300 sbuff.append(StringEscapeUtils.escapeHtml3(curAttr.getValue().toString()));
301 sbuff.append("</td></tr>");
303 sbuff.append("</table>>];");
305 pstr.println(sbuff.toString());
308 if (outcomes != null)
310 TreeMap<String, SvcLogicNode> sortedOutcomes = new TreeMap<String, SvcLogicNode>(outcomes);
311 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = sortedOutcomes.entrySet();
313 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
315 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
316 String outValue = curOutcome.getKey();
317 SvcLogicNode outNode = curOutcome.getValue();
318 pstr.println("node"+nodeId+" -> node"+outNode.getNodeId()+" [label=\""+outValue+"\"];");
319 outNode.printAsGv(pstr);
324 public void printAsXml(PrintStream pstr, int indentLvl)
331 for (int i = 0 ; i < indentLvl ; i++)
336 pstr.print(this.getNodeType());
338 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
339 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
341 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
343 pstr.print(curAttr.getKey());
345 pstr.print(curAttr.getValue());
349 if ((parameters == null || parameters.isEmpty()) &&
350 (outcomes == null || outcomes.isEmpty()))
361 // Print parameters (if any)
362 if (parameters != null)
364 Set<Map.Entry<String, SvcLogicExpression>> paramSet = parameters.entrySet();
365 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = paramSet.iterator() ; iter.hasNext();)
367 for (int i = 0 ; i < indentLvl+1 ; i++)
371 pstr.print("<parameter");
372 Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
373 pstr.print(" name='");
374 pstr.print(curAttr.getKey());
375 pstr.print("' value='`");
376 pstr.print(curAttr.getValue().toString());
377 pstr.print("`'/>\n");
381 // Print outcomes (if any)
382 if (outcomes != null)
384 Set<Map.Entry<String, SvcLogicNode>> outcomeSet = outcomes.entrySet();
385 for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator() ; iter.hasNext();)
387 for (int i = 0 ; i < indentLvl+1 ; i++)
391 pstr.print("<outcome");
392 Map.Entry<String, SvcLogicNode> curAttr = iter.next();
393 pstr.print(" value='");
394 pstr.print(curAttr.getKey());
396 SvcLogicNode outNode = curAttr.getValue();
397 outNode.printAsXml(pstr, indentLvl+2);
398 for (int i = 0 ; i < indentLvl+1 ; i++)
402 pstr.print("</outcome>\n");
406 // Print node end tag
407 for (int i = 0 ; i < indentLvl ; i++)
412 pstr.print(this.getNodeType());
419 public SvcLogicNode getOutcomeValue(String value)
422 if (value.length() == 0) {
425 if (outcomes == null)
430 if (outcomes.containsKey(value))
432 return outcomes.get(value);
436 StringBuffer keyBuffer = new StringBuffer();
437 keyBuffer.append("{");
438 for (String key : outcomes.keySet()) {
439 keyBuffer.append(" ("+key+")");
441 keyBuffer.append("}");
442 LOG.info("Outcome (" + value + ") not found, keys are " + keyBuffer.toString());
444 if (outcomes.containsKey("Other"))
446 return outcomes.get("Other");