Update dublin .gitreview
[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 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 oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
27 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
28 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
30
31 /**
32  * This class contains static methods of helper classes to convert TOSCA policies
33  * into XACML policies.
34  *
35  * @author pameladragosh
36  *
37  */
38 public final class ToscaPolicyTranslatorUtils {
39
40     private ToscaPolicyTranslatorUtils() {
41         super();
42     }
43
44     /**
45      * This method builds a MatchType for TargetType object for AttributeValue and AttributeDesignator
46      * combination.
47      *
48      * @param <T> Incoming value could be any object
49      * @param function Function for the Match
50      * @param value Attribute value used
51      * @param datatype Datatype for attribute value and AttributeDesignator
52      * @param designatorId ID for the AttributeDesignator
53      * @param designatorCategory Category ID for the AttributeDesignator
54      * @return The MatchType object
55      */
56     public static <T> MatchType buildMatchTypeDesignator(Identifier function,
57             T value,
58             Identifier datatype,
59             Identifier designatorId,
60             Identifier designatorCategory) {
61         //
62         // Create the MatchType object and set its function
63         //
64         MatchType match = new MatchType();
65         match.setMatchId(function.stringValue());
66         //
67         // Add in the AttributeValue object
68         //
69         AttributeValueType valueType = new AttributeValueType();
70         valueType.setDataType(datatype.stringValue());
71         valueType.getContent().add(value);
72
73         match.setAttributeValue(valueType);
74         //
75         // Add in the AttributeDesignator object
76         //
77         AttributeDesignatorType designator = new AttributeDesignatorType();
78         designator.setAttributeId(designatorId.stringValue());
79         designator.setCategory(designatorCategory.stringValue());
80         designator.setDataType(datatype.stringValue());
81
82         match.setAttributeDesignator(designator);
83         //
84         // Done
85         //
86         return match;
87     }
88
89     /**
90      * Builds an AllOfType (AND) with one or more MatchType objects.
91      *
92      * @param matches A list of one or more MatchType
93      * @return The AllOf object
94      */
95     public static AllOfType buildAllOf(MatchType... matches) {
96         AllOfType allOf = new AllOfType();
97         for (MatchType match : matches) {
98             allOf.getMatch().add(match);
99         }
100         return allOf;
101     }
102 }