f248a5930be9cecc299e5f0e76f6ebbda42ff570
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / SvcLogicExpressionResolver.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 package org.openecomp.sdnc.sli.provider;
23
24 import java.util.List;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.openecomp.sdnc.sli.SvcLogicAtom;
28 import org.openecomp.sdnc.sli.SvcLogicBinaryExpression;
29 import org.openecomp.sdnc.sli.SvcLogicContext;
30 import org.openecomp.sdnc.sli.SvcLogicException;
31 import org.openecomp.sdnc.sli.SvcLogicExpression;
32 import org.openecomp.sdnc.sli.SvcLogicFunctionCall;
33 import org.openecomp.sdnc.sli.SvcLogicNode;
34 import org.openecomp.sdnc.sli.SvcLogicVariableTerm;
35 import org.openecomp.sdnc.sli.SvcLogicAtom.AtomType;
36 import org.openecomp.sdnc.sli.SvcLogicBinaryExpression.OperatorType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class SvcLogicExpressionResolver {
41
42         private static final Logger LOG = LoggerFactory
43                         .getLogger(SvcLogicExpressionResolver.class);
44
45         public static String evaluate(SvcLogicExpression expr, SvcLogicNode node,
46                         SvcLogicContext ctx) throws SvcLogicException {
47                 if (expr == null) {
48                         return (null);
49                 }
50
51         
52
53                 if (expr instanceof SvcLogicAtom) {
54                         SvcLogicAtom atom = (SvcLogicAtom) expr;
55
56                         AtomType atomType = atom.getAtomType();
57                         switch (atomType) {
58                         case NUMBER:
59                         case STRING:
60                                 return (atom.toString());
61                         case CONTEXT_VAR:
62                         case IDENTIFIER:
63         
64                                 String varName = resolveVariableName(atom, node, ctx);
65                                 
66                                 if (atomType == AtomType.CONTEXT_VAR)
67                                 {
68                                         LOG.debug("Evaluating context variable $"+varName);
69                                         
70                                         String varValue = ctx.getAttribute(varName);
71                                         
72                                         if (varValue == null) {
73                                                 LOG.debug("Context variable $"+varName+" unset - treating as empty string");
74                                                 varValue = "";
75                                         }
76                                                 
77                                         return (varValue);
78                                 }
79                                 SvcLogicExpression parm = node.getParameter(varName);
80                                 if (parm != null) {
81                                         LOG.debug("Evaluating value of parameter "+varName+": "+parm.asParsedExpr());
82                                         
83                                         return (evaluate(parm, node, ctx));
84                                 }
85                                 else
86                                 {
87                                         return(varName);
88                                 }
89                         default:
90                                 return(null);
91                         }
92
93                 } else if (expr instanceof SvcLogicBinaryExpression) {
94                         SvcLogicBinaryExpression binExpr = (SvcLogicBinaryExpression) expr;
95                         List<OperatorType> operators = binExpr.getOperators();
96                         if (operators.isEmpty())
97                         {
98                                 List<SvcLogicExpression> operands = binExpr.getOperands();
99                                 if (operands.size() == 1)
100                                 {
101                                         LOG.debug("SvcLogicBinaryExpression as no operator and one operand - evaluating its operand");
102                                         return(evaluate(operands.get(0), node, ctx));
103                                 }
104                                 else
105                                 {
106                                         if (operands.isEmpty())
107                                         {
108                                                 LOG.error("SvcLogicBinaryExpression has no operators and no operands - evaluating value as null");
109                                         }
110                                         else
111                                         {
112                                                 LOG.error("SvcLogicBinaryExpression has no operators and "+operands.size()+" operands - evaluating value as null");
113                                         }
114                                         return(null);
115                                 }
116                         }
117                         switch (operators.get(0)) {
118                                 case addOp:
119                                 case subOp:
120                                 case multOp:
121                                 case divOp:
122                                         return(evalArithExpression(binExpr, node, ctx));
123                                 case equalOp:
124                                 case neOp:
125                                 case ltOp:
126                                 case leOp:
127                                 case gtOp:
128                                 case geOp:
129                                         return (evalCompareExpression(binExpr, node, ctx));
130                                 case andOp:
131                                 case orOp:
132                                         return(evalLogicExpression(binExpr, node, ctx));
133
134                                 default:
135                                         return(null);
136                         }
137                 }
138                 else if (expr instanceof SvcLogicFunctionCall)
139                 {
140                         return(evalFunctionCall((SvcLogicFunctionCall)expr, node, ctx));
141                 }
142                 else
143                 {
144                         throw new SvcLogicException("Unrecognized expression type ["+expr+"]");
145                 }
146         }
147
148         private static String evalArithExpression(SvcLogicBinaryExpression binExpr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
149                 List<SvcLogicExpression> operands = binExpr.getOperands();
150                 List<OperatorType> operators = binExpr.getOperators();
151                 if (operands.size() != (operators.size()+1))
152                 {
153                         throw new SvcLogicException("Invalid expression ("+binExpr+")");
154                 }       
155                 String retval = evaluate(operands.get(0), node, ctx);
156                 String retsval = retval;
157                 long retlval = 0;
158                 boolean valueIsLong = false;
159
160                 int i = 1;
161                 try
162                 {
163                         
164                         if ((retval.length() > 0) && StringUtils.isNumeric(retval))
165                         {
166                                 retlval = Long.parseLong(retval);
167                                 valueIsLong = true;
168                         }
169                         for (OperatorType operator: operators)
170                         {
171                                 String curOperandValue = evaluate(operands.get(i++), node, ctx);
172                                 switch(operator) {
173                                 case addOp:
174                                         retsval = retsval + curOperandValue;
175                                         if (valueIsLong)
176                                         {
177                                                 if ((curOperandValue.length() > 0) && StringUtils.isNumeric(curOperandValue) )
178                                                 {
179                                                         retlval = retlval + Long.parseLong(curOperandValue);
180                                                 }
181                                                 else
182                                                 {
183                                                         valueIsLong = false;
184                                                 }
185                                         }
186                                         break;
187                                 case subOp:
188                                         retlval = retlval - Long.parseLong(curOperandValue);
189                                         break;
190                                 case multOp:
191                                         retlval = retlval * Long.parseLong(curOperandValue);
192                                         break;
193                                 case divOp:
194                                         retlval = retlval / Long.parseLong(curOperandValue);
195                                         break;
196                                 }
197
198                         }
199                 }
200                 catch (NumberFormatException e1)
201                 {
202                         throw new SvcLogicException("Illegal value in arithmetic expression", e1);
203                 }
204                 
205                 if (valueIsLong)
206                 {
207                         return("" + retlval);
208                 }
209                 else
210                 {
211                         return(retsval);
212                 }
213                 
214         }
215
216
217         
218         private static String evalCompareExpression(SvcLogicBinaryExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
219         {
220
221                 List<OperatorType> operators = expr.getOperators();
222                 List<SvcLogicExpression> operands = expr.getOperands();
223                 
224                 if ((operators.size() != 1) || (operands.size() != 2))
225                 {
226                         throw new SvcLogicException ("Invalid comparison expression : "+expr);
227                 }
228                 
229                 OperatorType operator = operators.get(0);
230                 String op1Value = evaluate(operands.get(0), node, ctx);
231                 String op2Value = evaluate(operands.get(1), node, ctx);
232                 
233                 if ((StringUtils.isNotEmpty(op1Value) && StringUtils.isNumeric(op1Value) && StringUtils.isNotEmpty(op2Value) && StringUtils.isNumeric(op2Value)))
234                 {
235                         try
236                         {
237                                 double op1dbl = Double.parseDouble(op1Value);
238                                 double op2dbl = Double.parseDouble(op2Value);
239                                 
240                                 switch(operator)
241                                 {
242                                 case equalOp:
243                                         return(Boolean.toString(op1dbl == op2dbl));
244                                 case neOp:
245                                         return(Boolean.toString(op1dbl != op2dbl));
246                                 case ltOp:
247                                         return(Boolean.toString(op1dbl < op2dbl));
248                                 case leOp:
249                                         return(Boolean.toString(op1dbl <= op2dbl));
250                                 case gtOp:
251                                         return(Boolean.toString(op1dbl > op2dbl));
252                                 case geOp:
253                                         return(Boolean.toString(op1dbl >= op2dbl));
254                                 default:
255                                         return(null);
256                                 }
257                         }
258                         catch (NumberFormatException e)
259                         {
260                                 throw new SvcLogicException("Caught exception trying to compare numeric values", e);
261                         }
262                 }
263                 else
264                 {
265                         
266                         int compResult = 0;
267                         
268                         if (op1Value == null) {
269                                 compResult = -1;
270                         } else if (op2Value == null ) {
271                                 compResult = 1;
272                         } else {
273                                 compResult = op1Value.compareToIgnoreCase(op2Value);
274                         }
275                         
276                         switch(operator)
277                         {
278                         case equalOp:
279                                 return(Boolean.toString(compResult == 0));
280                         case neOp:
281                                 return(Boolean.toString(compResult != 0));
282                         case ltOp:
283                                 return(Boolean.toString(compResult < 0));
284                         case leOp:
285                                 return(Boolean.toString(compResult <= 0));
286                         case gtOp:
287                                 return(Boolean.toString(compResult > 0));
288                         case geOp:
289                                 return(Boolean.toString(compResult >= 0));
290                         default:
291                                 return(null);
292                         }
293                 }
294                 
295         }
296         
297         private static String evalLogicExpression(SvcLogicBinaryExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
298         {
299                 boolean retval;
300                 
301                 List<SvcLogicExpression> operands = expr.getOperands();
302                 List<OperatorType> operators = expr.getOperators();
303                 
304                 if (operands.size() != (operators.size()+1))
305                 {
306                         throw new SvcLogicException("Invalid expression ("+expr+")");
307                 }
308                 
309                 try
310                 {
311                         retval = Boolean.parseBoolean(evaluate(operands.get(0), node, ctx));
312                         int i = 1;
313                         for (OperatorType operator : operators)
314                         {
315                                 if (operator == OperatorType.andOp)
316                                 {
317                                         retval = retval && Boolean.parseBoolean(evaluate(operands.get(i++), node, ctx));
318                                 }
319                                 else
320                                 {
321
322                                         retval = retval || Boolean.parseBoolean(evaluate(operands.get(i++), node, ctx));
323                                 }
324                                 
325                         }
326                 }
327                 catch (Exception e)
328                 {
329                         throw new SvcLogicException("Invalid expression ("+expr+")");
330                 }
331                 
332                 
333                 return(Boolean.toString(retval));
334         }
335         
336         private static String evalFunctionCall(SvcLogicFunctionCall func, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
337         {
338                 String funcName = func.getFunctionName();
339                 List<SvcLogicExpression> operands = func.getOperands();
340                 
341                 if ("length".equalsIgnoreCase(funcName))
342                 {
343                         
344                         if (operands.size() == 1)
345                         {
346                                 String opValue = evaluate(operands.get(0), node, ctx);
347                                 return(""+opValue.length());
348                         }
349                         else
350                         {
351                                 throw new SvcLogicException("Invalid call to length() function");
352                         }
353                 }
354                 else if ("substr".equalsIgnoreCase(funcName))
355                 {
356                         if (operands.size() == 3)
357                         {
358                                 String op1Value = evaluate(operands.get(0), node, ctx);
359                                 String op2Value = evaluate(operands.get(1), node, ctx);
360                                 String op3Value = evaluate(operands.get(2), node, ctx);
361                                 
362                                 if (!StringUtils.isNumeric(op2Value) || !StringUtils.isNumeric(op3Value))
363                                 {
364                                         throw new SvcLogicException("Invalid arguments to substr() function");
365                                 }
366                                 
367                                 try
368                                 {
369                                         return(op1Value.substring(Integer.parseInt(op2Value), Integer.parseInt(op3Value)));
370                                 }
371                                 catch (Exception e)
372                                 {
373                                         throw new SvcLogicException("Caught exception trying to take substring", e);
374                                 }
375                         }
376                         else
377                         {
378
379                                 throw new SvcLogicException("Invalid call to substr() function");
380                         }
381                         
382                 }
383                 else if ("toUpperCase".equalsIgnoreCase(funcName))
384                 {
385                         if (operands.size() == 1)
386                         {
387                                 String opValue = evaluate(operands.get(0), node, ctx);
388                                 if (opValue != null) {
389                                         return(opValue.toUpperCase());
390                                 } else {
391                                         return("");
392                                 }
393                         }
394                         else
395                         {
396                                 throw new SvcLogicException("Invalid call to toUpperCase() function");
397                         }
398                 }
399                 else if ("toLowerCase".equalsIgnoreCase(funcName))
400                 {
401                         if (operands.size() == 1)
402                         {
403                                 String opValue = evaluate(operands.get(0), node, ctx);
404                                 if (opValue != null) {
405                                         return(opValue.toLowerCase());
406                                 } else {
407                                         return("");
408                                 }
409                         }
410                         else
411                         {
412                                 throw new SvcLogicException("Invalid call to toLowerCase() function");
413                         }
414                 }
415                 else if ("convertBase".equalsIgnoreCase(funcName)) {
416                         int fromBase = 10;
417                         int toBase = 10;
418                         String srcString = "";
419                         
420                         if (operands.size() == 2)
421                         {
422                                 fromBase = 10;
423                                 srcString = evaluate(operands.get(0), node, ctx);
424                                 toBase = Integer.parseInt(evaluate(operands.get(1), node, ctx));
425                         } else if (operands.size() == 3) {
426
427                                 srcString = evaluate(operands.get(0), node, ctx);
428                                 fromBase = Integer.parseInt(evaluate(operands.get(1), node, ctx));
429                                 toBase = Integer.parseInt(evaluate(operands.get(2), node, ctx));
430                         } else {
431                                 throw new SvcLogicException("Invalid call to convertBase() function");
432                         }
433                         
434                         long srcValue = Long.parseLong(srcString, fromBase);
435                         return(Long.toString(srcValue, toBase));
436                 }
437                 else
438                 {
439                         throw new SvcLogicException("Unrecognized function ("+funcName+")");
440                 }
441                 
442         }
443         
444         public static String evaluateAsKey(SvcLogicExpression expr, SvcLogicNode node,
445                         SvcLogicContext ctx) throws SvcLogicException {
446                 if (expr == null) {
447                         return (null);
448                 }
449
450         
451
452                 if (expr instanceof SvcLogicAtom) {
453                         SvcLogicAtom atom = (SvcLogicAtom) expr;
454
455                         AtomType atomType = atom.getAtomType();
456                         StringBuffer varNameBuff = new StringBuffer();
457                         switch (atomType) {
458                         case NUMBER:
459                                 return (atom.toString());
460                         case STRING:
461                                 return("'"+atom.toString()+"'");
462                         case CONTEXT_VAR:
463                         case IDENTIFIER:
464                                 boolean needDot = false;
465                 for (SvcLogicExpression term : atom.getOperands())
466                 {
467                         if (needDot)
468                         {
469                                 varNameBuff.append(".");
470                         }
471                         if (term instanceof SvcLogicVariableTerm)
472                         {
473                                 SvcLogicVariableTerm vterm = (SvcLogicVariableTerm) term;
474                                 varNameBuff.append(vterm.getName());
475                                 if (vterm.numOperands() > 0)
476                                 {
477                                         varNameBuff.append("[");
478                                         varNameBuff.append(evaluate(vterm.getSubscript(), node, ctx));
479                                         varNameBuff.append("]");
480                                         
481                                 }
482                         }
483                         else
484                         {
485                                 varNameBuff.append(term.toString());
486                         }
487                         needDot = true;
488                 }
489
490                                 String varName = varNameBuff.toString();
491                                 LOG.debug("Evaluating context variable $"+varName);
492                                 String ctxValue = ctx.getAttribute(varName);
493                                 if (ctxValue == null)
494                                 {
495                                         return(null);
496                                 }
497                                 if (StringUtils.isNumeric(ctxValue))
498                                 {
499                                         return(ctxValue);
500                                 }
501                                 else
502                                 {
503                                         return("'"+ctxValue+"'");
504                                 }
505         
506                         default:
507                                 return(null);
508                         }
509
510                 } else if (expr instanceof SvcLogicBinaryExpression) {
511                         SvcLogicBinaryExpression binExpr = (SvcLogicBinaryExpression) expr;
512                         List<OperatorType> operators = binExpr.getOperators();
513                         List<SvcLogicExpression> operands = binExpr.getOperands();
514                         if (operators.isEmpty())
515                         {
516                                 if (operands.size() == 1)
517                                 {
518                                         LOG.debug("SvcLogicBinaryExpression as no operator and one operand - evaluating its operand");
519                                         return(evaluateAsKey(operands.get(0), node, ctx));
520                                 }
521                                 else
522                                 {
523                                         if (operands.isEmpty())
524                                         {
525                                                 LOG.error("SvcLogicBinaryExpression has no operators and no operands - evaluating value as null");
526                                         }
527                                         else
528                                         {
529                                                 LOG.error("SvcLogicBinaryExpression has no operators and "+operands.size()+" operands - evaluating value as null");
530                                         }
531                                         return(null);
532                                 }
533                         }
534                         StringBuffer sbuff = new StringBuffer();
535                         sbuff.append(evaluateAsKey(operands.get(0), node, ctx));
536                         int i = 1;
537                         for (OperatorType operator : operators)
538                         {
539                                 sbuff.append(" ");
540                                 sbuff.append(operator.toString());
541                                 sbuff.append(" ");
542                                 sbuff.append(evaluateAsKey(operands.get(i++), node,ctx));
543                         }
544                         return(sbuff.toString());
545                 }
546                 else if (expr instanceof SvcLogicFunctionCall)
547                 {
548                         StringBuffer sbuff = new StringBuffer();
549                         SvcLogicFunctionCall funcCall = (SvcLogicFunctionCall) expr;
550                         sbuff.append(funcCall.getFunctionName());
551                         sbuff.append("(");
552                         boolean needComma = false;
553                         for (SvcLogicExpression operand : funcCall.getOperands())
554                         {
555                                 if (needComma)
556                                 {
557                                         sbuff.append(",");
558                                 }
559                                 else
560                                 {
561                                         needComma = true;
562                                 }
563                                 sbuff.append(evaluateAsKey(operand, node, ctx));
564                         }
565                         sbuff.append(")");
566                         return(sbuff.toString());
567                 }
568                 else
569                 {
570                         throw new SvcLogicException("Unrecognized expression type ["+expr+"]");
571                 }
572         }
573         
574         public static String resolveVariableName(SvcLogicExpression atom, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
575         {
576                 StringBuffer varNameBuff = new StringBuffer();
577                 
578                 boolean needDot = false;
579                 for (SvcLogicExpression term : atom.getOperands())
580                 {
581                         if (needDot)
582                         {
583                                 varNameBuff.append(".");
584                         }
585                         if (term instanceof SvcLogicVariableTerm)
586                         {
587                                 SvcLogicVariableTerm vterm = (SvcLogicVariableTerm) term;
588                                 varNameBuff.append(vterm.getName());
589                                 if (vterm.numOperands() > 0)
590                                 {
591                                         varNameBuff.append("[");
592                                         varNameBuff.append(evaluate(vterm.getSubscript(), node, ctx));
593                                         varNameBuff.append("]");
594                                 }
595                         }
596                         else
597                         {
598                                 varNameBuff.append(term.toString());
599                         }
600                         needDot = true;
601                 }
602                 return(varNameBuff.toString());
603         }
604
605 }