2 * ============LICENSE_START=======================================================
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
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.pdp.xacml.application.common.std;
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.att.research.xacml.api.pdp.PDPEngine;
28 import com.att.research.xacml.api.pdp.PDPEngineFactory;
29 import com.att.research.xacml.api.pdp.PDPException;
30 import com.att.research.xacml.util.FactoryException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.Collections;
39 import java.util.List;
41 import java.util.Properties;
43 import org.onap.policy.models.decisions.concepts.DecisionRequest;
44 import org.onap.policy.models.decisions.concepts.DecisionResponse;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
46 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
47 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
48 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class StdXacmlApplicationServiceProvider implements XacmlApplicationServiceProvider {
54 private static final Logger LOGGER = LoggerFactory.getLogger(StdXacmlApplicationServiceProvider.class);
55 private Path pathForData = null;
56 private Properties pdpProperties = null;
57 private PDPEngine pdpEngine = null;
59 public StdXacmlApplicationServiceProvider() {
64 public String applicationName() {
65 return "Please Override";
69 public List<String> actionDecisionsSupported() {
70 return Collections.emptyList();
74 public void initialize(Path pathForData) throws XacmlApplicationException {
78 this.pathForData = pathForData;
79 LOGGER.info("New Path is {}", this.pathForData.toAbsolutePath());
81 // Ensure properties exist
83 Path propertiesPath = XacmlPolicyUtils.getPropertiesPath(pathForData);
84 if (! propertiesPath.toFile().exists()) {
85 LOGGER.info("Copying src/main/resources/xacml.properties to path");
87 // Properties do not exist, by default we will copy ours over
88 // from src/main/resources
91 Files.copy(Paths.get("src/main/resources/xacml.properties"), propertiesPath);
92 } catch (IOException e) {
93 throw new XacmlApplicationException("Failed to copy xacml.propertis", e);
97 // Look for and load the properties object
100 pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
101 LOGGER.debug("{}", pdpProperties);
102 } catch (IOException e) {
103 throw new XacmlApplicationException("Failed to load xacml.propertis", e);
108 createEngine(pdpProperties);
112 public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
113 return Collections.emptyList();
117 public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
122 public void loadPolicies(Map<String, Object> toscaPolicies) throws XacmlApplicationException {
123 throw new UnsupportedOperationException("Please override and implement loadPolicies");
127 public DecisionResponse makeDecision(DecisionRequest request) {
129 // We should have a standard error response to return
134 protected synchronized PDPEngine getEngine() {
135 return this.pdpEngine;
138 protected synchronized Properties getProperties() {
139 return new Properties(pdpProperties);
142 protected synchronized Path getDataPath() {
147 * Load properties from given file.
149 * @throws IOException If unable to read file
151 protected synchronized Properties loadXacmlProperties() throws IOException {
152 LOGGER.debug("Loading xacml properties {}", pathForData);
153 try (InputStream is = Files.newInputStream(pathForData)) {
154 Properties properties = new Properties();
161 * Stores the XACML Properties to the given file location.
163 * @throws IOException If unable to store the file.
165 protected synchronized void storeXacmlProperties() throws IOException {
166 try (OutputStream os = Files.newOutputStream(pathForData)) {
167 String strComments = "#";
168 pdpProperties.store(os, strComments);
173 * Appends 'xacml.properties' to a root Path object
175 * @return Path to rootPath/xacml.properties file
177 protected synchronized Path getPropertiesPath() {
178 return Paths.get(pathForData.toAbsolutePath().toString(), "xacml.properties");
182 * Creates an instance of PDP engine given the Properties object.
184 protected synchronized void createEngine(Properties properties) {
186 // Now initialize the XACML PDP Engine
189 PDPEngineFactory factory = PDPEngineFactory.newInstance();
190 PDPEngine engine = factory.newEngine(properties);
191 if (engine != null) {
192 this.pdpEngine = engine;
193 this.pdpProperties = new Properties(properties);
195 } catch (FactoryException e) {
196 LOGGER.error("Failed to create XACML PDP Engine {}", e);
201 * Make a decision call.
203 * @param request Incoming request object
204 * @return Response object
206 protected synchronized Response xacmlDecision(Request request) {
208 // This is what we need to return
210 Response response = null;
214 long timeStart = System.currentTimeMillis();
216 response = this.pdpEngine.decide(request);
217 } catch (PDPException e) {
218 LOGGER.error("Xacml PDP Engine failed {}", e);
221 // Track the end of timing
223 long timeEnd = System.currentTimeMillis();
224 LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));