2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.xacml.application.common;
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;
31 import lombok.AccessLevel;
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;
43 public class OnapObligation {
45 @Getter(AccessLevel.NONE)
46 private static final ObjectFactory factory = new ObjectFactory();
48 @Getter(AccessLevel.NONE)
49 private static final Gson gson =
50 new GsonBuilder().registerTypeAdapterFactory(new MapDoubleAdapterFactory()).create();
52 private String policyId;
53 private String policyType;
54 private String policyContent;
55 private Integer weight;
58 * Constructor from an obligation.
60 * @param obligation Obligation object
62 public OnapObligation(Obligation obligation) {
64 // Scan through the obligations for them
66 for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
67 scanAttribute(assignment);
72 * Constructor for just the policy details.
74 * @param policyId String
75 * @param policyContent String
77 public OnapObligation(String policyId, String policyContent) {
78 this.policyId = policyId;
79 this.policyContent = policyContent;
84 * Constructor for policy details, type and weight.
86 * @param policyId String
87 * @param policyContent String
88 * @param policyType String
91 public OnapObligation(String policyId, String policyContent, String policyType, Integer weight) {
92 this.policyId = policyId;
93 this.policyContent = policyContent;
94 this.policyType = policyType;
99 * getPolicyContentAsMap returns the policy as a map for convience.
101 * @return {@code Map<String, Object>}
103 @SuppressWarnings("unchecked")
104 public Map<String, Object> getPolicyContentAsMap() {
105 if (this.policyContent == null) {
108 return gson.fromJson(this.policyContent, Map.class);
112 * Generates default obligation using default Permit and Obligation Id.
114 * @return ObligationExpressionType object
116 public ObligationExpressionType generateObligation() {
117 return this.generateObligation(EffectType.PERMIT, ToscaDictionary.ID_OBLIGATION_REST_BODY);
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.
124 * @param effectType EffectType object
125 * @param obligationId Id for the obligation
126 * @return ObligationExpressionType object
128 public ObligationExpressionType generateObligation(EffectType effectType, Identifier obligationId) {
130 // Create an ObligationExpression
132 var obligation = new ObligationExpressionType();
133 obligation.setFulfillOn(effectType);
134 obligation.setObligationId(obligationId.stringValue());
136 // Update the obligation
138 updateObligation(obligation);
140 // Convenience return
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.
149 * @param obligation ObligationExpressionType object
150 * @return ObligationExpressionType object
152 public ObligationExpressionType updateObligation(ObligationExpressionType obligation) {
156 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_ID,
157 ToscaDictionary.ID_OBLIGATION_POLICY_ID_DATATYPE,
158 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY,
161 // Add policy contents
163 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT,
164 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_DATATYPE,
165 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY,
170 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT,
171 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_DATATYPE,
172 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY,
175 // Add the policy type
177 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_TYPE,
178 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_DATATYPE,
179 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY,
182 // Return as a convenience
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.
191 * @param assignment AttributeAssignment object
192 * @return true if found an ONAP supported attribute
194 protected boolean scanAttribute(AttributeAssignment assignment) {
196 // Check for our supported attributes. Note: Cannot use a switch
197 // as Identifier isn't a constant.
199 if (ToscaDictionary.ID_OBLIGATION_POLICY_ID.equals(assignment.getAttributeId())) {
200 policyId = assignment.getAttributeValue().getValue().toString();
202 } else if (ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.equals(assignment.getAttributeId())) {
203 policyType = assignment.getAttributeValue().getValue().toString();
205 } else if (ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.equals(assignment.getAttributeId())) {
206 policyContent = assignment.getAttributeValue().getValue().toString();
208 } else if (ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.equals(assignment.getAttributeId())) {
209 weight = Integer.decode(assignment.getAttributeValue().getValue().toString());
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.
221 * Creates the necessary objects to insert into the obligation, if the value object is not null.
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
230 protected ObligationExpressionType addOptionalAttributeToObligation(ObligationExpressionType obligation,
231 Identifier id, Identifier datatype, Identifier category, Object theValue) {
233 // Simple check for null
235 if (theValue == null) {
239 // Create an AttributeValue for it
241 var value = new AttributeValueType();
242 value.setDataType(datatype.stringValue());
243 value.getContent().add(theValue.toString());
245 // Create our AttributeAssignmentExpression where we will
246 // store the contents of the policy id.
248 var expressionType = new AttributeAssignmentExpressionType();
249 expressionType.setAttributeId(id.stringValue());
250 expressionType.setCategory(category.stringValue());
251 expressionType.setExpression(factory.createAttributeValue(value));
253 // Add it to the obligation
255 obligation.getAttributeAssignmentExpression().add(expressionType);
257 // Return as convenience