Use protected methods vs private
[policy/xacml-pdp.git] / applications / native / src / main / java / org / onap / policy / xacml / pdp / application / nativ / NativePdpApplicationTranslator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.xacml.pdp.application.nativ;
25
26 import com.att.research.xacml.api.Request;
27 import com.att.research.xacml.api.Response;
28 import com.att.research.xacml.util.XACMLPolicyScanner;
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Base64;
33 import lombok.Getter;
34 import org.onap.policy.common.parameters.annotations.NotBlank;
35 import org.onap.policy.common.parameters.annotations.NotNull;
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37 import org.onap.policy.models.decisions.concepts.DecisionResponse;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
40 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
41 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * This class implements one translator that interprets TOSCA policy and decision API request/response payload.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  *
50  */
51 public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTranslator.class);
54
55     public NativePdpApplicationTranslator() {
56         super();
57     }
58
59     @Override
60     public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
61         //
62         // Extract the Base64 encoded policy xml string and decode it
63         //
64         String encodedXacmlPolicy = getNativeXacmlPolicy(toscaPolicy);
65         String decodedXacmlPolicy;
66         try {
67             decodedXacmlPolicy = new String(Base64.getDecoder().decode(encodedXacmlPolicy), StandardCharsets.UTF_8);
68         } catch (IllegalArgumentException exc) {
69             throw new ToscaPolicyConversionException("error on Base64 decoding the native policy", exc);
70         }
71         LOGGER.debug("Decoded xacml policy {}", decodedXacmlPolicy);
72         //
73         // Scan the string and convert to xacml PolicyType
74         //
75         try (var is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
76             //
77             // Read the Policy In
78             //
79             Object policy = XACMLPolicyScanner.readPolicy(is);
80             if (policy == null) {
81                 throw new ToscaPolicyConversionException("Invalid XACML Policy");
82             }
83             return policy;
84         } catch (IOException exc) {
85             throw new ToscaPolicyConversionException("Failed to read policy", exc);
86         }
87     }
88
89     protected String getNativeXacmlPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
90
91         var nativeDefinition = ToscaPolicyTranslatorUtils.decodeProperties(toscaPolicy.getProperties(),
92                         NativeDefinition.class);
93
94         LOGGER.debug("Base64 encoded native xacml policy {}", nativeDefinition.getPolicy());
95         return nativeDefinition.getPolicy();
96     }
97
98     @Override
99     public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
100         throw new ToscaPolicyConversionException("Do not call native convertRequest");
101     }
102
103     @Override
104     public DecisionResponse convertResponse(Response xacmlResponse) {
105         //
106         // We do nothing to DecisionResponse for native xacml application
107         //
108         return null;
109     }
110
111     @Getter
112     public static class NativeDefinition {
113         @NotNull
114         @NotBlank
115         private String policy;
116     }
117 }