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