Initial OpenECOMP policy/engine commit
[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 com.att.research.xacml.api.DataType;
26 import com.att.research.xacml.api.DataTypeException;
27 import com.att.research.xacml.api.Identifier;
28 import com.att.research.xacml.std.StdStatus;
29 import com.att.research.xacml.std.StdStatusCode;
30 import com.att.research.xacml.std.datatypes.DataTypes;
31 import com.att.research.xacmlatt.pdp.eval.EvaluationContext;
32 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
33 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
34 import com.att.research.xacmlatt.pdp.std.functions.ConvertedArgument;
35 import com.att.research.xacmlatt.pdp.std.functions.FunctionDefinitionBase;
36
37 /**
38  * FunctionDefinitionCustomRegexMatch implements {@link com.att.research.xacmlatt.pdp.policy.FunctionDefinition} to
39  * implement the custom 'type'-regex-match predicates as functions taking two arguments, the first of <code>String</code>,
40  * and the second of the type for that specific predicate as a regular expression,
41  * and returning a <code>Boolean</code> for whether the regular expression matches the string representation of the first argument.
42  * 
43  * 
44  * @version $Revision: 0.1 $
45  * 
46  * @param <I> the java class for the data type of the function Input arguments
47  */
48 public class FunctionDefinitionCustomRegexpMatch<I> extends FunctionDefinitionBase<Boolean, I> {
49
50         
51         /**
52          * Constructor - need dataTypeArgs input because of java Generic type-erasure during compilation.
53          * 
54          * @param idIn
55          * @param dataTypeArgsIn
56          */
57         public FunctionDefinitionCustomRegexpMatch(Identifier idIn, DataType<I> dataTypeArgsIn) {
58                 super(idIn, DataTypes.DT_BOOLEAN, dataTypeArgsIn, false);
59         }
60
61
62         @Override
63         public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) {
64
65                 if (arguments == null || arguments.size() != 2) {
66                         return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " + 
67                                         ((arguments == null) ? "null" : arguments.size()) ));
68                 }
69                 
70                 // get the regular expression
71                 FunctionArgument regexpArgument = arguments.get(0);
72
73                 ConvertedArgument<String> convertedArgument = new ConvertedArgument<String>(regexpArgument, DataTypes.DT_STRING, false);
74                 if ( ! convertedArgument.isOk()) {
75                         return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus()));
76                 }
77                 
78                 // String regexpValue = (String)regexpArgument.getValue().getValue();
79                 String regexpValue      = convertedArgument.getValue();
80
81                 
82                 // now get the element to match
83                 FunctionArgument elementArgument = arguments.get(1);
84                 
85                 ConvertedArgument<I> convertedElement = new ConvertedArgument<I>(elementArgument, this.getDataTypeArgs(), false);
86                 if ( ! convertedElement.isOk()) {
87                         return ExpressionResult.newError(getFunctionStatus(convertedElement.getStatus()));
88                 }
89                 
90                 I elementValueObject = convertedElement.getValue();
91
92                 String elementValueString;
93                 try {
94                         elementValueString = this.getDataTypeArgs().toStringValue(elementValueObject);
95                 } catch (DataTypeException e) {
96                         String message = e.getMessage();
97                         if (e.getCause() != null) {
98                                 message = e.getCause().getMessage();
99                         }
100                         return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message));
101                 }
102                 
103                 // ConvertedArgument checks for null value, so do not need to do again here
104                 
105                 // Adding this code will Change the Functionality which allows to retrieve Multiple-policy using single request.
106                 elementValueString = elementValueString + regexpValue ;
107                 regexpValue = elementValueString.substring(0,(elementValueString.length()- regexpValue.length()));
108                 elementValueString = elementValueString.substring(regexpValue.length(),(elementValueString.length()));
109                 // 
110                 
111                 if (elementValueString.matches(regexpValue)) {
112                         return ER_TRUE;
113                 } else {
114                         return ER_FALSE;
115                 }
116
117         }
118
119
120         
121         
122
123 }