2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.ccsdk.sli.core.sli.provider.base;
26 import java.util.List;
28 import org.apache.commons.lang.StringUtils;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicAtom;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicBinaryExpression;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicFunctionCall;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicVariableTerm;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicAtom.AtomType;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicBinaryExpression.OperatorType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class SvcLogicExpressionResolver {
44 private static final Logger LOG = LoggerFactory
45 .getLogger(SvcLogicExpressionResolver.class);
46 private static final String INVALID_EXPRESSION_MSG= "Invalid expression (";
48 public static String evaluate(SvcLogicExpression expr, SvcLogicNode node,
49 SvcLogicContext ctx) throws SvcLogicException {
56 if (expr instanceof SvcLogicAtom) {
57 SvcLogicAtom atom = (SvcLogicAtom) expr;
59 AtomType atomType = atom.getAtomType();
63 return (atom.toString());
67 String varName = resolveVariableName(atom, node, ctx);
69 if (atomType == AtomType.CONTEXT_VAR)
71 LOG.trace("Evaluating context variable $"+varName);
73 String varValue = ctx.getAttribute(varName);
75 if (varValue == null) {
76 LOG.trace("Context variable $"+varName+" unset - treating as empty string");
82 SvcLogicExpression parm = node.getParameter(varName);
84 LOG.trace("Evaluating value of parameter "+varName+": "+parm.asParsedExpr());
86 return (evaluate(parm, node, ctx));
96 } else if (expr instanceof SvcLogicBinaryExpression) {
97 SvcLogicBinaryExpression binExpr = (SvcLogicBinaryExpression) expr;
98 List<OperatorType> operators = binExpr.getOperators();
99 if (operators.isEmpty())
101 List<SvcLogicExpression> operands = binExpr.getOperands();
102 if (operands.size() == 1)
104 LOG.trace("SvcLogicBinaryExpression as no operator and one operand - evaluating its operand");
105 return(evaluate(operands.get(0), node, ctx));
109 if (operands.isEmpty())
111 LOG.error("SvcLogicBinaryExpression has no operators and no operands - evaluating value as null");
115 LOG.error("SvcLogicBinaryExpression has no operators and "+operands.size()+" operands - evaluating value as null");
120 switch (operators.get(0)) {
125 return(evalArithExpression(binExpr, node, ctx));
132 return (evalCompareExpression(binExpr, node, ctx));
135 return(evalLogicExpression(binExpr, node, ctx));
141 else if (expr instanceof SvcLogicFunctionCall)
143 return(evalFunctionCall((SvcLogicFunctionCall)expr, node, ctx));
147 throw new SvcLogicException("Unrecognized expression type ["+expr+"]");
151 private static String evalArithExpression(SvcLogicBinaryExpression binExpr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
152 List<SvcLogicExpression> operands = binExpr.getOperands();
153 List<OperatorType> operators = binExpr.getOperators();
154 if (operands.size() != (operators.size()+1))
156 throw new SvcLogicException(INVALID_EXPRESSION_MSG+binExpr+")");
158 String retval = evaluate(operands.get(0), node, ctx);
159 String retsval = retval;
161 boolean valueIsLong = false;
167 if ((retval.length() > 0) && StringUtils.isNumeric(retval))
169 retlval = Long.parseLong(retval);
172 for (OperatorType operator: operators)
174 String curOperandValue = evaluate(operands.get(i++), node, ctx);
177 retsval = retsval + curOperandValue;
180 if ((curOperandValue.length() > 0) && StringUtils.isNumeric(curOperandValue) )
182 retlval = retlval + Long.parseLong(curOperandValue);
191 retlval = retlval - Long.parseLong(curOperandValue);
194 retlval = retlval * Long.parseLong(curOperandValue);
197 retlval = retlval / Long.parseLong(curOperandValue);
203 catch (NumberFormatException e1)
205 throw new SvcLogicException("Illegal value in arithmetic expression", e1);
210 return("" + retlval);
221 private static String evalCompareExpression(SvcLogicBinaryExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
224 List<OperatorType> operators = expr.getOperators();
225 List<SvcLogicExpression> operands = expr.getOperands();
227 if ((operators.size() != 1) || (operands.size() != 2))
229 throw new SvcLogicException ("Invalid comparison expression : "+expr);
232 OperatorType operator = operators.get(0);
233 String op1Value = evaluate(operands.get(0), node, ctx);
234 String op2Value = evaluate(operands.get(1), node, ctx);
236 if ((StringUtils.isNotEmpty(op1Value) && StringUtils.isNumeric(op1Value) && StringUtils.isNotEmpty(op2Value) && StringUtils.isNumeric(op2Value)))
240 double op1dbl = Double.parseDouble(op1Value);
241 double op2dbl = Double.parseDouble(op2Value);
246 return(Boolean.toString(op1dbl == op2dbl));
248 return(Boolean.toString(op1dbl != op2dbl));
250 return(Boolean.toString(op1dbl < op2dbl));
252 return(Boolean.toString(op1dbl <= op2dbl));
254 return(Boolean.toString(op1dbl > op2dbl));
256 return(Boolean.toString(op1dbl >= op2dbl));
261 catch (NumberFormatException e)
263 throw new SvcLogicException("Caught exception trying to compare numeric values", e);
271 if (op1Value == null) {
273 } else if (op2Value == null ) {
276 compResult = op1Value.compareToIgnoreCase(op2Value);
282 return(Boolean.toString(compResult == 0));
284 return(Boolean.toString(compResult != 0));
286 return(Boolean.toString(compResult < 0));
288 return(Boolean.toString(compResult <= 0));
290 return(Boolean.toString(compResult > 0));
292 return(Boolean.toString(compResult >= 0));
300 private static String evalLogicExpression(SvcLogicBinaryExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
304 List<SvcLogicExpression> operands = expr.getOperands();
305 List<OperatorType> operators = expr.getOperators();
307 if (operands.size() != (operators.size()+1))
309 throw new SvcLogicException(INVALID_EXPRESSION_MSG+expr+")");
314 retval = Boolean.parseBoolean(evaluate(operands.get(0), node, ctx));
316 for (OperatorType operator : operators)
318 if (operator == OperatorType.andOp)
320 retval = retval && Boolean.parseBoolean(evaluate(operands.get(i++), node, ctx));
325 retval = retval || Boolean.parseBoolean(evaluate(operands.get(i++), node, ctx));
332 throw new SvcLogicException(INVALID_EXPRESSION_MSG+expr+")");
336 return(Boolean.toString(retval));
339 private static String evalFunctionCall(SvcLogicFunctionCall func, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
341 String funcName = func.getFunctionName();
342 List<SvcLogicExpression> operands = func.getOperands();
344 if ("length".equalsIgnoreCase(funcName))
347 if (operands.size() == 1)
349 String opValue = evaluate(operands.get(0), node, ctx);
350 return(""+opValue.length());
354 throw new SvcLogicException("Invalid call to length() function");
357 else if ("substr".equalsIgnoreCase(funcName))
359 if (operands.size() == 3)
361 String op1Value = evaluate(operands.get(0), node, ctx);
362 String op2Value = evaluate(operands.get(1), node, ctx);
363 String op3Value = evaluate(operands.get(2), node, ctx);
365 if (!StringUtils.isNumeric(op2Value) || !StringUtils.isNumeric(op3Value))
367 throw new SvcLogicException("Invalid arguments to substr() function");
372 return(op1Value.substring(Integer.parseInt(op2Value), Integer.parseInt(op3Value)));
376 throw new SvcLogicException("Caught exception trying to take substring", e);
382 throw new SvcLogicException("Invalid call to substr() function");
386 else if ("toUpperCase".equalsIgnoreCase(funcName))
388 if (operands.size() == 1)
390 String opValue = evaluate(operands.get(0), node, ctx);
391 if (opValue != null) {
392 return(opValue.toUpperCase());
399 throw new SvcLogicException("Invalid call to toUpperCase() function");
402 else if ("toLowerCase".equalsIgnoreCase(funcName))
404 if (operands.size() == 1)
406 String opValue = evaluate(operands.get(0), node, ctx);
407 if (opValue != null) {
408 return(opValue.toLowerCase());
415 throw new SvcLogicException("Invalid call to toLowerCase() function");
418 else if ("convertBase".equalsIgnoreCase(funcName)) {
421 String srcString = "";
423 if (operands.size() == 2)
426 srcString = evaluate(operands.get(0), node, ctx);
427 toBase = Integer.parseInt(evaluate(operands.get(1), node, ctx));
428 } else if (operands.size() == 3) {
430 srcString = evaluate(operands.get(0), node, ctx);
431 fromBase = Integer.parseInt(evaluate(operands.get(1), node, ctx));
432 toBase = Integer.parseInt(evaluate(operands.get(2), node, ctx));
434 throw new SvcLogicException("Invalid call to convertBase() function");
437 long srcValue = Long.parseLong(srcString, fromBase);
438 return(Long.toString(srcValue, toBase));
442 throw new SvcLogicException("Unrecognized function ("+funcName+")");
447 public static String evaluateAsKey(SvcLogicExpression expr, SvcLogicNode node,
448 SvcLogicContext ctx) throws SvcLogicException {
455 if (expr instanceof SvcLogicAtom) {
456 SvcLogicAtom atom = (SvcLogicAtom) expr;
458 AtomType atomType = atom.getAtomType();
459 StringBuffer varNameBuff = new StringBuffer();
462 return (atom.toString());
464 return("'"+atom.toString()+"'");
467 boolean needDot = false;
468 for (SvcLogicExpression term : atom.getOperands())
472 varNameBuff.append(".");
474 if (term instanceof SvcLogicVariableTerm)
476 SvcLogicVariableTerm vterm = (SvcLogicVariableTerm) term;
477 varNameBuff.append(vterm.getName());
478 if (vterm.numOperands() > 0)
480 varNameBuff.append("[");
481 varNameBuff.append(evaluate(vterm.getSubscript(), node, ctx));
482 varNameBuff.append("]");
488 varNameBuff.append(term.toString());
493 String varName = varNameBuff.toString();
494 LOG.debug("Evaluating context variable $"+varName);
495 String ctxValue = ctx.getAttribute(varName);
496 if (ctxValue == null)
500 if (StringUtils.isNumeric(ctxValue))
506 return("'"+ctxValue+"'");
513 } else if (expr instanceof SvcLogicBinaryExpression) {
514 SvcLogicBinaryExpression binExpr = (SvcLogicBinaryExpression) expr;
515 List<OperatorType> operators = binExpr.getOperators();
516 List<SvcLogicExpression> operands = binExpr.getOperands();
517 if (operators.isEmpty())
519 if (operands.size() == 1)
521 LOG.debug("SvcLogicBinaryExpression as no operator and one operand - evaluating its operand");
522 return(evaluateAsKey(operands.get(0), node, ctx));
526 if (operands.isEmpty())
528 LOG.error("SvcLogicBinaryExpression has no operators and no operands - evaluating value as null");
532 LOG.error("SvcLogicBinaryExpression has no operators and "+operands.size()+" operands - evaluating value as null");
537 StringBuffer sbuff = new StringBuffer();
538 sbuff.append(evaluateAsKey(operands.get(0), node, ctx));
540 for (OperatorType operator : operators)
543 sbuff.append(operator.toString());
545 sbuff.append(evaluateAsKey(operands.get(i++), node,ctx));
547 return(sbuff.toString());
549 else if (expr instanceof SvcLogicFunctionCall)
551 StringBuffer sbuff = new StringBuffer();
552 SvcLogicFunctionCall funcCall = (SvcLogicFunctionCall) expr;
553 sbuff.append(funcCall.getFunctionName());
555 boolean needComma = false;
556 for (SvcLogicExpression operand : funcCall.getOperands())
566 sbuff.append(evaluateAsKey(operand, node, ctx));
569 return(sbuff.toString());
573 throw new SvcLogicException("Unrecognized expression type ["+expr+"]");
577 public static String resolveVariableName(SvcLogicExpression atom, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
579 StringBuffer varNameBuff = new StringBuffer();
581 boolean needDot = false;
582 for (SvcLogicExpression term : atom.getOperands())
586 varNameBuff.append(".");
588 if (term instanceof SvcLogicVariableTerm)
590 SvcLogicVariableTerm vterm = (SvcLogicVariableTerm) term;
591 varNameBuff.append(vterm.getName());
592 if (vterm.numOperands() > 0)
594 varNameBuff.append("[");
595 varNameBuff.append(evaluate(vterm.getSubscript(), node, ctx));
596 varNameBuff.append("]");
601 varNameBuff.append(term.toString());
605 return(varNameBuff.toString());