2 * ============LICENSE_START=======================================================
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
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.xacml.application.common;
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;
40 * This class contains static methods of helper classes to convert TOSCA policies
41 * into XACML policies.
43 * @author pameladragosh
46 public final class ToscaPolicyTranslatorUtils {
47 private static final ObjectFactory factory = new ObjectFactory();
49 private ToscaPolicyTranslatorUtils() {
54 * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
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
65 public static <T> MatchType buildMatchTypeDesignator(Identifier function,
68 Identifier designatorId,
69 Identifier designatorCategory) {
71 // Create the MatchType object and set its function
73 MatchType match = new MatchType();
74 match.setMatchId(function.stringValue());
76 // Add in the AttributeValue object
78 AttributeValueType valueType = new AttributeValueType();
79 valueType.setDataType(datatype.stringValue());
80 valueType.getContent().add(value);
82 match.setAttributeValue(valueType);
84 // Add in the AttributeDesignator object
86 AttributeDesignatorType designator = new AttributeDesignatorType();
87 designator.setAttributeId(designatorId.stringValue());
88 designator.setCategory(designatorCategory.stringValue());
89 designator.setDataType(datatype.stringValue());
91 match.setAttributeDesignator(designator);
99 * Builds an AllOfType (AND) with one or more MatchType objects.
101 * @param matches A list of one or more MatchType
102 * @return The AllOf object
104 public static AllOfType buildAllOf(MatchType... matches) {
105 AllOfType allOf = new AllOfType();
106 for (MatchType match : matches) {
107 allOf.getMatch().add(match);
113 * Takes start and end time interval and generates an ApplyType for it.
115 * @param start ISO8601 timestamp
116 * @param end ISO8601 timestamp
119 public static ApplyType generateTimeInRange(String start, String end, boolean useRecurringFunction) {
120 if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
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());
129 AttributeValueType valueStart = new AttributeValueType();
130 valueStart.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
131 valueStart.getContent().add(start);
133 AttributeValueType valueEnd = new AttributeValueType();
134 valueEnd.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
135 valueEnd.getContent().add(end);
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));
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());
148 applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RANGE.stringValue());
150 applyTimeInRange.getExpression().add(factory.createApply(applyOneAndOnly));
151 applyTimeInRange.getExpression().add(factory.createAttributeValue(valueStart));
152 applyTimeInRange.getExpression().add(factory.createAttributeValue(valueEnd));
154 return applyTimeInRange;
158 * Parses an integer value from the string.
160 * @param strInteger String representation of integer
161 * @return Integer object
163 public static Integer parseInteger(String strInteger) {
164 Integer theInt = null;
166 theInt = Integer.parseInt(strInteger);
167 } catch (NumberFormatException e) {
169 Double dblLimit = Double.parseDouble(strInteger);
170 theInt = dblLimit.intValue();
171 } catch (NumberFormatException e1) {
179 * For a given MatchType or AnyOfType, builds it and appends it into the
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
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);
191 anyOf = new AnyOfType();
193 anyOf.getAllOf().add(allOf);
194 } else if (type instanceof AllOfType) {
196 anyOf = new AnyOfType();
198 anyOf.getAllOf().add((AllOfType) type);
205 * buildAndAppendTarget - adds in the potential object into TargetType.
207 * @param target TargetType - must exist
208 * @param object AnyOfType or MatchType
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);
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.
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
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));