Fix sonars in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / OnapObligation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 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.AttributeAssignment;
26 import com.att.research.xacml.api.Identifier;
27 import com.att.research.xacml.api.Obligation;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import java.util.Map;
31 import lombok.AccessLevel;
32 import lombok.Getter;
33 import lombok.ToString;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
39 import org.onap.policy.common.gson.MapDoubleAdapterFactory;
40
41 @Getter
42 @ToString
43 public class OnapObligation {
44
45     @Getter(AccessLevel.NONE)
46     private static final ObjectFactory factory = new ObjectFactory();
47
48     @Getter(AccessLevel.NONE)
49     private static final Gson gson =
50             new GsonBuilder().registerTypeAdapterFactory(new MapDoubleAdapterFactory()).create();
51
52     private String policyId;
53     private String policyType;
54     private String policyContent;
55     private Integer weight;
56
57     /**
58      * Constructor from an obligation.
59      *
60      * @param obligation Obligation object
61      */
62     public OnapObligation(Obligation obligation) {
63         //
64         // Scan through the obligations for them
65         //
66         for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
67             scanAttribute(assignment);
68         }
69     }
70
71     /**
72      * Constructor for just the policy details.
73      *
74      * @param policyId String
75      * @param policyContent String
76      */
77     public OnapObligation(String policyId, String policyContent) {
78         this.policyId = policyId;
79         this.policyContent = policyContent;
80     }
81
82
83     /**
84      * Constructor for policy details, type and weight.
85      *
86      * @param policyId String
87      * @param policyContent String
88      * @param policyType String
89      * @param weight int
90      */
91     public OnapObligation(String policyId, String policyContent, String policyType, Integer weight) {
92         this.policyId = policyId;
93         this.policyContent = policyContent;
94         this.policyType = policyType;
95         this.weight = weight;
96     }
97
98     /**
99      * getPolicyContentAsMap returns the policy as a map for convience.
100      *
101      * @return {@code Map<String, Object>}
102      */
103     @SuppressWarnings("unchecked")
104     public Map<String, Object> getPolicyContentAsMap() {
105         if (this.policyContent == null) {
106             return null;
107         }
108         return gson.fromJson(this.policyContent, Map.class);
109     }
110
111     /**
112      * Generates default obligation using default Permit and Obligation Id.
113      *
114      * @return ObligationExpressionType object
115      */
116     public ObligationExpressionType generateObligation() {
117         return this.generateObligation(EffectType.PERMIT, ToscaDictionary.ID_OBLIGATION_REST_BODY);
118     }
119
120     /**
121      * generateObligation - generates an obligation object with the attributes that exist. Note:
122      * any null values will result in NO attribute assignment for that attribute.
123      *
124      * @param effectType EffectType object
125      * @param obligationId Id for the obligation
126      * @return ObligationExpressionType object
127      */
128     public ObligationExpressionType generateObligation(EffectType effectType, Identifier obligationId) {
129         //
130         // Create an ObligationExpression
131         //
132         var obligation = new ObligationExpressionType();
133         obligation.setFulfillOn(effectType);
134         obligation.setObligationId(obligationId.stringValue());
135         //
136         // Update the obligation
137         //
138         updateObligation(obligation);
139         //
140         // Convenience return
141         //
142         return obligation;
143     }
144
145     /**
146      * Updates an existing Obligation object with the attributes that exist. Note: any null values
147      * will result in NO attribute assignment for that attribute.
148      *
149      * @param obligation ObligationExpressionType object
150      * @return ObligationExpressionType object
151      */
152     public ObligationExpressionType updateObligation(ObligationExpressionType obligation) {
153         //
154         // Add policy-id
155         //
156         addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_ID,
157                 ToscaDictionary.ID_OBLIGATION_POLICY_ID_DATATYPE,
158                 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY,
159                 policyId);
160         //
161         // Add policy contents
162         //
163         addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT,
164                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_DATATYPE,
165                 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY,
166                 policyContent);
167         //
168         // Add the weight
169         //
170         addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT,
171                 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_DATATYPE,
172                 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY,
173                 weight);
174         //
175         // Add the policy type
176         //
177         addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_TYPE,
178                 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_DATATYPE,
179                 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY,
180                 policyType);
181         //
182         // Return as a convenience
183         //
184         return obligation;
185     }
186
187     /**
188      * scanAttribute - scans the assignment for a supported obligation assignment. Applications
189      * can override this class and provide their own custom attribute assignments if desired.
190      *
191      * @param assignment AttributeAssignment object
192      * @return true if found an ONAP supported attribute
193      */
194     protected boolean scanAttribute(AttributeAssignment assignment) {
195         //
196         // Check for our supported attributes. Note: Cannot use a switch
197         // as Identifier isn't a constant.
198         //
199         if (ToscaDictionary.ID_OBLIGATION_POLICY_ID.equals(assignment.getAttributeId())) {
200             policyId = assignment.getAttributeValue().getValue().toString();
201             return true;
202         } else if (ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.equals(assignment.getAttributeId())) {
203             policyType = assignment.getAttributeValue().getValue().toString();
204             return true;
205         } else if (ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.equals(assignment.getAttributeId())) {
206             policyContent = assignment.getAttributeValue().getValue().toString();
207             return true;
208         } else if (ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.equals(assignment.getAttributeId())) {
209             weight = Integer.decode(assignment.getAttributeValue().getValue().toString());
210             return true;
211         }
212         //
213         // By returning true, we indicate this isn't an attribute
214         // supported in the this class. Targeted for applications
215         // that derive from this class in order to extend it.
216         //
217         return false;
218     }
219
220     /**
221      * Creates the necessary objects to insert into the obligation, if the value object is not null.
222      *
223      * @param obligation Incoming Obligation
224      * @param id Attribute Id
225      * @param datatype Attribute's Data type
226      * @param category Attributes Category
227      * @param theValue Attribute value
228      * @return obligation Incoming obligation
229      */
230     protected ObligationExpressionType addOptionalAttributeToObligation(ObligationExpressionType obligation,
231             Identifier id, Identifier datatype, Identifier category, Object theValue) {
232         //
233         // Simple check for null
234         //
235         if (theValue == null) {
236             return obligation;
237         }
238         //
239         // Create an AttributeValue for it
240         //
241         var value = new AttributeValueType();
242         value.setDataType(datatype.stringValue());
243         value.getContent().add(theValue.toString());
244         //
245         // Create our AttributeAssignmentExpression where we will
246         // store the contents of the policy id.
247         //
248         var expressionType = new AttributeAssignmentExpressionType();
249         expressionType.setAttributeId(id.stringValue());
250         expressionType.setCategory(category.stringValue());
251         expressionType.setExpression(factory.createAttributeValue(value));
252         //
253         // Add it to the obligation
254         //
255         obligation.getAttributeAssignmentExpression().add(expressionType);
256         //
257         // Return as convenience
258         //
259         return obligation;
260     }
261
262 }