2 * ============LICENSE_START=======================================================
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
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;
30 import lombok.AccessLevel;
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;
41 public class OnapObligation {
43 @Getter(AccessLevel.NONE)
44 private static final ObjectFactory factory = new ObjectFactory();
46 @Getter(AccessLevel.NONE)
47 private static final Gson gson = new Gson();
49 private String policyId;
50 private String policyType;
51 private String policyContent;
52 private Integer weight;
55 * Constructor from an obligation.
57 * @param obligation Obligation object
59 public OnapObligation(Obligation obligation) {
61 // Scan through the obligations for them
63 for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
64 scanAttribute(assignment);
69 * Constructor for just the policy details.
71 * @param policyId String
72 * @param policyContent String
74 public OnapObligation(String policyId, String policyContent) {
75 this.policyId = policyId;
76 this.policyContent = policyContent;
81 * Constructor for policy details, type and weight.
83 * @param policyId String
84 * @param policyContent String
85 * @param policyType String
88 public OnapObligation(String policyId, String policyContent, String policyType, Integer weight) {
89 this.policyId = policyId;
90 this.policyContent = policyContent;
91 this.policyType = policyType;
96 * getPolicyContentAsMap returns the policy as a map for convience.
98 * @return {@code Map<String, Object>}
100 @SuppressWarnings("unchecked")
101 public Map<String, Object> getPolicyContentAsMap() {
102 if (this.policyContent == null) {
105 return gson.fromJson(this.policyContent, Map.class);
109 * Generates default obligation using default Permit and Obligation Id.
111 * @return ObligationExpressionType object
113 public ObligationExpressionType generateObligation() {
114 return this.generateObligation(EffectType.PERMIT, ToscaDictionary.ID_OBLIGATION_REST_BODY);
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.
121 * @param effectType EffectType object
122 * @param obligationId Id for the obligation
123 * @return ObligationExpressionType object
125 public ObligationExpressionType generateObligation(EffectType effectType, Identifier obligationId) {
127 // Create an ObligationExpression
129 ObligationExpressionType obligation = new ObligationExpressionType();
130 obligation.setFulfillOn(effectType);
131 obligation.setObligationId(obligationId.stringValue());
133 // Update the obligation
135 updateObligation(obligation);
137 // Convenience return
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.
146 * @param obligation ObligationExpressionType object
147 * @return ObligationExpressionType object
149 public ObligationExpressionType updateObligation(ObligationExpressionType obligation) {
153 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_ID,
154 ToscaDictionary.ID_OBLIGATION_POLICY_ID_DATATYPE,
155 ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY,
158 // Add policy contents
160 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT,
161 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_DATATYPE,
162 ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY,
167 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT,
168 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_DATATYPE,
169 ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY,
172 // Add the policy type
174 addOptionalAttributeToObligation(obligation, ToscaDictionary.ID_OBLIGATION_POLICY_TYPE,
175 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_DATATYPE,
176 ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY,
179 // Return as a convenience
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.
188 * @param assignment AttributeAssignment object
189 * @return true if found an ONAP supported attribute
191 protected boolean scanAttribute(AttributeAssignment assignment) {
193 // Check for our supported attributes. Note: Cannot use a switch
194 // as Identifier isn't a constant.
196 if (ToscaDictionary.ID_OBLIGATION_POLICY_ID.equals(assignment.getAttributeId())) {
197 policyId = assignment.getAttributeValue().getValue().toString();
199 } else if (ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.equals(assignment.getAttributeId())) {
200 policyType = assignment.getAttributeValue().getValue().toString();
202 } else if (ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.equals(assignment.getAttributeId())) {
203 policyContent = assignment.getAttributeValue().getValue().toString();
205 } else if (ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.equals(assignment.getAttributeId())) {
206 weight = Integer.decode(assignment.getAttributeValue().getValue().toString());
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.
218 * Creates the necessary objects to insert into the obligation, if the value object is not null.
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
227 protected ObligationExpressionType addOptionalAttributeToObligation(ObligationExpressionType obligation,
228 Identifier id, Identifier datatype, Identifier category, Object theValue) {
230 // Simple check for null
232 if (theValue == null) {
236 // Create an AttributeValue for it
238 AttributeValueType value = new AttributeValueType();
239 value.setDataType(datatype.stringValue());
240 value.getContent().add(theValue.toString());
242 // Create our AttributeAssignmentExpression where we will
243 // store the contents of the policy id.
245 AttributeAssignmentExpressionType expressionType = new AttributeAssignmentExpressionType();
246 expressionType.setAttributeId(id.stringValue());
247 expressionType.setCategory(category.stringValue());
248 expressionType.setExpression(factory.createAttributeValue(value));
250 // Add it to the obligation
252 obligation.getAttributeAssignmentExpression().add(expressionType);
254 // Return as convenience