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.std;
25 import com.att.research.xacml.api.DataTypeException;
26 import com.att.research.xacml.api.Request;
27 import com.att.research.xacml.api.XACML3;
28 import com.att.research.xacml.std.annotations.RequestParser;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.models.decisions.concepts.DecisionRequest;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
40 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
41 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
47 private static final Logger LOGGER = LoggerFactory.getLogger(StdCombinedPolicyResultsTranslator.class);
49 public StdCombinedPolicyResultsTranslator() {
54 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
56 // Set it as the policy ID
58 PolicyType newPolicyType = new PolicyType();
59 newPolicyType.setPolicyId(toscaPolicy.getMetadata().get(POLICY_ID));
61 // Optional description
63 newPolicyType.setDescription(toscaPolicy.getDescription());
65 // There should be a metadata section
67 this.fillMetadataSection(newPolicyType, toscaPolicy.getMetadata());
69 // Set the combining rule
71 newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_FIRST_APPLICABLE.stringValue());
73 // Generate the TargetType
75 TargetType target = this.generateTargetType(toscaPolicy.getMetadata().get(POLICY_ID),
76 toscaPolicy.getType(), toscaPolicy.getVersion());
77 newPolicyType.setTarget(target);
79 // Now create the Permit Rule
80 // No target since the policy has a target
83 RuleType rule = new RuleType();
84 rule.setDescription("Default is to PERMIT if the policy matches.");
85 rule.setRuleId(toscaPolicy.getMetadata().get(POLICY_ID) + ":rule");
86 rule.setEffect(EffectType.PERMIT);
87 rule.setTarget(new TargetType());
89 // Now represent the policy as Json
91 StandardCoder coder = new StandardCoder();
94 jsonPolicy = coder.encode(toscaPolicy);
95 } catch (CoderException e) {
96 throw new ToscaPolicyConversionException(e);
98 addObligation(rule, jsonPolicy);
100 // Add the rule to the policy
102 newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
104 // Return our new policy
106 return newPolicyType;
110 public Request convertRequest(DecisionRequest request) {
111 LOGGER.info("Converting Request {}", request);
113 return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request));
114 } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
115 LOGGER.error("Failed to convert DecisionRequest: {}", e);
118 // TODO throw exception
123 protected TargetType generateTargetType(String policyId, String policyType, String policyTypeVersion) {
125 // Create all the match's that are possible
127 // This is for the Policy Id
129 MatchType matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
130 XACML3.ID_FUNCTION_STRING_EQUAL,
132 XACML3.ID_DATATYPE_STRING,
133 ToscaDictionary.ID_RESOURCE_POLICY_ID,
134 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
136 // This is for the Policy Type
138 MatchType matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
139 XACML3.ID_FUNCTION_STRING_EQUAL,
141 XACML3.ID_DATATYPE_STRING,
142 ToscaDictionary.ID_RESOURCE_POLICY_TYPE,
143 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
145 // This is for the Policy Type version
147 MatchType matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
148 XACML3.ID_FUNCTION_STRING_EQUAL,
150 XACML3.ID_DATATYPE_STRING,
151 ToscaDictionary.ID_RESOURCE_POLICY_TYPE_VERSION,
152 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
154 // This is our outer AnyOf - which is an OR
156 AnyOfType anyOf = new AnyOfType();
158 // Create AllOf (AND) of just Policy Id
160 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyId));
162 // Create AllOf (AND) of just Policy Type
164 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType));
166 // Create AllOf (AND) of Policy Type and Policy Type Version
168 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType, matchPolicyTypeVersion));
170 // Now we can create the TargetType, add the top-level anyOf (OR),
171 // and return the value.
173 TargetType target = new TargetType();
174 target.getAnyOf().add(anyOf);