Remove checkstyle warnings in policy/engine
[policy/engine.git] / ONAP-PDP / src / main / java / org / onap / policy / xacml / pdp / std / functions / FunctionDefinitionCustomRegexpMatch.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.xacml.pdp.std.functions;
22
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;
34
35 import java.util.List;
36
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39
40 /**
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.
45  *
46  *
47  * @version $Revision: 0.2 $
48  *
49  * @param <I> the java class for the data type of the function Input arguments
50  */
51 public class FunctionDefinitionCustomRegexpMatch<I> extends FunctionDefinitionBase<Boolean, I> {
52     private static Logger logger = FlexLogger.getLogger(FunctionDefinitionCustomRegexpMatch.class);
53
54     /**
55      * Constructor - need dataTypeArgs input because of java Generic type-erasure during compilation.
56      *
57      * @param idIn the identifier
58      * @param dataTypeArgsIn the data type
59      */
60     public FunctionDefinitionCustomRegexpMatch(final Identifier idIn, final DataType<I> dataTypeArgsIn) {
61         super(idIn, DataTypes.DT_BOOLEAN, dataTypeArgsIn, false);
62     }
63
64
65     @Override
66     public ExpressionResult evaluate(final EvaluationContext evaluationContext,
67             final List<FunctionArgument> arguments) {
68
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())));
73         }
74
75         // get the regular expression
76         final FunctionArgument regexpArgument = arguments.get(0);
77
78         final ConvertedArgument<String> convertedArgument =
79                 new ConvertedArgument<>(regexpArgument, DataTypes.DT_STRING, false);
80         if (!convertedArgument.isOk()) {
81             return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus()));
82         }
83
84         // String regexpValue = (String)regexpArgument.getValue().getValue();
85         String regexpValue = convertedArgument.getValue();
86
87
88         // now get the element to match
89         final FunctionArgument elementArgument = arguments.get(1);
90
91         final ConvertedArgument<I> convertedElement =
92                 new ConvertedArgument<>(elementArgument, this.getDataTypeArgs(), false);
93         if (!convertedElement.isOk()) {
94             return ExpressionResult.newError(getFunctionStatus(convertedElement.getStatus()));
95         }
96
97         final I elementValueObject = convertedElement.getValue();
98
99         String elementValueString;
100         try {
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();
107             }
108             return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
109                     this.getShortFunctionId() + " " + message));
110         }
111
112         // ConvertedArgument checks for null value, so do not need to do again here
113
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()));
118         //
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)) {
124                     return ER_TRUE;
125                 }
126             }
127         }
128         if (elementValueString.matches(regexpValue)) {
129             return ER_TRUE;
130         } else {
131             return ER_FALSE;
132         }
133
134     }
135 }