2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.xacml.pdp.std.functions;
23 import java.util.List;
25 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
28 import com.att.research.xacml.api.DataType;
29 import com.att.research.xacml.api.DataTypeException;
30 import com.att.research.xacml.api.Identifier;
31 import com.att.research.xacml.std.StdStatus;
32 import com.att.research.xacml.std.StdStatusCode;
33 import com.att.research.xacml.std.datatypes.DataTypes;
34 import com.att.research.xacmlatt.pdp.eval.EvaluationContext;
35 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
36 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
37 import com.att.research.xacmlatt.pdp.std.functions.ConvertedArgument;
38 import com.att.research.xacmlatt.pdp.std.functions.FunctionDefinitionBase;
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 <code>String</code>,
43 * and the second of the type for that specific predicate as a regular expression,
44 * and returning a <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.
58 * @param dataTypeArgsIn
60 public FunctionDefinitionCustomRegexpMatch(Identifier idIn, DataType<I> dataTypeArgsIn) {
61 super(idIn, DataTypes.DT_BOOLEAN, dataTypeArgsIn, false);
66 public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) {
68 if (arguments == null || arguments.size() != 2) {
69 return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " +
70 ((arguments == null) ? "null" : arguments.size()) ));
73 // get the regular expression
74 FunctionArgument regexpArgument = arguments.get(0);
76 ConvertedArgument<String> convertedArgument = new ConvertedArgument<>(regexpArgument, DataTypes.DT_STRING, false);
77 if ( ! convertedArgument.isOk()) {
78 return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus()));
81 // String regexpValue = (String)regexpArgument.getValue().getValue();
82 String regexpValue = convertedArgument.getValue();
85 // now get the element to match
86 FunctionArgument elementArgument = arguments.get(1);
88 ConvertedArgument<I> convertedElement = new ConvertedArgument<>(elementArgument, this.getDataTypeArgs(), false);
89 if ( ! convertedElement.isOk()) {
90 return ExpressionResult.newError(getFunctionStatus(convertedElement.getStatus()));
93 I elementValueObject = convertedElement.getValue();
95 String elementValueString;
97 elementValueString = this.getDataTypeArgs().toStringValue(elementValueObject);
98 } catch (DataTypeException e) {
99 logger.error(e.getMessage() +e);
100 String message = e.getMessage();
101 if (e.getCause() != null) {
102 message = e.getCause().getMessage();
104 return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message));
107 // ConvertedArgument checks for null value, so do not need to do again here
109 // Adding this code will Change the Functionality which allows to retrieve Multiple-policy using single request.
110 elementValueString = elementValueString + regexpValue ;
111 regexpValue = elementValueString.substring(0,(elementValueString.length()- regexpValue.length()));
112 elementValueString = elementValueString.substring(regexpValue.length(),(elementValueString.length()));
114 // Supporting multiple values in the element and be able to query them.
115 if(elementValueString.contains(",")){
116 String[] elements = elementValueString.split(",");
117 for(int i=0; i<elements.length; i++){
118 if(elements[i].trim().matches(regexpValue)) {
123 if (elementValueString.matches(regexpValue)) {