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 (";
47 private static final String EXPRESSION_DEBUG_PATTERN = "Expression: ({}) resolves to: (${}) which has the value: ({})";
49 public static String evaluate(SvcLogicExpression expr, SvcLogicNode node,
50 SvcLogicContext ctx) throws SvcLogicException {
55 if (expr instanceof SvcLogicAtom) {
56 SvcLogicAtom atom = (SvcLogicAtom) expr;
58 AtomType atomType = atom.getAtomType();
62 return (atom.toString());
66 String varName = resolveVariableName(atom, node, ctx);
68 if (atomType == AtomType.CONTEXT_VAR)
70 String varValue = ctx.getAttribute(varName);
71 if (varValue == null) {
72 LOG.trace("Context variable: ($"+varName+") unset - treating as empty string");
75 LOG.trace(EXPRESSION_DEBUG_PATTERN, expr, varName, varValue);
79 SvcLogicExpression parm = node.getParameter(varName);
81 String value = evaluate(parm, node, ctx);
82 LOG.trace(EXPRESSION_DEBUG_PATTERN, expr, varName, value);
86 LOG.trace(EXPRESSION_DEBUG_PATTERN, expr, varName, varName);
93 } else if (expr instanceof SvcLogicBinaryExpression) {
94 SvcLogicBinaryExpression binExpr = (SvcLogicBinaryExpression) expr;
95 List<OperatorType> operators = binExpr.getOperators();
96 if (operators.isEmpty())
98 List<SvcLogicExpression> operands = binExpr.getOperands();
99 if (operands.size() == 1)
101 return(evaluate(operands.get(0), node, ctx));
105 if (operands.isEmpty())
107 LOG.error("SvcLogicBinaryExpression has no operators and no operands - evaluating value as null");
111 LOG.error("SvcLogicBinaryExpression has no operators and "+operands.size()+" operands - evaluating value as null");
116 switch (operators.get(0)) {
121 return(evalArithExpression(binExpr, node, ctx));
128 return (evalCompareExpression(binExpr, node, ctx));
131 return(evalLogicExpression(binExpr, node, ctx));
137 else if (expr instanceof SvcLogicFunctionCall)
139 return(evalFunctionCall((SvcLogicFunctionCall)expr, node, ctx));
143 throw new SvcLogicException("Unrecognized expression type ["+expr+"]");
147 private static String evalArithExpression(SvcLogicBinaryExpression binExpr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
148 List<SvcLogicExpression> operands = binExpr.getOperands();
149 List<OperatorType> operators = binExpr.getOperators();
150 if (operands.size() != (operators.size()+1))
152 throw new SvcLogicException(INVALID_EXPRESSION_MSG+binExpr+")");
154 String retval = evaluate(operands.get(0), node, ctx);
155 String retsval = retval;
157 boolean valueIsLong = false;
163 if ((retval.length() > 0) && StringUtils.isNumeric(retval))
165 retlval = Long.parseLong(retval);
168 for (OperatorType operator: operators)
170 String curOperandValue = evaluate(operands.get(i++), node, ctx);
173 retsval = retsval + curOperandValue;
176 if ((curOperandValue.length() > 0) && StringUtils.isNumeric(curOperandValue) )
178 retlval = retlval + Long.parseLong(curOperandValue);
187 retlval = retlval - Long.parseLong(curOperandValue);
190 retlval = retlval * Long.parseLong(curOperandValue);
193 retlval = retlval / Long.parseLong(curOperandValue);
199 catch (NumberFormatException e1)
201 throw new SvcLogicException("Illegal value in arithmetic expression", e1);
206 return("" + retlval);
217 private static String evalCompareExpression(SvcLogicBinaryExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
220 List<OperatorType> operators = expr.getOperators();
221 List<SvcLogicExpression> operands = expr.getOperands();
223 if ((operators.size() != 1) || (operands.size() != 2))
225 throw new SvcLogicException ("Invalid comparison expression : "+expr);
228 OperatorType operator = operators.get(0);
229 String op1Value = evaluate(operands.get(0), node, ctx);
230 String op2Value = evaluate(operands.get(1), node, ctx);
232 if ((StringUtils.isNotEmpty(op1Value) && StringUtils.isNumeric(op1Value) && StringUtils.isNotEmpty(op2Value) && StringUtils.isNumeric(op2Value)))
236 double op1dbl = Double.parseDouble(op1Value);
237 double op2dbl = Double.parseDouble(op2Value);
242 return(Boolean.toString(op1dbl == op2dbl));
244 return(Boolean.toString(op1dbl != op2dbl));
246 return(Boolean.toString(op1dbl < op2dbl));
248 return(Boolean.toString(op1dbl <= op2dbl));
250 return(Boolean.toString(op1dbl > op2dbl));
252 return(Boolean.toString(op1dbl >= op2dbl));
257 catch (NumberFormatException e)
259 throw new SvcLogicException("Caught exception trying to compare numeric values", e);
267 if (op1Value == null) {
269 } else if (op2Value == null ) {
272 compResult = op1Value.compareToIgnoreCase(op2Value);
278 return(Boolean.toString(compResult == 0));
280 return(Boolean.toString(compResult != 0));
282 return(Boolean.toString(compResult < 0));
284 return(Boolean.toString(compResult <= 0));
286 return(Boolean.toString(compResult > 0));
288 return(Boolean.toString(compResult >= 0));
296 private static String evalLogicExpression(SvcLogicBinaryExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
300 List<SvcLogicExpression> operands = expr.getOperands();
301 List<OperatorType> operators = expr.getOperators();
303 if (operands.size() != (operators.size()+1))
305 throw new SvcLogicException(INVALID_EXPRESSION_MSG+expr+")");
310 retval = Boolean.parseBoolean(evaluate(operands.get(0), node, ctx));
312 for (OperatorType operator : operators)
314 if (operator == OperatorType.andOp)
316 retval = retval && Boolean.parseBoolean(evaluate(operands.get(i++), node, ctx));
321 retval = retval || Boolean.parseBoolean(evaluate(operands.get(i++), node, ctx));
328 throw new SvcLogicException(INVALID_EXPRESSION_MSG+expr+")");
332 return(Boolean.toString(retval));
335 private static String evalFunctionCall(SvcLogicFunctionCall func, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
337 String funcName = func.getFunctionName();
338 List<SvcLogicExpression> operands = func.getOperands();
340 if ("length".equalsIgnoreCase(funcName))
343 if (operands.size() == 1)
345 String opValue = evaluate(operands.get(0), node, ctx);
346 return(""+opValue.length());
350 throw new SvcLogicException("Invalid call to length() function");
353 else if ("substr".equalsIgnoreCase(funcName))
355 if (operands.size() == 3)
357 String op1Value = evaluate(operands.get(0), node, ctx);
358 String op2Value = evaluate(operands.get(1), node, ctx);
359 String op3Value = evaluate(operands.get(2), node, ctx);
361 if (!StringUtils.isNumeric(op2Value) || !StringUtils.isNumeric(op3Value))
363 throw new SvcLogicException("Invalid arguments to substr() function");
368 return(op1Value.substring(Integer.parseInt(op2Value), Integer.parseInt(op3Value)));
372 throw new SvcLogicException("Caught exception trying to take substring", e);
378 throw new SvcLogicException("Invalid call to substr() function");
382 else if ("toUpperCase".equalsIgnoreCase(funcName))
384 if (operands.size() == 1)
386 String opValue = evaluate(operands.get(0), node, ctx);
387 if (opValue != null) {
388 return(opValue.toUpperCase());
395 throw new SvcLogicException("Invalid call to toUpperCase() function");
398 else if ("toLowerCase".equalsIgnoreCase(funcName))
400 if (operands.size() == 1)
402 String opValue = evaluate(operands.get(0), node, ctx);
403 if (opValue != null) {
404 return(opValue.toLowerCase());
411 throw new SvcLogicException("Invalid call to toLowerCase() function");
414 else if ("convertBase".equalsIgnoreCase(funcName)) {
417 String srcString = "";
419 if (operands.size() == 2)
422 srcString = evaluate(operands.get(0), node, ctx);
423 toBase = Integer.parseInt(evaluate(operands.get(1), node, ctx));
424 } else if (operands.size() == 3) {
426 srcString = evaluate(operands.get(0), node, ctx);
427 fromBase = Integer.parseInt(evaluate(operands.get(1), node, ctx));
428 toBase = Integer.parseInt(evaluate(operands.get(2), node, ctx));
430 throw new SvcLogicException("Invalid call to convertBase() function");
433 long srcValue = Long.parseLong(srcString, fromBase);
434 return(Long.toString(srcValue, toBase));
438 throw new SvcLogicException("Unrecognized function ("+funcName+")");
443 public static String evaluateAsKey(SvcLogicExpression expr, SvcLogicNode node,
444 SvcLogicContext ctx) throws SvcLogicException {
451 if (expr instanceof SvcLogicAtom) {
452 SvcLogicAtom atom = (SvcLogicAtom) expr;
454 AtomType atomType = atom.getAtomType();
455 StringBuffer varNameBuff = new StringBuffer();
458 return (atom.toString());
460 return("'"+atom.toString()+"'");
463 boolean needDot = false;
464 for (SvcLogicExpression term : atom.getOperands())
468 varNameBuff.append(".");
470 if (term instanceof SvcLogicVariableTerm)
472 SvcLogicVariableTerm vterm = (SvcLogicVariableTerm) term;
473 varNameBuff.append(vterm.getName());
474 if (vterm.numOperands() > 0)
476 varNameBuff.append("[");
477 varNameBuff.append(evaluate(vterm.getSubscript(), node, ctx));
478 varNameBuff.append("]");
484 varNameBuff.append(term.toString());
489 String varName = varNameBuff.toString();
490 String ctxValue = ctx.getAttribute(varName);
491 if (ctxValue == null)
495 if (StringUtils.isNumeric(ctxValue))
501 return("'"+ctxValue+"'");
508 } else if (expr instanceof SvcLogicBinaryExpression) {
509 SvcLogicBinaryExpression binExpr = (SvcLogicBinaryExpression) expr;
510 List<OperatorType> operators = binExpr.getOperators();
511 List<SvcLogicExpression> operands = binExpr.getOperands();
512 if (operators.isEmpty())
514 if (operands.size() == 1)
516 LOG.debug("SvcLogicBinaryExpression as no operator and one operand - evaluating its operand");
517 return(evaluateAsKey(operands.get(0), node, ctx));
521 if (operands.isEmpty())
523 LOG.error("SvcLogicBinaryExpression has no operators and no operands - evaluating value as null");
527 LOG.error("SvcLogicBinaryExpression has no operators and "+operands.size()+" operands - evaluating value as null");
532 StringBuffer sbuff = new StringBuffer();
533 sbuff.append(evaluateAsKey(operands.get(0), node, ctx));
535 for (OperatorType operator : operators)
538 sbuff.append(operator.toString());
540 sbuff.append(evaluateAsKey(operands.get(i++), node,ctx));
542 return(sbuff.toString());
544 else if (expr instanceof SvcLogicFunctionCall)
546 StringBuffer sbuff = new StringBuffer();
547 SvcLogicFunctionCall funcCall = (SvcLogicFunctionCall) expr;
548 sbuff.append(funcCall.getFunctionName());
550 boolean needComma = false;
551 for (SvcLogicExpression operand : funcCall.getOperands())
561 sbuff.append(evaluateAsKey(operand, node, ctx));
564 return(sbuff.toString());
568 throw new SvcLogicException("Unrecognized expression type ["+expr+"]");
572 public static String resolveVariableName(SvcLogicExpression atom, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException
574 StringBuffer varNameBuff = new StringBuffer();
576 boolean needDot = false;
577 for (SvcLogicExpression term : atom.getOperands())
581 varNameBuff.append(".");
583 if (term instanceof SvcLogicVariableTerm)
585 SvcLogicVariableTerm vterm = (SvcLogicVariableTerm) term;
586 varNameBuff.append(vterm.getName());
587 if (vterm.numOperands() > 0)
589 varNameBuff.append("[");
590 varNameBuff.append(evaluate(vterm.getSubscript(), node, ctx));
591 varNameBuff.append("]");
596 varNameBuff.append(term.toString());
600 return(varNameBuff.toString());