Consolidate common translatable code some sonar
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdCombinedPolicyResultsTranslator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.xacml.application.common.std;
24
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;
44
45 public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(StdCombinedPolicyResultsTranslator.class);
48
49     public StdCombinedPolicyResultsTranslator() {
50         super();
51     }
52
53     @Override
54     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
55         //
56         // Set it as the policy ID
57         //
58         PolicyType newPolicyType = new PolicyType();
59         newPolicyType.setPolicyId(toscaPolicy.getMetadata().get(POLICY_ID));
60         //
61         // Optional description
62         //
63         newPolicyType.setDescription(toscaPolicy.getDescription());
64         //
65         // There should be a metadata section
66         //
67         this.fillMetadataSection(newPolicyType, toscaPolicy.getMetadata());
68         //
69         // Set the combining rule
70         //
71         newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_FIRST_APPLICABLE.stringValue());
72         //
73         // Generate the TargetType
74         //
75         TargetType target = this.generateTargetType(toscaPolicy.getMetadata().get(POLICY_ID),
76                 toscaPolicy.getType(), toscaPolicy.getVersion());
77         newPolicyType.setTarget(target);
78         //
79         // Now create the Permit Rule
80         // No target since the policy has a target
81         // With obligations.
82         //
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());
88         //
89         // Now represent the policy as Json
90         //
91         StandardCoder coder = new StandardCoder();
92         String jsonPolicy;
93         try {
94             jsonPolicy = coder.encode(toscaPolicy);
95         } catch (CoderException e) {
96             throw new ToscaPolicyConversionException(e);
97         }
98         addObligation(rule, jsonPolicy);
99         //
100         // Add the rule to the policy
101         //
102         newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
103         //
104         // Return our new policy
105         //
106         return newPolicyType;
107     }
108
109     @Override
110     public Request convertRequest(DecisionRequest request) {
111         LOGGER.info("Converting Request {}", request);
112         try {
113             return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request));
114         } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
115             LOGGER.error("Failed to convert DecisionRequest: {}", e);
116         }
117         //
118         // TODO throw exception
119         //
120         return null;
121     }
122
123     protected TargetType generateTargetType(String policyId, String policyType, String policyTypeVersion) {
124         //
125         // Create all the match's that are possible
126         //
127         // This is for the Policy Id
128         //
129         MatchType matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
130                 XACML3.ID_FUNCTION_STRING_EQUAL,
131                 policyId,
132                 XACML3.ID_DATATYPE_STRING,
133                 ToscaDictionary.ID_RESOURCE_POLICY_ID,
134                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
135         //
136         // This is for the Policy Type
137         //
138         MatchType matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
139                 XACML3.ID_FUNCTION_STRING_EQUAL,
140                 policyType,
141                 XACML3.ID_DATATYPE_STRING,
142                 ToscaDictionary.ID_RESOURCE_POLICY_TYPE,
143                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
144         //
145         // This is for the Policy Type version
146         //
147         MatchType matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
148                 XACML3.ID_FUNCTION_STRING_EQUAL,
149                 policyTypeVersion,
150                 XACML3.ID_DATATYPE_STRING,
151                 ToscaDictionary.ID_RESOURCE_POLICY_TYPE_VERSION,
152                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
153         //
154         // This is our outer AnyOf - which is an OR
155         //
156         AnyOfType anyOf = new AnyOfType();
157         //
158         // Create AllOf (AND) of just Policy Id
159         //
160         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyId));
161         //
162         // Create AllOf (AND) of just Policy Type
163         //
164         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType));
165         //
166         // Create AllOf (AND) of Policy Type and Policy Type Version
167         //
168         anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(matchPolicyType, matchPolicyTypeVersion));
169         //
170         // Now we can create the TargetType, add the top-level anyOf (OR),
171         // and return the value.
172         //
173         TargetType target = new TargetType();
174         target.getAnyOf().add(anyOf);
175         return target;
176     }
177
178 }