Update XACML Tutorial
[policy/parent.git] / docs / xacml / tutorial / app / src / main / java / org / onap / policy / tutorial / tutorial / TutorialTranslator.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.tutorial.tutorial;
20
21 import java.util.List;
22 import java.util.Map;
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;
45
46 public class TutorialTranslator implements ToscaPolicyTranslator {
47
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-permission");
52
53     @SuppressWarnings("unchecked")
54     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
55         //
56         // Here is our policy with a version and default combining algo
57         //
58         PolicyType newPolicyType = new PolicyType();
59         newPolicyType.setPolicyId(toscaPolicy.getMetadata().get("policy-id"));
60         newPolicyType.setVersion(toscaPolicy.getMetadata().get("policy-version"));
61         //
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.
66         //
67         // In this case, we will only produce XACML rules for permissions. If no permission
68         // combo exists, then the default is to deny.
69         //
70         newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_DENY_UNLESS_PERMIT.stringValue());
71         //
72         // Create the target for the Policy.
73         //
74         // For simplicity, let's just match on the action "authorize" and the user
75         //
76         MatchType matchAction = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(XACML3.ID_FUNCTION_STRING_EQUAL,
77                 "authorize", XACML3.ID_DATATYPE_STRING, XACML3.ID_ACTION_ACTION_ID, 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();
83         //
84         // Create AllOf (AND) of just Policy Id
85         //
86         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchAction, matchUser));
87         TargetType target = new TargetType();
88         target.getAnyOf().add(anyOf);
89         newPolicyType.setTarget(target);
90         //
91         // Now add the rule for each permission
92         //
93         int ruleNumber = 0;
94         List<Object> permissions = (List<Object>) props.get("permissions");
95         for (Object permission : permissions) {
96
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);
100
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, matchPermission));
106             target = new TargetType();
107             target.getAnyOf().add(anyOf);
108
109             RuleType rule = new RuleType();
110             rule.setDescription("Default is to PERMIT if the policy matches.");
111             rule.setRuleId(newPolicyType.getPolicyId() + ":rule" + ruleNumber);
112
113             rule.setEffect(EffectType.PERMIT);
114             rule.setTarget(target);
115
116             newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
117
118             ruleNumber++;
119         }
120         return newPolicyType;
121     }
122
123     public Request convertRequest(DecisionRequest request) {
124         try {
125             return RequestParser.parseRequest(TutorialRequest.createRequest(request));
126         } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
127         }
128         return null;
129     }
130
131     public DecisionResponse convertResponse(Response xacmlResponse) {
132         DecisionResponse decisionResponse = new DecisionResponse();
133         //
134         // Iterate through all the results
135         //
136         for (Result xacmlResult : xacmlResponse.getResults()) {
137             //
138             // Check the result
139             //
140             if (xacmlResult.getDecision() == Decision.PERMIT) {
141                 //
142                 // Just simply return a Permit response
143                 //
144                 decisionResponse.setStatus(Decision.PERMIT.toString());
145             } else {
146                 //
147                 // Just simply return a Deny response
148                 //
149                 decisionResponse.setStatus(Decision.DENY.toString());
150             }
151         }
152
153         return decisionResponse;
154     }
155
156 }