2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.xacml.pdp.std.functions;
23 import com.att.research.xacml.api.DataType;
24 import com.att.research.xacml.api.DataTypeException;
25 import com.att.research.xacml.api.Identifier;
26 import com.att.research.xacml.std.StdStatus;
27 import com.att.research.xacml.std.StdStatusCode;
28 import com.att.research.xacml.std.datatypes.DataTypes;
29 import com.att.research.xacmlatt.pdp.eval.EvaluationContext;
30 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
31 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
32 import com.att.research.xacmlatt.pdp.std.functions.ConvertedArgument;
33 import com.att.research.xacmlatt.pdp.std.functions.FunctionDefinitionBase;
35 import java.util.List;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
41 * FunctionDefinitionCustomRegexMatch implements {@link com.att.research.xacmlatt.pdp.policy.FunctionDefinition} to
42 * implement the custom 'type'-regex-match predicates as functions taking two arguments, the first of
43 * <code>String</code>, and the second of the type for that specific predicate as a regular expression, and returning a
44 * <code>Boolean</code> for whether the regular expression matches the string representation of the first argument.
47 * @version $Revision: 0.2 $
49 * @param <I> the java class for the data type of the function Input arguments
51 public class FunctionDefinitionCustomRegexpMatch<I> extends FunctionDefinitionBase<Boolean, I> {
52 private static Logger logger = FlexLogger.getLogger(FunctionDefinitionCustomRegexpMatch.class);
55 * Constructor - need dataTypeArgs input because of java Generic type-erasure during compilation.
57 * @param idIn the identifier
58 * @param dataTypeArgsIn the data type
60 public FunctionDefinitionCustomRegexpMatch(final Identifier idIn, final DataType<I> dataTypeArgsIn) {
61 super(idIn, DataTypes.DT_BOOLEAN, dataTypeArgsIn, false);
66 public ExpressionResult evaluate(final EvaluationContext evaluationContext,
67 final List<FunctionArgument> arguments) {
69 if (arguments == null || arguments.size() != 2) {
70 return ExpressionResult
71 .newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId()
72 + " Expected 2 arguments, got " + ((arguments == null) ? "null" : arguments.size())));
75 // get the regular expression
76 final FunctionArgument regexpArgument = arguments.get(0);
78 final ConvertedArgument<String> convertedArgument =
79 new ConvertedArgument<>(regexpArgument, DataTypes.DT_STRING, false);
80 if (!convertedArgument.isOk()) {
81 return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus()));
84 // String regexpValue = (String)regexpArgument.getValue().getValue();
85 String regexpValue = convertedArgument.getValue();
88 // now get the element to match
89 final FunctionArgument elementArgument = arguments.get(1);
91 final ConvertedArgument<I> convertedElement =
92 new ConvertedArgument<>(elementArgument, this.getDataTypeArgs(), false);
93 if (!convertedElement.isOk()) {
94 return ExpressionResult.newError(getFunctionStatus(convertedElement.getStatus()));
97 final I elementValueObject = convertedElement.getValue();
99 String elementValueString;
101 elementValueString = this.getDataTypeArgs().toStringValue(elementValueObject);
102 } catch (final DataTypeException e) {
103 logger.error(e.getMessage() + e);
104 String message = e.getMessage();
105 if (e.getCause() != null) {
106 message = e.getCause().getMessage();
108 return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
109 this.getShortFunctionId() + " " + message));
112 // ConvertedArgument checks for null value, so do not need to do again here
114 // Adding this code will Change the Functionality which allows to retrieve Multiple-policy using single request.
115 elementValueString = elementValueString + regexpValue;
116 regexpValue = elementValueString.substring(0, (elementValueString.length() - regexpValue.length()));
117 elementValueString = elementValueString.substring(regexpValue.length(), (elementValueString.length()));
119 // Supporting multiple values in the element and be able to query them.
120 if (elementValueString.contains(",")) {
121 final String[] elements = elementValueString.split(",");
122 for (int i = 0; i < elements.length; i++) {
123 if (elements[i].trim().matches(regexpValue)) {
128 if (elementValueString.matches(regexpValue)) {