Utilize time extensions
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / ToscaPolicyTranslatorUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.xacml.application.common;
24
25 import com.att.research.xacml.api.Identifier;
26 import com.att.research.xacml.api.XACML3;
27 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
28 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
37 import org.apache.commons.lang3.StringUtils;
38
39 /**
40  * This class contains static methods of helper classes to convert TOSCA policies
41  * into XACML policies.
42  *
43  * @author pameladragosh
44  *
45  */
46 public final class ToscaPolicyTranslatorUtils {
47     private static final ObjectFactory factory = new ObjectFactory();
48
49     private ToscaPolicyTranslatorUtils() {
50         super();
51     }
52
53     /**
54      * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
55      * combination.
56      *
57      * @param <T> Incoming value could be any object
58      * @param function Function for the Match
59      * @param value Attribute value used
60      * @param datatype Datatype for attribute value and AttributeDesignator
61      * @param designatorId ID for the AttributeDesignator
62      * @param designatorCategory Category ID for the AttributeDesignator
63      * @return The MatchType object
64      */
65     public static <T> MatchType buildMatchTypeDesignator(Identifier function,
66             T value,
67             Identifier datatype,
68             Identifier designatorId,
69             Identifier designatorCategory) {
70         //
71         // Create the MatchType object and set its function
72         //
73         MatchType match = new MatchType();
74         match.setMatchId(function.stringValue());
75         //
76         // Add in the AttributeValue object
77         //
78         AttributeValueType valueType = new AttributeValueType();
79         valueType.setDataType(datatype.stringValue());
80         valueType.getContent().add(value);
81
82         match.setAttributeValue(valueType);
83         //
84         // Add in the AttributeDesignator object
85         //
86         AttributeDesignatorType designator = new AttributeDesignatorType();
87         designator.setAttributeId(designatorId.stringValue());
88         designator.setCategory(designatorCategory.stringValue());
89         designator.setDataType(datatype.stringValue());
90
91         match.setAttributeDesignator(designator);
92         //
93         // Done
94         //
95         return match;
96     }
97
98     /**
99      * Builds an AllOfType (AND) with one or more MatchType objects.
100      *
101      * @param matches A list of one or more MatchType
102      * @return The AllOf object
103      */
104     public static AllOfType buildAllOf(MatchType... matches) {
105         AllOfType allOf = new AllOfType();
106         for (MatchType match : matches) {
107             allOf.getMatch().add(match);
108         }
109         return allOf;
110     }
111
112     /**
113      * Takes start and end time interval and generates an ApplyType for it.
114      *
115      * @param start ISO8601 timestamp
116      * @param end ISO8601 timestamp
117      * @return ApplyType
118      */
119     public static ApplyType generateTimeInRange(String start, String end, boolean useRecurringFunction) {
120         if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
121             return null;
122         }
123
124         AttributeDesignatorType designator = new AttributeDesignatorType();
125         designator.setAttributeId(XACML3.ID_ENVIRONMENT_CURRENT_TIME.stringValue());
126         designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ENVIRONMENT.stringValue());
127         designator.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
128
129         AttributeValueType valueStart = new AttributeValueType();
130         valueStart.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
131         valueStart.getContent().add(start);
132
133         AttributeValueType valueEnd = new AttributeValueType();
134         valueEnd.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
135         valueEnd.getContent().add(end);
136
137
138         ApplyType applyOneAndOnly = new ApplyType();
139         applyOneAndOnly.setDescription("Unbag the current time");
140         applyOneAndOnly.setFunctionId(XACML3.ID_FUNCTION_TIME_ONE_AND_ONLY.stringValue());
141         applyOneAndOnly.getExpression().add(factory.createAttributeDesignator(designator));
142
143         ApplyType applyTimeInRange = new ApplyType();
144         applyTimeInRange.setDescription("return true if current time is in range.");
145         if (useRecurringFunction) {
146             applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RECURRING_RANGE.stringValue());
147         } else {
148             applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RANGE.stringValue());
149         }
150         applyTimeInRange.getExpression().add(factory.createApply(applyOneAndOnly));
151         applyTimeInRange.getExpression().add(factory.createAttributeValue(valueStart));
152         applyTimeInRange.getExpression().add(factory.createAttributeValue(valueEnd));
153
154         return applyTimeInRange;
155     }
156
157     /**
158      * Parses an integer value from the string.
159      *
160      * @param strInteger String representation of integer
161      * @return Integer object
162      */
163     public static Integer parseInteger(String strInteger) {
164         Integer theInt = null;
165         try {
166             theInt = Integer.parseInt(strInteger);
167         } catch (NumberFormatException e) {
168             try {
169                 Double dblLimit = Double.parseDouble(strInteger);
170                 theInt = dblLimit.intValue();
171             } catch (NumberFormatException e1) {
172                 return null;
173             }
174         }
175         return theInt;
176     }
177
178     /**
179      * For a given MatchType or AnyOfType, builds it and appends it into the
180      * AnyOfType.
181      *
182      * @param anyOf AnyOfType - will create if null
183      * @param type MatchType or AnyOfType
184      * @return returns the given anyOf or new AnyTypeOf if null
185      */
186     public static AnyOfType buildAndAppendAllof(AnyOfType anyOf, Object type) {
187         if (type instanceof MatchType) {
188             AllOfType allOf = new AllOfType();
189             allOf.getMatch().add((MatchType) type);
190             if (anyOf == null) {
191                 anyOf = new AnyOfType();
192             }
193             anyOf.getAllOf().add(allOf);
194         } else if (type instanceof AllOfType) {
195             if (anyOf == null) {
196                 anyOf = new AnyOfType();
197             }
198             anyOf.getAllOf().add((AllOfType) type);
199         }
200
201         return anyOf;
202     }
203
204     /**
205      * buildAndAppendTarget - adds in the potential object into TargetType.
206      *
207      * @param target TargetType - must exist
208      * @param object AnyOfType or MatchType
209      * @return TargetType
210      */
211     public static TargetType buildAndAppendTarget(TargetType target, Object object) {
212         if (object instanceof AnyOfType) {
213             target.getAnyOf().add((AnyOfType) object);
214         } else if (object instanceof MatchType) {
215             AllOfType allOf = new AllOfType();
216             allOf.getMatch().add((MatchType) object);
217             AnyOfType anyOf = new AnyOfType();
218             anyOf.getAllOf().add(allOf);
219             target.getAnyOf().add(anyOf);
220         }
221         return target;
222     }
223
224     /**
225      * For an existing ConditionType, this method adds in a check for a variable. You must specify
226      * the function that compares the existing ConditionType's expression against the Variable.
227      *
228      * @param condition Existing ConditionType to use
229      * @param variable VariableReferenceType to use
230      * @param functionId XACML 3.0 identifier for the function
231      * @return a new ConditionType
232      */
233     public static ConditionType addVariableToCondition(ConditionType condition, VariableReferenceType variable,
234             Identifier functionId) {
235         ApplyType applyFunction = new ApplyType();
236         applyFunction.setFunctionId(functionId.stringValue());
237         applyFunction.getExpression().add(condition.getExpression());
238         applyFunction.getExpression().add(factory.createVariableReference(variable));
239         ConditionType newCondition = new ConditionType();
240         newCondition.setExpression(factory.createApply(applyFunction));
241         return newCondition;
242     }
243 }