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