Merge "Added support for application/xacml+json in native app"
[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) {
131         LOGGER.info("Converting Request {}", request);
132         try {
133             return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request));
134         } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
135             LOGGER.error("Failed to convert DecisionRequest", e);
136         }
137         //
138         // TODO throw exception
139         //
140         return null;
141     }
142
143     /**
144      * scanObligations - scans the list of obligations and make appropriate method calls to process
145      * obligations.
146      *
147      * @param obligations Collection of obligation objects
148      * @param decisionResponse DecisionResponse object used to store any results from obligations.
149      */
150     @Override
151     protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
152         for (Obligation obligation : obligations) {
153             Identifier obligationId = obligation.getId();
154             LOGGER.info("Obligation: {}", obligationId);
155             if (ToscaDictionary.ID_OBLIGATION_REST_BODY.equals(obligationId)) {
156                 scanContentObligation(obligation, decisionResponse);
157             }
158         }
159     }
160
161     /**
162      * scanAdvice - not implemented in this class.
163      *
164      * @param advice Collection of advice objects
165      * @param DecisionResponse DecisionResponse object
166      */
167     @Override
168     protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
169         //
170         // By default there are no advice supported in this object. Please override and provide
171         // any needed functionality.
172         //
173         LOGGER.warn("Advice found - not supported in this class {}", this.getClass());
174     }
175
176     /**
177      * scanContentObligation - scans the specific obligation for policy-id and policy-content.
178      *
179      * @param obligation Obligation incoming obligation object
180      * @param decisionResponse DecisionResponse object
181      */
182     protected void scanContentObligation(Obligation obligation, DecisionResponse decisionResponse) {
183         //
184         // Create our OnapObligation which will scan for attributes
185         //
186         OnapObligation onapObligation = new OnapObligation(obligation);
187         //
188         // Get the attributes we care about
189         //
190         String policyId = onapObligation.getPolicyId();
191         Map<String, Object> policyContent = onapObligation.getPolicyContentAsMap();
192         //
193         // Sanity check that we got the attributes we care about. NOTE: This translator
194         // ensures that these are set when convertPolicy is called.
195         //
196         if (! Strings.isNullOrEmpty(policyId) && policyContent != null) {
197             decisionResponse.getPolicies().put(policyId, policyContent);
198         } else {
199             LOGGER.error("Missing obligation policyId {} or policyContent {}", policyId,
200                     policyContent == null ? "null" : policyContent.size());
201         }
202     }
203
204     /**
205      * generateTargetType - Generates a TargetType object for the policy-id and policy-type.
206      *
207      * @param policyId String policy-id
208      * @param policyType String policy type
209      * @param policyTypeVersion String policy type version
210      * @return TargetType object
211      */
212     protected TargetType generateTargetType(String policyId, String policyType, String policyTypeVersion) {
213         //
214         // Create all the match's that are possible
215         //
216         // This is for the Policy Id
217         //
218         MatchType matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
219                 XACML3.ID_FUNCTION_STRING_EQUAL,
220                 policyId,
221                 XACML3.ID_DATATYPE_STRING,
222                 ToscaDictionary.ID_RESOURCE_POLICY_ID,
223                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
224         //
225         // This is for the Policy Type
226         //
227         MatchType matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
228                 XACML3.ID_FUNCTION_STRING_EQUAL,
229                 policyType,
230                 XACML3.ID_DATATYPE_STRING,
231                 ToscaDictionary.ID_RESOURCE_POLICY_TYPE,
232                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
233         //
234         // This is for the Policy Type version
235         //
236         MatchType matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
237                 XACML3.ID_FUNCTION_STRING_EQUAL,
238                 policyTypeVersion,
239                 XACML3.ID_DATATYPE_STRING,
240                 ToscaDictionary.ID_RESOURCE_POLICY_TYPE_VERSION,
241                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
242         //
243         // This is our outer AnyOf - which is an OR
244         //
245         AnyOfType anyOf = new AnyOfType();
246         //
247         // Create AllOf (AND) of just Policy Id
248         //
249         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyId));
250         //
251         // Create AllOf (AND) of just Policy Type
252         //
253         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType));
254         //
255         // Create AllOf (AND) of Policy Type and Policy Type Version
256         //
257         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType, matchPolicyTypeVersion));
258         //
259         // Now we can create the TargetType, add the top-level anyOf (OR),
260         // and return the value.
261         //
262         TargetType target = new TargetType();
263         target.getAnyOf().add(anyOf);
264         return target;
265     }
266
267 }