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