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