2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.tutorial.tutorial;
21 import com.att.research.xacml.api.Advice;
22 import com.att.research.xacml.api.DataTypeException;
23 import com.att.research.xacml.api.Decision;
24 import com.att.research.xacml.api.Identifier;
25 import com.att.research.xacml.api.Obligation;
26 import com.att.research.xacml.api.Request;
27 import com.att.research.xacml.api.Response;
28 import com.att.research.xacml.api.Result;
29 import com.att.research.xacml.api.XACML3;
30 import com.att.research.xacml.std.IdentifierImpl;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
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.models.decisions.concepts.DecisionRequest;
42 import org.onap.policy.models.decisions.concepts.DecisionResponse;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
45 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
46 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
47 import org.onap.policy.pdp.xacml.application.common.std.StdBaseTranslator;
49 public class TutorialTranslator extends StdBaseTranslator {
51 private static final Identifier ID_TUTORIAL_USER = new IdentifierImpl(ToscaDictionary.ID_URN_ONAP, "tutorial-user");
52 private static final Identifier ID_TUTORIAL_ENTITY =
53 new IdentifierImpl(ToscaDictionary.ID_URN_ONAP, "tutorial-entity");
54 private static final Identifier ID_TUTORIAL_PERM =
55 new IdentifierImpl(ToscaDictionary.ID_URN_ONAP, "tutorial-permission");
58 * Constructor will setup some defaults.
60 public TutorialTranslator() {
62 // For demonstration purposes, this tutorial will have
63 // the original attributes returned in the request.
65 this.booleanReturnAttributes = true;
66 this.booleanReturnSingleValueAttributesAsCollection = false;
70 * Convert Policy from TOSCA to XACML.
72 @SuppressWarnings("unchecked")
74 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
76 // Here is our policy with a version and default combining algo
78 var newPolicyType = new PolicyType();
79 newPolicyType.setPolicyId(String.valueOf(toscaPolicy.getMetadata().get("policy-id")));
80 newPolicyType.setVersion(String.valueOf(toscaPolicy.getMetadata().get("policy-version")));
82 // When choosing the rule combining algorithm, be sure to be mindful of the
83 // setting xacml.att.policyFinderFactory.combineRootPolicies in the
84 // xacml.properties file. As that choice for ALL the policies together may have
85 // an impact on the decision rendered from each individual policy.
87 // In this case, we will only produce XACML rules for permissions. If no permission
88 // combo exists, then the default is to deny.
90 newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_DENY_UNLESS_PERMIT.stringValue());
92 // Create the target for the Policy.
94 // For simplicity, let's just match on the action "authorize" and the user
97 ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL, "authorize",
98 XACML3.ID_DATATYPE_STRING, XACML3.ID_ACTION_ACTION_ID, XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
99 Map<String, Object> props = toscaPolicy.getProperties();
100 var user = props.get("user").toString();
101 var matchUser = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL, user,
102 XACML3.ID_DATATYPE_STRING, ID_TUTORIAL_USER, XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
103 var anyOf = new AnyOfType();
105 // Create AllOf (AND) of just Policy Id
107 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchAction, matchUser));
108 var target = new TargetType();
109 target.getAnyOf().add(anyOf);
110 newPolicyType.setTarget(target);
112 // Now add the rule for each permission
115 List<Object> permissions = (List<Object>) props.get("permissions");
116 for (Object permission : permissions) {
118 var matchEntity = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL,
119 ((Map<String, String>) permission).get("entity"), XACML3.ID_DATATYPE_STRING, ID_TUTORIAL_ENTITY,
120 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
122 var matchPermission = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL,
123 ((Map<String, String>) permission).get("permission"), XACML3.ID_DATATYPE_STRING, ID_TUTORIAL_PERM,
124 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
125 anyOf = new AnyOfType();
126 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchEntity, matchPermission));
127 target = new TargetType();
128 target.getAnyOf().add(anyOf);
130 var rule = new RuleType();
131 rule.setDescription("Default is to PERMIT if the policy matches.");
132 rule.setRuleId(newPolicyType.getPolicyId() + ":rule" + ruleNumber);
134 rule.setEffect(EffectType.PERMIT);
135 rule.setTarget(target);
137 newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
141 return newPolicyType;
145 * Convert ONAP DecisionRequest to XACML Request.
148 public Request convertRequest(DecisionRequest request) {
150 return TutorialRequest.createRequest(request);
151 } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
158 public DecisionResponse convertResponse(Response xacmlResponse) {
162 if (xacmlResponse.getResults().size() > 1) {
163 return convertMultiResponse(xacmlResponse);
165 return convertSingleResponse(xacmlResponse.getResults().iterator().next());
169 protected DecisionResponse convertSingleResponse(Result xacmlResult) {
170 var decisionResponse = new DecisionResponse();
174 decisionResponse.setPolicies(new HashMap<>());
178 if (xacmlResult.getDecision() == Decision.PERMIT) {
180 // This tutorial will simply set the status to Permit
182 decisionResponse.setStatus(Decision.PERMIT.toString());
185 // This tutorial will simply set the status to Deny
187 decisionResponse.setStatus(Decision.DENY.toString());
190 // Add attributes use the default scanAttributes. Note that one
191 // could override that method and return the structure as desired.
192 // The attributes returned by default method are in the format
193 // of XACML syntax. It may be more desirable to map them back to
194 // the original request name-value.
196 if (booleanReturnAttributes) {
197 scanAttributes(xacmlResult.getAttributes(), decisionResponse);
199 return decisionResponse;
202 protected DecisionResponse convertMultiResponse(Response xacmlResponse) {
203 TutorialResponse decisionResponse = new TutorialResponse();
207 decisionResponse.setPolicies(new HashMap<>());
208 decisionResponse.setStatus("multi");
209 List<TutorialResponsePermission> permissions = new ArrayList<>();
210 for (Result xacmlResult : xacmlResponse.getResults()) {
211 TutorialResponsePermission permission = new TutorialResponsePermission();
215 if (xacmlResult.getDecision() == Decision.PERMIT) {
217 // This tutorial will simply set the status to Permit
219 permission.setStatus(Decision.PERMIT.toString());
222 // This tutorial will simply set the status to Deny
224 permission.setStatus(Decision.DENY.toString());
227 // Add attributes use the default scanAttributes. Note that one
228 // could override that method and return the structure as desired.
229 // The attributes returned by default method are in the format
230 // of XACML syntax. It may be more desirable to map them back to
231 // the original request name-value.
233 if (booleanReturnAttributes) {
235 // Call existing method
237 scanAttributes(xacmlResult.getAttributes(), decisionResponse);
239 // Move from overall response to the individual permission
241 permission.setAttributes(decisionResponse.getAttributes());
242 decisionResponse.setAttributes(null);
247 permissions.add(permission);
249 decisionResponse.setPermissions(permissions);
250 return decisionResponse;
254 protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
256 // No obligations in this tutorial yet.
261 protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
263 // No advice in this tutorial yet.