Updates to support fixed guard policy types
[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-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.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;
34
35 /**
36  * This class contains static methods of helper classes to convert TOSCA policies
37  * into XACML policies.
38  *
39  * @author pameladragosh
40  *
41  */
42 public final class ToscaPolicyTranslatorUtils {
43     private static final ObjectFactory factory = new ObjectFactory();
44
45     private ToscaPolicyTranslatorUtils() {
46         super();
47     }
48
49     /**
50      * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
51      * combination.
52      *
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
60      */
61     public static <T> MatchType buildMatchTypeDesignator(Identifier function,
62             T value,
63             Identifier datatype,
64             Identifier designatorId,
65             Identifier designatorCategory) {
66         //
67         // Create the MatchType object and set its function
68         //
69         MatchType match = new MatchType();
70         match.setMatchId(function.stringValue());
71         //
72         // Add in the AttributeValue object
73         //
74         AttributeValueType valueType = new AttributeValueType();
75         valueType.setDataType(datatype.stringValue());
76         valueType.getContent().add(value);
77
78         match.setAttributeValue(valueType);
79         //
80         // Add in the AttributeDesignator object
81         //
82         AttributeDesignatorType designator = new AttributeDesignatorType();
83         designator.setAttributeId(designatorId.stringValue());
84         designator.setCategory(designatorCategory.stringValue());
85         designator.setDataType(datatype.stringValue());
86
87         match.setAttributeDesignator(designator);
88         //
89         // Done
90         //
91         return match;
92     }
93
94     /**
95      * Builds an AllOfType (AND) with one or more MatchType objects.
96      *
97      * @param matches A list of one or more MatchType
98      * @return The AllOf object
99      */
100     public static AllOfType buildAllOf(MatchType... matches) {
101         AllOfType allOf = new AllOfType();
102         for (MatchType match : matches) {
103             allOf.getMatch().add(match);
104         }
105         return allOf;
106     }
107
108     /**
109      * Takes start and end time interval and generates an ApplyType for it.
110      *
111      * @param start ISO8601 timestamp
112      * @param end ISO8601 timestamp
113      * @return ApplyType
114      */
115     public static ApplyType generateTimeInRange(String start, String end) {
116         if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
117             return null;
118         }
119
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());
124
125         AttributeValueType valueStart = new AttributeValueType();
126         valueStart.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
127         valueStart.getContent().add(start);
128
129         AttributeValueType valueEnd = new AttributeValueType();
130         valueEnd.setDataType(XACML3.ID_DATATYPE_TIME.stringValue());
131         valueEnd.getContent().add(end);
132
133
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));
138
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));
145
146         return applyTimeInRange;
147     }
148
149     /**
150      * Parses an integer value from the string.
151      *
152      * @param strInteger String representation of integer
153      * @return Integer object
154      */
155     public static Integer parseInteger(String strInteger) {
156         Integer theInt = null;
157         try {
158             theInt = Integer.parseInt(strInteger);
159         } catch (NumberFormatException e) {
160             try {
161                 Double dblLimit = Double.parseDouble(strInteger);
162                 theInt = dblLimit.intValue();
163             } catch (NumberFormatException e1) {
164                 return null;
165             }
166         }
167         return theInt;
168     }
169 }