Better error handling for decisions
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdCombinedPolicyResultsTranslator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.std;
24
25 import com.att.research.xacml.api.Advice;
26 import com.att.research.xacml.api.DataTypeException;
27 import com.att.research.xacml.api.Identifier;
28 import com.att.research.xacml.api.Obligation;
29 import com.att.research.xacml.api.Request;
30 import com.att.research.xacml.api.XACML3;
31 import com.att.research.xacml.std.annotations.RequestParser;
32 import com.google.common.base.Strings;
33 import java.util.Collection;
34 import java.util.Map;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
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.MatchType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.models.decisions.concepts.DecisionRequest;
44 import org.onap.policy.models.decisions.concepts.DecisionResponse;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.pdp.xacml.application.common.OnapObligation;
47 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
48 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
49 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(StdCombinedPolicyResultsTranslator.class);
56
57     public StdCombinedPolicyResultsTranslator() {
58         super();
59     }
60
61     @Override
62     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
63         //
64         // Sanity checks
65         //
66         if (toscaPolicy == null) {
67             throw new ToscaPolicyConversionException("Cannot convert a NULL policy");
68         }
69         if (toscaPolicy.getMetadata() == null) {
70             throw new ToscaPolicyConversionException("Cannot convert a policy with missing metadata section");
71         }
72         //
73         // Get the policy Id
74         //
75         String policyId = toscaPolicy.getMetadata().get(POLICY_ID);
76         //
77         // Set it as the policy ID
78         //
79         PolicyType newPolicyType = new PolicyType();
80         newPolicyType.setPolicyId(policyId);
81         //
82         // Optional description
83         //
84         newPolicyType.setDescription(toscaPolicy.getDescription());
85         //
86         // There should be a metadata section
87         //
88         this.fillMetadataSection(newPolicyType, toscaPolicy.getMetadata());
89         //
90         // Set the combining rule
91         //
92         newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_FIRST_APPLICABLE.stringValue());
93         //
94         // Generate the TargetType
95         //
96         TargetType target = this.generateTargetType(policyId, toscaPolicy.getType(), toscaPolicy.getVersion());
97         newPolicyType.setTarget(target);
98         //
99         // Now create the Permit Rule
100         // No target since the policy has a target
101         // With obligations.
102         //
103         RuleType rule = new RuleType();
104         rule.setDescription("Default is to PERMIT if the policy matches.");
105         rule.setRuleId(policyId + ":rule");
106         rule.setEffect(EffectType.PERMIT);
107         rule.setTarget(new TargetType());
108         //
109         // Now represent the policy as Json
110         //
111         StandardCoder coder = new StandardCoder();
112         String jsonPolicy;
113         try {
114             jsonPolicy = coder.encode(toscaPolicy);
115         } catch (CoderException e) {
116             throw new ToscaPolicyConversionException(e);
117         }
118         addObligation(rule, policyId, jsonPolicy, null, toscaPolicy.getType());
119         //
120         // Add the rule to the policy
121         //
122         newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
123         //
124         // Return our new policy
125         //
126         return newPolicyType;
127     }
128
129     @Override
130     public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
131         LOGGER.info("Converting Request {}", request);
132         try {
133             return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request));
134         } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
135             throw new ToscaPolicyConversionException("Failed to parse request", e);
136         }
137     }
138
139     /**
140      * scanObligations - scans the list of obligations and make appropriate method calls to process
141      * obligations.
142      *
143      * @param obligations Collection of obligation objects
144      * @param decisionResponse DecisionResponse object used to store any results from obligations.
145      */
146     @Override
147     protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
148         for (Obligation obligation : obligations) {
149             Identifier obligationId = obligation.getId();
150             LOGGER.info("Obligation: {}", obligationId);
151             if (ToscaDictionary.ID_OBLIGATION_REST_BODY.equals(obligationId)) {
152                 scanContentObligation(obligation, decisionResponse);
153             }
154         }
155     }
156
157     /**
158      * scanAdvice - not implemented in this class.
159      *
160      * @param advice Collection of advice objects
161      * @param DecisionResponse DecisionResponse object
162      */
163     @Override
164     protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
165         //
166         // By default there are no advice supported in this object. Please override and provide
167         // any needed functionality.
168         //
169         LOGGER.warn("Advice found - not supported in this class {}", this.getClass());
170     }
171
172     /**
173      * scanContentObligation - scans the specific obligation for policy-id and policy-content.
174      *
175      * @param obligation Obligation incoming obligation object
176      * @param decisionResponse DecisionResponse object
177      */
178     protected void scanContentObligation(Obligation obligation, DecisionResponse decisionResponse) {
179         //
180         // Create our OnapObligation which will scan for attributes
181         //
182         OnapObligation onapObligation = new OnapObligation(obligation);
183         //
184         // Get the attributes we care about
185         //
186         String policyId = onapObligation.getPolicyId();
187         Map<String, Object> policyContent = onapObligation.getPolicyContentAsMap();
188         //
189         // Sanity check that we got the attributes we care about. NOTE: This translator
190         // ensures that these are set when convertPolicy is called.
191         //
192         if (! Strings.isNullOrEmpty(policyId) && policyContent != null) {
193             decisionResponse.getPolicies().put(policyId, policyContent);
194         } else {
195             LOGGER.error("Missing obligation policyId {} or policyContent {}", policyId,
196                     policyContent == null ? "null" : policyContent.size());
197         }
198     }
199
200     /**
201      * generateTargetType - Generates a TargetType object for the policy-id and policy-type.
202      *
203      * @param policyId String policy-id
204      * @param policyType String policy type
205      * @param policyTypeVersion String policy type version
206      * @return TargetType object
207      */
208     protected TargetType generateTargetType(String policyId, String policyType, String policyTypeVersion) {
209         //
210         // Create all the match's that are possible
211         //
212         // This is for the Policy Id
213         //
214         MatchType matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
215                 XACML3.ID_FUNCTION_STRING_EQUAL,
216                 policyId,
217                 XACML3.ID_DATATYPE_STRING,
218                 ToscaDictionary.ID_RESOURCE_POLICY_ID,
219                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
220         //
221         // This is for the Policy Type
222         //
223         MatchType matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
224                 XACML3.ID_FUNCTION_STRING_EQUAL,
225                 policyType,
226                 XACML3.ID_DATATYPE_STRING,
227                 ToscaDictionary.ID_RESOURCE_POLICY_TYPE,
228                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
229         //
230         // This is for the Policy Type version
231         //
232         MatchType matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
233                 XACML3.ID_FUNCTION_STRING_EQUAL,
234                 policyTypeVersion,
235                 XACML3.ID_DATATYPE_STRING,
236                 ToscaDictionary.ID_RESOURCE_POLICY_TYPE_VERSION,
237                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
238         //
239         // This is our outer AnyOf - which is an OR
240         //
241         AnyOfType anyOf = new AnyOfType();
242         //
243         // Create AllOf (AND) of just Policy Id
244         //
245         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyId));
246         //
247         // Create AllOf (AND) of just Policy Type
248         //
249         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType));
250         //
251         // Create AllOf (AND) of Policy Type and Policy Type Version
252         //
253         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType, matchPolicyTypeVersion));
254         //
255         // Now we can create the TargetType, add the top-level anyOf (OR),
256         // and return the value.
257         //
258         TargetType target = new TargetType();
259         target.getAnyOf().add(anyOf);
260         return target;
261     }
262
263 }