2 * ============LICENSE_START=======================================================
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
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.std;
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;
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;
53 public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
55 private static final Logger LOGGER = LoggerFactory.getLogger(StdCombinedPolicyResultsTranslator.class);
57 public StdCombinedPolicyResultsTranslator() {
62 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
66 if (toscaPolicy == null) {
67 throw new ToscaPolicyConversionException("Cannot convert a NULL policy");
69 if (toscaPolicy.getMetadata() == null) {
70 throw new ToscaPolicyConversionException("Cannot convert a policy with missing metadata section");
75 String policyId = toscaPolicy.getMetadata().get(POLICY_ID);
77 // Set it as the policy ID
79 PolicyType newPolicyType = new PolicyType();
80 newPolicyType.setPolicyId(policyId);
82 // Optional description
84 newPolicyType.setDescription(toscaPolicy.getDescription());
86 // There should be a metadata section
88 this.fillMetadataSection(newPolicyType, toscaPolicy.getMetadata());
90 // Set the combining rule
92 newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_FIRST_APPLICABLE.stringValue());
94 // Generate the TargetType
96 TargetType target = this.generateTargetType(policyId, toscaPolicy.getType(), toscaPolicy.getVersion());
97 newPolicyType.setTarget(target);
99 // Now create the Permit Rule
100 // No target since the policy has a target
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());
109 // Now represent the policy as Json
111 StandardCoder coder = new StandardCoder();
114 jsonPolicy = coder.encode(toscaPolicy);
115 } catch (CoderException e) {
116 throw new ToscaPolicyConversionException(e);
118 addObligation(rule, policyId, jsonPolicy, null, toscaPolicy.getType());
120 // Add the rule to the policy
122 newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
124 // Return our new policy
126 return newPolicyType;
130 public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
131 LOGGER.info("Converting Request {}", request);
133 return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request));
134 } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
135 throw new ToscaPolicyConversionException("Failed to parse request", e);
140 * scanObligations - scans the list of obligations and make appropriate method calls to process
143 * @param obligations Collection of obligation objects
144 * @param decisionResponse DecisionResponse object used to store any results from obligations.
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);
158 * scanAdvice - not implemented in this class.
160 * @param advice Collection of advice objects
161 * @param DecisionResponse DecisionResponse object
164 protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
166 // By default there are no advice supported in this object. Please override and provide
167 // any needed functionality.
169 LOGGER.warn("Advice found - not supported in this class {}", this.getClass());
173 * scanContentObligation - scans the specific obligation for policy-id and policy-content.
175 * @param obligation Obligation incoming obligation object
176 * @param decisionResponse DecisionResponse object
178 protected void scanContentObligation(Obligation obligation, DecisionResponse decisionResponse) {
180 // Create our OnapObligation which will scan for attributes
182 OnapObligation onapObligation = new OnapObligation(obligation);
184 // Get the attributes we care about
186 String policyId = onapObligation.getPolicyId();
187 Map<String, Object> policyContent = onapObligation.getPolicyContentAsMap();
189 // Sanity check that we got the attributes we care about. NOTE: This translator
190 // ensures that these are set when convertPolicy is called.
192 if (! Strings.isNullOrEmpty(policyId) && policyContent != null) {
193 decisionResponse.getPolicies().put(policyId, policyContent);
195 LOGGER.error("Missing obligation policyId {} or policyContent {}", policyId,
196 policyContent == null ? "null" : policyContent.size());
201 * generateTargetType - Generates a TargetType object for the policy-id and policy-type.
203 * @param policyId String policy-id
204 * @param policyType String policy type
205 * @param policyTypeVersion String policy type version
206 * @return TargetType object
208 protected TargetType generateTargetType(String policyId, String policyType, String policyTypeVersion) {
210 // Create all the match's that are possible
212 // This is for the Policy Id
214 MatchType matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
215 XACML3.ID_FUNCTION_STRING_EQUAL,
217 XACML3.ID_DATATYPE_STRING,
218 ToscaDictionary.ID_RESOURCE_POLICY_ID,
219 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
221 // This is for the Policy Type
223 MatchType matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
224 XACML3.ID_FUNCTION_STRING_EQUAL,
226 XACML3.ID_DATATYPE_STRING,
227 ToscaDictionary.ID_RESOURCE_POLICY_TYPE,
228 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
230 // This is for the Policy Type version
232 MatchType matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
233 XACML3.ID_FUNCTION_STRING_EQUAL,
235 XACML3.ID_DATATYPE_STRING,
236 ToscaDictionary.ID_RESOURCE_POLICY_TYPE_VERSION,
237 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
239 // This is our outer AnyOf - which is an OR
241 AnyOfType anyOf = new AnyOfType();
243 // Create AllOf (AND) of just Policy Id
245 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyId));
247 // Create AllOf (AND) of just Policy Type
249 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType));
251 // Create AllOf (AND) of Policy Type and Policy Type Version
253 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType, matchPolicyTypeVersion));
255 // Now we can create the TargetType, add the top-level anyOf (OR),
256 // and return the value.
258 TargetType target = new TargetType();
259 target.getAnyOf().add(anyOf);