546c29eba5672f8fbe1e03dda70f474917a8eda4
[policy/xacml-pdp.git] /
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.io.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Base64;
33 import java.util.Map;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
35 import org.onap.policy.models.decisions.concepts.DecisionRequest;
36 import org.onap.policy.models.decisions.concepts.DecisionResponse;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
39 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This class implements one translator that interprets TOSCA policy and decision API request/response payload.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  *
48  */
49 public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTranslator.class);
52     private static final String POLICY = "policy";
53
54     public NativePdpApplicationTranslator() {
55         super();
56     }
57
58     @Override
59     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
60         //
61         // Extract the Base64 encoded policy xml string and decode it
62         //
63         String encodedXacmlPolicy = getNativeXacmlPolicy(toscaPolicy);
64         String decodedXacmlPolicy;
65         try {
66             decodedXacmlPolicy = new String(Base64.getDecoder().decode(encodedXacmlPolicy), StandardCharsets.UTF_8);
67         } catch (IllegalArgumentException exc) {
68             throw new ToscaPolicyConversionException("error on Base64 decoding the native policy", exc);
69         }
70         LOGGER.debug("Decoded xacml policy {}",decodedXacmlPolicy);
71         //
72         // Scan the string and convert to xacml PolicyType
73         //
74         try (InputStream is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
75             //
76             // Here we assume it is PolicyType, not PolicySetType
77             // PolicySetType will be addressed later
78             //
79             return (PolicyType) XACMLPolicyScanner.readPolicy(is);
80         } catch (IOException exc) {
81             throw new ToscaPolicyConversionException("Failed to read policy", exc);
82         }
83     }
84
85     private String getNativeXacmlPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
86
87         Map<String, Object> propertyMap = toscaPolicy.getProperties();
88         if (propertyMap.isEmpty() || !propertyMap.containsKey(POLICY)) {
89             throw new ToscaPolicyConversionException("no xacml native policy found in the tosca policy");
90         }
91
92         String nativePolicyString = propertyMap.get(POLICY).toString();
93         LOGGER.debug("Base64 encoded native xacml policy {}", nativePolicyString);
94         return nativePolicyString;
95     }
96
97     @Override
98     public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
99         throw new ToscaPolicyConversionException("Do not call native convertRequest");
100     }
101
102     @Override
103     public DecisionResponse convertResponse(Response xacmlResponse) {
104         //
105         // We do nothing to DecisionResponse for native xacml application
106         //
107         return null;
108     }
109 }