[POLICY-117] Resolve the Policy Critical issues
[policy/engine.git] / ECOMP-PDP / src / main / java / org / openecomp / policy / xacml / pdp / std / functions / FunctionDefinitionCustomRegexpMatch.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
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
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 package org.openecomp.policy.xacml.pdp.std.functions;
21
22
23 import java.util.List;
24
25 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
26 import org.openecomp.policy.common.logging.flexlogger.Logger;
27
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;
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 <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.
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
58          * @param dataTypeArgsIn
59          */
60         public FunctionDefinitionCustomRegexpMatch(Identifier idIn, DataType<I> dataTypeArgsIn) {
61                 super(idIn, DataTypes.DT_BOOLEAN, dataTypeArgsIn, false);
62         }
63
64
65         @Override
66         public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) {
67
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()) ));
71                 }
72                 
73                 // get the regular expression
74                 FunctionArgument regexpArgument = arguments.get(0);
75
76                 ConvertedArgument<String> convertedArgument = new ConvertedArgument<>(regexpArgument, DataTypes.DT_STRING, false);
77                 if ( ! convertedArgument.isOk()) {
78                         return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus()));
79                 }
80                 
81                 // String regexpValue = (String)regexpArgument.getValue().getValue();
82                 String regexpValue      = convertedArgument.getValue();
83
84                 
85                 // now get the element to match
86                 FunctionArgument elementArgument = arguments.get(1);
87                 
88                 ConvertedArgument<I> convertedElement = new ConvertedArgument<>(elementArgument, this.getDataTypeArgs(), false);
89                 if ( ! convertedElement.isOk()) {
90                         return ExpressionResult.newError(getFunctionStatus(convertedElement.getStatus()));
91                 }
92                 
93                 I elementValueObject = convertedElement.getValue();
94
95                 String elementValueString;
96                 try {
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();
103                         }
104                         return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message));
105                 }
106                 
107                 // ConvertedArgument checks for null value, so do not need to do again here
108                 
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()));
113                 // 
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)) {
119                                         return ER_TRUE;
120                                 }
121                         }
122                 }
123                 if (elementValueString.matches(regexpValue)) {
124                         return ER_TRUE;
125                 } else {
126                         return ER_FALSE;
127                 }
128
129         }
130 }