2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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 java.util.List;
23 import org.onap.policy.models.decisions.concepts.DecisionRequest;
24 import org.onap.policy.models.decisions.concepts.DecisionResponse;
25 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
26 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
27 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
28 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
29 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
30 import com.att.research.xacml.api.DataTypeException;
31 import com.att.research.xacml.api.Decision;
32 import com.att.research.xacml.api.Identifier;
33 import com.att.research.xacml.api.Request;
34 import com.att.research.xacml.api.Response;
35 import com.att.research.xacml.api.Result;
36 import com.att.research.xacml.api.XACML3;
37 import com.att.research.xacml.std.IdentifierImpl;
38 import com.att.research.xacml.std.annotations.RequestParser;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
46 public class TutorialTranslator implements ToscaPolicyTranslator {
48 private static final Identifier ID_TUTORIAL_USER = new IdentifierImpl(ToscaDictionary.ID_URN_ONAP, "tutorial-user");
49 private static final Identifier ID_TUTORIAL_ENTITY =
50 new IdentifierImpl(ToscaDictionary.ID_URN_ONAP, "tutorial-entity");
51 private static final Identifier ID_TUTORIAL_PERM = new IdentifierImpl(ToscaDictionary.ID_URN_ONAP, "tutorial-perm");
53 @SuppressWarnings("unchecked")
54 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
56 // Here is our policy with a version and default combining algo
58 PolicyType newPolicyType = new PolicyType();
59 newPolicyType.setPolicyId(toscaPolicy.getMetadata().get("policy-id"));
60 newPolicyType.setVersion(toscaPolicy.getMetadata().get("policy-version"));
62 // When choosing the rule combining algorithm, be sure to be mindful of the
63 // setting xacml.att.policyFinderFactory.combineRootPolicies in the
64 // xacml.properties file. As that choice for ALL the policies together may have
65 // an impact on the decision rendered from each individual policy.
67 // In this case, we will only produce XACML rules for permissions. If no permission
68 // combo exists, then the default is to deny.
70 newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_DENY_UNLESS_PERMIT.stringValue());
72 // Create the target for the Policy.
74 // For simplicity, let's just match on the action "authorize" and the user
76 MatchType matchAction = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL,
77 "authorize", XACML3.ID_DATATYPE_STRING, XACML3.ID_ACTION, XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
78 Map<String, Object> props = toscaPolicy.getProperties();
79 String user = props.get("user").toString();
80 MatchType matchUser = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL, user,
81 XACML3.ID_DATATYPE_STRING, ID_TUTORIAL_USER, XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
82 AnyOfType anyOf = new AnyOfType();
84 // Create AllOf (AND) of just Policy Id
86 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchAction));
87 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchUser));
88 TargetType target = new TargetType();
89 target.getAnyOf().add(anyOf);
90 newPolicyType.setTarget(target);
92 // Now add the rule for each permission
94 List<Object> permissions = (List<Object>) props.get("permissions");
95 for (Object permission : permissions) {
97 MatchType matchEntity = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL,
98 ((Map<String, String>) permission).get("entity"), XACML3.ID_DATATYPE_STRING, ID_TUTORIAL_ENTITY,
99 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
101 MatchType matchPermission = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
102 XACML3.ID_FUNCTION_STRING_EQUAL, ((Map<String, String>) permission).get("permission"),
103 XACML3.ID_DATATYPE_STRING, ID_TUTORIAL_PERM, XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
104 anyOf = new AnyOfType();
105 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchEntity));
106 anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPermission));
107 target = new TargetType();
108 target.getAnyOf().add(anyOf);
110 RuleType rule = new RuleType();
111 rule.setDescription("Default is to PERMIT if the policy matches.");
112 rule.setRuleId(newPolicyType.getPolicyId() + ":rule");
113 rule.setEffect(EffectType.PERMIT);
114 rule.setTarget(target);
116 newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
118 return newPolicyType;
121 public Request convertRequest(DecisionRequest request) {
123 return RequestParser.parseRequest(TutorialRequest.createRequest(request));
124 } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
129 public DecisionResponse convertResponse(Response xacmlResponse) {
130 DecisionResponse decisionResponse = new DecisionResponse();
132 // Iterate through all the results
134 for (Result xacmlResult : xacmlResponse.getResults()) {
138 if (xacmlResult.getDecision() == Decision.PERMIT) {
140 // Just simply return a Permit response
142 decisionResponse.setStatus(Decision.PERMIT.toString());
144 if (xacmlResult.getDecision() == Decision.DENY) {
146 // Just simply return a Deny response
148 decisionResponse.setStatus(Decision.DENY.toString());
150 if (xacmlResult.getDecision() == Decision.NOTAPPLICABLE) {
152 // There is no guard policy, so we return a permit
154 decisionResponse.setStatus(Decision.PERMIT.toString());
158 return decisionResponse;