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.ApplyType;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
33 import org.apache.commons.lang3.StringUtils;
36 * This class contains static methods of helper classes to convert TOSCA policies
37 * into XACML policies.
39 * @author pameladragosh
42 public final class ToscaPolicyTranslatorUtils {
43 private static final ObjectFactory factory = new ObjectFactory();
45 private ToscaPolicyTranslatorUtils() {
50 * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
53 * @param <T> Incoming value could be any object
54 * @param function Function for the Match
55 * @param value Attribute value used
56 * @param datatype Datatype for attribute value and AttributeDesignator
57 * @param designatorId ID for the AttributeDesignator
58 * @param designatorCategory Category ID for the AttributeDesignator
59 * @return The MatchType object
61 public static <T> MatchType buildMatchTypeDesignator(Identifier function,
64 Identifier designatorId,
65 Identifier designatorCategory) {
67 // Create the MatchType object and set its function
69 MatchType match = new MatchType();
70 match.setMatchId(function.stringValue());
72 // Add in the AttributeValue object
74 AttributeValueType valueType = new AttributeValueType();
75 valueType.setDataType(datatype.stringValue());
76 valueType.getContent().add(value);
78 match.setAttributeValue(valueType);
80 // Add in the AttributeDesignator object
82 AttributeDesignatorType designator = new AttributeDesignatorType();
83 designator.setAttributeId(designatorId.stringValue());
84 designator.setCategory(designatorCategory.stringValue());
85 designator.setDataType(datatype.stringValue());
87 match.setAttributeDesignator(designator);
95 * Builds an AllOfType (AND) with one or more MatchType objects.
97 * @param matches A list of one or more MatchType
98 * @return The AllOf object
100 public static AllOfType buildAllOf(MatchType... matches) {
101 AllOfType allOf = new AllOfType();
102 for (MatchType match : matches) {
103 allOf.getMatch().add(match);
109 * Takes start and end time interval and generates an ApplyType for it.
111 * @param start ISO8601 timestamp
112 * @param end ISO8601 timestamp
115 public static ApplyType generateTimeInRange(String start, String end) {
116 if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
120 AttributeDesignatorType designator = new AttributeDesignatorType();
121 designator.setAttributeId(XACML3.ID_ENVIRONMENT_CURRENT_TIME.stringValue());
122 designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ENVIRONMENT.stringValue());
123 designator.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
125 AttributeValueType valueStart = new AttributeValueType();
126 valueStart.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
127 valueStart.getContent().add(start);
129 AttributeValueType valueEnd = new AttributeValueType();
130 valueEnd.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
131 valueEnd.getContent().add(end);
134 ApplyType applyOneAndOnly = new ApplyType();
135 applyOneAndOnly.setDescription("Unbag the current time");
136 applyOneAndOnly.setFunctionId(XACML3.ID_FUNCTION_TIME_ONE_AND_ONLY.stringValue());
137 applyOneAndOnly.getExpression().add(factory.createAttributeDesignator(designator));
139 ApplyType applyTimeInRange = new ApplyType();
140 applyTimeInRange.setDescription("return true if current time is in range.");
141 applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RANGE.stringValue());
142 applyTimeInRange.getExpression().add(factory.createApply(applyOneAndOnly));
143 applyTimeInRange.getExpression().add(factory.createAttributeValue(valueStart));
144 applyTimeInRange.getExpression().add(factory.createAttributeValue(valueEnd));
146 return applyTimeInRange;
150 * Parses an integer value from the string.
152 * @param strInteger String representation of integer
153 * @return Integer object
155 public static Integer parseInteger(String strInteger) {
156 Integer theInt = null;
158 theInt = Integer.parseInt(strInteger);
159 } catch (NumberFormatException e) {
161 Double dblLimit = Double.parseDouble(strInteger);
162 theInt = dblLimit.intValue();
163 } catch (NumberFormatException e1) {