5ce25facdbcab5e862c57bc67d7e2b7617eeb075
[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 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.xacml.pdp.application.nativ;
24
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.att.research.xacml.util.XACMLPolicyScanner;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Base64;
32 import java.util.Map;
33 import org.onap.policy.models.decisions.concepts.DecisionRequest;
34 import org.onap.policy.models.decisions.concepts.DecisionResponse;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
37 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * This class implements one translator that interprets TOSCA policy and decision API request/response payload.
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  *
46  */
47 public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTranslator.class);
50     private static final String POLICY = "policy";
51
52     public NativePdpApplicationTranslator() {
53         super();
54     }
55
56     @Override
57     public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
58         //
59         // Extract the Base64 encoded policy xml string and decode it
60         //
61         String encodedXacmlPolicy = getNativeXacmlPolicy(toscaPolicy);
62         String decodedXacmlPolicy;
63         try {
64             decodedXacmlPolicy = new String(Base64.getDecoder().decode(encodedXacmlPolicy), StandardCharsets.UTF_8);
65         } catch (IllegalArgumentException exc) {
66             throw new ToscaPolicyConversionException("error on Base64 decoding the native policy", exc);
67         }
68         LOGGER.debug("Decoded xacml policy {}", decodedXacmlPolicy);
69         //
70         // Scan the string and convert to xacml PolicyType
71         //
72         try (ByteArrayInputStream is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
73             //
74             // Read the Policy In
75             //
76             Object policy = XACMLPolicyScanner.readPolicy(is);
77             if (policy == null) {
78                 throw new ToscaPolicyConversionException("Invalid XACML Policy");
79             }
80             return policy;
81         } catch (IOException exc) {
82             throw new ToscaPolicyConversionException("Failed to read policy", exc);
83         }
84     }
85
86     private String getNativeXacmlPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
87
88         Map<String, Object> propertyMap = toscaPolicy.getProperties();
89         if (propertyMap.isEmpty() || !propertyMap.containsKey(POLICY)) {
90             throw new ToscaPolicyConversionException("no xacml native policy found in the tosca policy");
91         }
92
93         String nativePolicyString = propertyMap.get(POLICY).toString();
94         LOGGER.debug("Base64 encoded native xacml policy {}", nativePolicyString);
95         return nativePolicyString;
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 }