Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /**
22  * 
23  */
24 package org.onap.ccsdk.sli.core.sli;
25
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;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.TreeMap;
34
35 import org.apache.commons.lang3.StringEscapeUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.xml.sax.Locator;
39
40
41 public class SvcLogicNode implements Serializable {
42         
43         private static final Logger LOG = LoggerFactory
44                         .getLogger(SvcLogicExprListener.class);
45         
46         private static final long serialVersionUID = 2L;
47         
48         private String nodeName;
49         private int nodeId;
50         private String nodeType;
51         private boolean visited;
52         private SvcLogicGraph graph;
53
54
55         private HashMap<String, SvcLogicExpression> attributes;
56         private HashMap<String, SvcLogicNode> outcomes;
57         private HashMap<String, SvcLogicExpression> parameters;
58         
59         public SvcLogicNode(int nodeId, String nodeType, SvcLogicGraph graph)
60         {
61                 this.nodeId = nodeId;
62                 nodeName = "";
63                 this.nodeType = nodeType;
64                 this.graph = graph;
65                 attributes = new HashMap<String, SvcLogicExpression> ();
66                 parameters = new HashMap<String, SvcLogicExpression> ();
67                 outcomes = null;
68                 
69         }
70         
71         public SvcLogicNode(int nodeId, String nodeType, String nodeName, SvcLogicGraph graph) throws DuplicateValueException
72         {
73                 this.nodeId = nodeId;
74                 this.nodeName = nodeName;
75                 this.nodeType = nodeType;
76                 this.graph = graph;
77                 attributes = new HashMap<String, SvcLogicExpression> ();
78                 parameters = new HashMap<String, SvcLogicExpression> ();
79                 outcomes = null;
80                 graph.setNamedNode(nodeName, this);
81         }
82         
83         
84         public int getNodeId()
85         {
86                 return nodeId;
87         }
88         
89         public String getNodeName()
90         {
91                 return(nodeName);
92         }
93         
94         public String getNodeType()
95         {
96                 return(nodeType);
97         }
98         
99         public SvcLogicGraph getGraph()
100         {
101                 return(graph);
102         }
103         
104         public int getNumOutcomes()
105         {
106                 if (outcomes == null)
107                 {
108                         return(0);
109                 }
110                 else
111                 {
112                         return(outcomes.size());
113                 }
114         }
115         
116         public SvcLogicExpression getAttribute(String name)
117         {
118                 if (attributes.containsKey(name))
119                 {
120                         return(attributes.get(name));
121                 }
122                 else
123                 {
124                         return(null);
125                 }
126                         
127         }
128         
129         public void setAttribute(String name, String value) throws SvcLogicException
130         {
131                 setAttribute(name, new SvcLogicAtom("STRING", value));
132         }
133         
134         public void setAttribute(String name, SvcLogicExpression value) throws SvcLogicException
135         {
136                 if (attributes.containsKey(name))
137                 {
138                         throw new DuplicateValueException("Duplicate attribute "+name);
139                 }
140                 
141                 attributes.put(name, value);
142         }
143         
144
145         public void mapParameter(String name, String value) throws SvcLogicException
146         {
147                 
148                 if (parameters.containsKey(name))
149                 {
150                         throw new DuplicateValueException("Duplicate parameter "+name);
151                 }
152                 try
153                 {
154                         SvcLogicExpression parmValue;
155                         if ((value == null) || (value.length() == 0))
156                         {
157                                 parmValue = new SvcLogicAtom("STRING", "");
158                         }
159                         else if (value.trim().startsWith("`"))
160                         {
161                                 int lastParen = value.lastIndexOf("`");
162                                 String evalExpr = value.trim().substring(1, lastParen);
163                                 parmValue = SvcLogicExpressionFactory.parse(evalExpr);
164                                 
165                         }
166                         else
167                         {
168                                 if (Character.isDigit(value.charAt(0)))
169                                 {
170                                         parmValue = new SvcLogicAtom("NUMBER", value);
171                                 }
172                                 else
173                                 {
174                                         parmValue = new SvcLogicAtom("STRING", value);
175                                 }
176                         }
177                         LOG.debug("Setting parameter "+name+" = "+value+" = "+parmValue.asParsedExpr());
178                         parameters.put(name, parmValue);
179                 }
180                 catch (IOException e) {
181
182                         LOG.error("Invalid parameter value expression ("+value+")");
183                         throw new SvcLogicException(e.getMessage());
184                 }
185         }
186         
187         public SvcLogicExpression getParameter(String name)
188         {
189                 if (parameters.containsKey(name))
190                 {
191                         return(parameters.get(name));
192                 }
193                 else
194                 {
195                         return(null);
196                 }
197         }
198         
199         public boolean isVisited() {
200                 return visited;
201         }
202
203         public void setVisited(boolean visited, boolean recursive) {
204                 this.visited = visited;
205                 
206                 if (recursive)
207                 {
208                         Set<Map.Entry<String, SvcLogicNode>> outcomeSet = getOutcomeSet();
209                         
210                         if (outcomeSet == null)
211                         {
212                                 return;
213                         }
214                         
215                         for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
216                         {
217                                 Map.Entry<String, SvcLogicNode> curOutcome = iter.next();
218                                 SvcLogicNode outNode = curOutcome.getValue();
219                                 outNode.setVisited(visited, recursive);
220                         }
221                 }
222         }
223         
224         public void addOutcome(String outcomeValue, SvcLogicNode node) throws SvcLogicException
225         {
226                 if (outcomes == null)
227                 {
228                         outcomes = new HashMap<String, SvcLogicNode>();
229                 }
230                 
231                 if (outcomeValue.length() == 0) {
232                         outcomeValue = "\"\"";
233                 }
234                 if (outcomes.containsKey(outcomeValue))
235                 {
236                         throw new DuplicateValueException("Duplicate outcome value "+outcomeValue);
237                 }
238                 
239                 outcomes.put(outcomeValue, node);
240         }
241         
242         public Set<Map.Entry<String, SvcLogicNode>> getOutcomeSet()
243         {
244                 if (outcomes == null)
245                 {
246                         return null;
247                 }
248                 
249                 return(outcomes.entrySet());
250                 
251         }
252         
253         public Set<Map.Entry<String, SvcLogicExpression>> getParameterSet()
254         {
255                 if (parameters == null)
256                 {
257                         return null;
258                 }
259                 
260                 return(parameters.entrySet());
261                 
262         }
263         
264         public void printAsGv(PrintStream pstr)
265         {
266                 
267                 if (visited)
268                 {
269                         return;
270                 }
271                 else
272                 {
273                         visited = true;
274                 }
275                 
276                 StringBuffer sbuff = new StringBuffer();
277                 
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);
283                 sbuff.append(" : ");
284                 sbuff.append(nodeType);
285                 sbuff.append("</b></td></tr><th><td><i>Attribute</i></td><td><i>Value</i></td></th>");
286
287                 if (nodeName.length() > 0)
288                 {
289                         sbuff.append("<tr><td>name</td><td>");
290                         sbuff.append(nodeName);
291                         sbuff.append("</td></tr>");
292                 }
293                 
294                 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
295                 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
296                 {
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>");
303                 }
304                 sbuff.append("</table>>];");
305                 
306                 pstr.println(sbuff.toString());
307                 
308                 
309                 if (outcomes != null)
310                 {
311                         TreeMap<String, SvcLogicNode> sortedOutcomes = new TreeMap<String, SvcLogicNode>(outcomes);
312                         Set<Map.Entry<String, SvcLogicNode>> outcomeSet = sortedOutcomes.entrySet();
313                         
314                         for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator(); iter.hasNext();)
315                         {
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);
321                         }
322                 }
323         }
324         
325         public void printAsXml(PrintStream pstr, int indentLvl)
326         {
327                 if (visited)
328                 {
329                         return;
330                 }
331                 // Print node tag
332                 for (int i = 0 ; i < indentLvl ; i++)
333                 {
334                         pstr.print("  ");
335                 }
336                 pstr.print("<");
337                 pstr.print(this.getNodeType());
338                 
339                 Set<Map.Entry<String, SvcLogicExpression>> attrSet = attributes.entrySet();
340                 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = attrSet.iterator() ; iter.hasNext();)
341                 {
342                         Map.Entry<String, SvcLogicExpression> curAttr = iter.next();
343                         pstr.print(" ");
344                         pstr.print(curAttr.getKey());
345                         pstr.print("='`");
346                         pstr.print(curAttr.getValue());
347                         pstr.print("'`");
348                 }
349                 
350                 if (((parameters == null) || (parameters.isEmpty())) && 
351                                 ((outcomes == null) || outcomes.isEmpty()))
352                 {
353                         pstr.print("/>\n");
354                         pstr.flush();
355                         return;
356                 }
357                 else
358                 {
359                         pstr.print(">\n");
360                 }
361                 
362                 // Print parameters (if any)
363                 if (parameters != null)
364                 {
365                         Set<Map.Entry<String, SvcLogicExpression>> paramSet = parameters.entrySet();
366                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = paramSet.iterator() ; iter.hasNext();)
367                         {
368                                 for (int i = 0 ; i < indentLvl+1 ; i++)
369                                 {
370                                         pstr.print("  ");
371                                 }
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");
379                         }
380                 }
381
382                 // Print outcomes (if any)
383                 if (outcomes != null)
384                 {
385                         Set<Map.Entry<String, SvcLogicNode>> outcomeSet = outcomes.entrySet();
386                         for (Iterator<Map.Entry<String, SvcLogicNode>> iter = outcomeSet.iterator() ; iter.hasNext();)
387                         {
388                                 for (int i = 0 ; i < indentLvl+1 ; i++)
389                                 {
390                                         pstr.print("  ");
391                                 }
392                                 pstr.print("<outcome");
393                                 Map.Entry<String, SvcLogicNode> curAttr = iter.next();
394                                 pstr.print(" value='");
395                                 pstr.print(curAttr.getKey());
396                                 pstr.print("'>\n");
397                                 SvcLogicNode outNode = curAttr.getValue();
398                                 outNode.printAsXml(pstr, indentLvl+2);
399                                 for (int i = 0 ; i < indentLvl+1 ; i++)
400                                 {
401                                         pstr.print("  ");
402                                 }
403                                 pstr.print("</outcome>\n");
404                         }
405                 }
406                 
407                 // Print node end tag
408                 for (int i = 0 ; i < indentLvl ; i++)
409                 {
410                         pstr.print("  ");
411                 }
412                 pstr.print("</");
413                 pstr.print(this.getNodeType());
414                 pstr.print(">\n");
415                 pstr.flush();
416                 
417         }
418
419
420         public SvcLogicNode getOutcomeValue(String value)
421         {
422
423                 if (value.length() == 0) {
424                         value = "\"\"";
425                 }
426                 if (outcomes == null)
427                 {
428                         return(null);
429                 }
430                 
431                 if (outcomes.containsKey(value))
432                 {
433                         return(outcomes.get(value));
434                 }
435                 else
436                 {
437                         StringBuffer keyBuffer = new StringBuffer();
438                         keyBuffer.append("{");
439                         for (String key : outcomes.keySet()) {
440                                 keyBuffer.append(" ("+key+")");
441                         }
442                         keyBuffer.append("}");
443                         LOG.info("Outcome (" + value + ") not found, keys are " + keyBuffer.toString());
444
445                         if (outcomes.containsKey("Other"))
446                         {
447                                 return(outcomes.get("Other"));
448                         }
449                         else
450                         {
451                                 return(null);
452                         }
453                 }
454         }
455 }