2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.nativ;
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;
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;
42 * This class implements one translator that interprets TOSCA policy and decision API request/response payload.
44 * @author Chenfei Gao (cgao@research.att.com)
47 public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
49 private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTranslator.class);
50 private static final String POLICY = "policy";
52 public NativePdpApplicationTranslator() {
57 public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
59 // Extract the Base64 encoded policy xml string and decode it
61 String encodedXacmlPolicy = getNativeXacmlPolicy(toscaPolicy);
62 String decodedXacmlPolicy;
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);
68 LOGGER.debug("Decoded xacml policy {}", decodedXacmlPolicy);
70 // Scan the string and convert to xacml PolicyType
72 try (ByteArrayInputStream is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
76 Object policy = XACMLPolicyScanner.readPolicy(is);
78 throw new ToscaPolicyConversionException("Invalid XACML Policy");
81 } catch (IOException exc) {
82 throw new ToscaPolicyConversionException("Failed to read policy", exc);
86 private String getNativeXacmlPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
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");
93 String nativePolicyString = propertyMap.get(POLICY).toString();
94 LOGGER.debug("Base64 encoded native xacml policy {}", nativePolicyString);
95 return nativePolicyString;
99 public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
100 throw new ToscaPolicyConversionException("Do not call native convertRequest");
104 public DecisionResponse convertResponse(Response xacmlResponse) {
106 // We do nothing to DecisionResponse for native xacml application