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