2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.MatchType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
35 import org.apache.commons.lang3.StringUtils;
38 * This class contains static methods of helper classes to convert TOSCA policies
39 * into XACML policies.
41 * @author pameladragosh
44 public final class ToscaPolicyTranslatorUtils {
45 private static final ObjectFactory factory = new ObjectFactory();
47 private ToscaPolicyTranslatorUtils() {
52 * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
55 * @param <T> Incoming value could be any object
56 * @param function Function for the Match
57 * @param value Attribute value used
58 * @param datatype Datatype for attribute value and AttributeDesignator
59 * @param designatorId ID for the AttributeDesignator
60 * @param designatorCategory Category ID for the AttributeDesignator
61 * @return The MatchType object
63 public static <T> MatchType buildMatchTypeDesignator(Identifier function,
66 Identifier designatorId,
67 Identifier designatorCategory) {
69 // Create the MatchType object and set its function
71 MatchType match = new MatchType();
72 match.setMatchId(function.stringValue());
74 // Add in the AttributeValue object
76 AttributeValueType valueType = new AttributeValueType();
77 valueType.setDataType(datatype.stringValue());
78 valueType.getContent().add(value);
80 match.setAttributeValue(valueType);
82 // Add in the AttributeDesignator object
84 AttributeDesignatorType designator = new AttributeDesignatorType();
85 designator.setAttributeId(designatorId.stringValue());
86 designator.setCategory(designatorCategory.stringValue());
87 designator.setDataType(datatype.stringValue());
89 match.setAttributeDesignator(designator);
97 * Builds an AllOfType (AND) with one or more MatchType objects.
99 * @param matches A list of one or more MatchType
100 * @return The AllOf object
102 public static AllOfType buildAllOf(MatchType... matches) {
103 AllOfType allOf = new AllOfType();
104 for (MatchType match : matches) {
105 allOf.getMatch().add(match);
111 * Takes start and end time interval and generates an ApplyType for it.
113 * @param start ISO8601 timestamp
114 * @param end ISO8601 timestamp
117 public static ApplyType generateTimeInRange(String start, String end) {
118 if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
122 AttributeDesignatorType designator = new AttributeDesignatorType();
123 designator.setAttributeId(XACML3.ID_ENVIRONMENT_CURRENT_TIME.stringValue());
124 designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ENVIRONMENT.stringValue());
125 designator.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
127 AttributeValueType valueStart = new AttributeValueType();
128 valueStart.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
129 valueStart.getContent().add(start);
131 AttributeValueType valueEnd = new AttributeValueType();
132 valueEnd.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
133 valueEnd.getContent().add(end);
136 ApplyType applyOneAndOnly = new ApplyType();
137 applyOneAndOnly.setDescription("Unbag the current time");
138 applyOneAndOnly.setFunctionId(XACML3.ID_FUNCTION_TIME_ONE_AND_ONLY.stringValue());
139 applyOneAndOnly.getExpression().add(factory.createAttributeDesignator(designator));
141 ApplyType applyTimeInRange = new ApplyType();
142 applyTimeInRange.setDescription("return true if current time is in range.");
143 applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RANGE.stringValue());
144 applyTimeInRange.getExpression().add(factory.createApply(applyOneAndOnly));
145 applyTimeInRange.getExpression().add(factory.createAttributeValue(valueStart));
146 applyTimeInRange.getExpression().add(factory.createAttributeValue(valueEnd));
148 return applyTimeInRange;
152 * Parses an integer value from the string.
154 * @param strInteger String representation of integer
155 * @return Integer object
157 public static Integer parseInteger(String strInteger) {
158 Integer theInt = null;
160 theInt = Integer.parseInt(strInteger);
161 } catch (NumberFormatException e) {
163 Double dblLimit = Double.parseDouble(strInteger);
164 theInt = dblLimit.intValue();
165 } catch (NumberFormatException e1) {
173 * For a given MatchType or AnyOfType, builds it and appends it into the
176 * @param anyOf AnyOfType - will create if null
177 * @param type MatchType or AnyOfType
178 * @return returns the given anyOf or new AnyTypeOf if null
180 public static AnyOfType buildAndAppendAllof(AnyOfType anyOf, Object type) {
181 if (type instanceof MatchType) {
182 AllOfType allOf = new AllOfType();
183 allOf.getMatch().add((MatchType) type);
185 anyOf = new AnyOfType();
187 anyOf.getAllOf().add(allOf);
188 } else if (type instanceof AllOfType) {
190 anyOf = new AnyOfType();
192 anyOf.getAllOf().add((AllOfType) type);
199 * buildAndAppendTarget - adds in the potential object into TargetType.
201 * @param target TargetType - must exist
202 * @param object AnyOfType or MatchType
205 public static TargetType buildAndAppendTarget(TargetType target, Object object) {
206 if (object instanceof AnyOfType) {
207 target.getAnyOf().add((AnyOfType) object);
208 } else if (object instanceof MatchType) {
209 AllOfType allOf = new AllOfType();
210 allOf.getMatch().add((MatchType) object);
211 AnyOfType anyOf = new AnyOfType();
212 anyOf.getAllOf().add(allOf);
213 target.getAnyOf().add(anyOf);