796499d415cca751240ff6dc229c09f6587f03d8
[policy/xacml-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
36
37 /**
38  * This class contains static methods of helper classes to convert TOSCA policies
39  * into XACML policies.
40  *
41  * @author pameladragosh
42  *
43  */
44 public final class ToscaPolicyTranslatorUtils {
45     private static final ObjectFactory factory = new ObjectFactory();
46
47     private ToscaPolicyTranslatorUtils() {
48         super();
49     }
50
51     /**
52      * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
53      * combination.
54      *
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
62      */
63     public static <T> MatchType buildMatchTypeDesignator(Identifier function,
64             T value,
65             Identifier datatype,
66             Identifier designatorId,
67             Identifier designatorCategory) {
68         //
69         // Create the MatchType object and set its function
70         //
71         MatchType match = new MatchType();
72         match.setMatchId(function.stringValue());
73         //
74         // Add in the AttributeValue object
75         //
76         AttributeValueType valueType = new AttributeValueType();
77         valueType.setDataType(datatype.stringValue());
78         valueType.getContent().add(value);
79
80         match.setAttributeValue(valueType);
81         //
82         // Add in the AttributeDesignator object
83         //
84         AttributeDesignatorType designator = new AttributeDesignatorType();
85         designator.setAttributeId(designatorId.stringValue());
86         designator.setCategory(designatorCategory.stringValue());
87         designator.setDataType(datatype.stringValue());
88
89         match.setAttributeDesignator(designator);
90         //
91         // Done
92         //
93         return match;
94     }
95
96     /**
97      * Builds an AllOfType (AND) with one or more MatchType objects.
98      *
99      * @param matches A list of one or more MatchType
100      * @return The AllOf object
101      */
102     public static AllOfType buildAllOf(MatchType... matches) {
103         AllOfType allOf = new AllOfType();
104         for (MatchType match : matches) {
105             allOf.getMatch().add(match);
106         }
107         return allOf;
108     }
109
110     /**
111      * Takes start and end time interval and generates an ApplyType for it.
112      *
113      * @param start ISO8601 timestamp
114      * @param end ISO8601 timestamp
115      * @return ApplyType
116      */
117     public static ApplyType generateTimeInRange(String start, String end) {
118         if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
119             return null;
120         }
121
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());
126
127         AttributeValueType valueStart = new AttributeValueType();
128         valueStart.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
129         valueStart.getContent().add(start);
130
131         AttributeValueType valueEnd = new AttributeValueType();
132         valueEnd.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
133         valueEnd.getContent().add(end);
134
135
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));
140
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));
147
148         return applyTimeInRange;
149     }
150
151     /**
152      * Parses an integer value from the string.
153      *
154      * @param strInteger String representation of integer
155      * @return Integer object
156      */
157     public static Integer parseInteger(String strInteger) {
158         Integer theInt = null;
159         try {
160             theInt = Integer.parseInt(strInteger);
161         } catch (NumberFormatException e) {
162             try {
163                 Double dblLimit = Double.parseDouble(strInteger);
164                 theInt = dblLimit.intValue();
165             } catch (NumberFormatException e1) {
166                 return null;
167             }
168         }
169         return theInt;
170     }
171
172     /**
173      * For a given MatchType or AnyOfType, builds it and appends it into the
174      * AnyOfType.
175      *
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
179      */
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);
184             if (anyOf == null) {
185                 anyOf = new AnyOfType();
186             }
187             anyOf.getAllOf().add(allOf);
188         } else if (type instanceof AllOfType) {
189             if (anyOf == null) {
190                 anyOf = new AnyOfType();
191             }
192             anyOf.getAllOf().add((AllOfType) type);
193         }
194
195         return anyOf;
196     }
197
198     /**
199      * buildAndAppendTarget - adds in the potential object into TargetType.
200      *
201      * @param target TargetType - must exist
202      * @param object AnyOfType or MatchType
203      * @return TargetType
204      */
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);
214         }
215         return target;
216     }
217 }