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;
31 import com.att.research.xacml.util.XACMLPolicyWriter;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.Properties;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
45 import org.onap.policy.models.decisions.concepts.DecisionRequest;
46 import org.onap.policy.models.decisions.concepts.DecisionResponse;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
50 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
52 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
53 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public abstract class StdXacmlApplicationServiceProvider implements XacmlApplicationServiceProvider {
59 private static final Logger LOGGER = LoggerFactory.getLogger(StdXacmlApplicationServiceProvider.class);
60 private Path pathForData = null;
61 private Properties pdpProperties = null;
62 private PDPEngine pdpEngine = null;
64 public StdXacmlApplicationServiceProvider() {
69 public String applicationName() {
70 return "Please Override";
74 public List<String> actionDecisionsSupported() {
75 return Collections.emptyList();
79 public void initialize(Path pathForData) throws XacmlApplicationException {
83 this.pathForData = pathForData;
84 LOGGER.info("New Path is {}", this.pathForData.toAbsolutePath());
86 // Ensure properties exist
88 Path propertiesPath = XacmlPolicyUtils.getPropertiesPath(pathForData);
89 if (! propertiesPath.toFile().exists()) {
90 LOGGER.info("Copying src/main/resources/xacml.properties to path");
92 // Properties do not exist, by default we will copy ours over
93 // from src/main/resources
96 Files.copy(Paths.get("src/main/resources/xacml.properties"), propertiesPath);
97 } catch (IOException e) {
98 throw new XacmlApplicationException("Failed to copy xacml.propertis", e);
102 // Look for and load the properties object
105 pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
106 LOGGER.debug("{}", pdpProperties);
107 } catch (IOException e) {
108 throw new XacmlApplicationException("Failed to load xacml.propertis", e);
113 createEngine(pdpProperties);
117 public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
118 return Collections.emptyList();
122 public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
123 throw new UnsupportedOperationException("Please override and implement canSupportPolicyType");
127 public synchronized void loadPolicy(ToscaPolicy toscaPolicy) {
130 // Convert the policies first
132 PolicyType xacmlPolicy = this.getTranslator().convertPolicy(toscaPolicy);
133 if (xacmlPolicy == null) {
134 throw new ToscaPolicyConversionException("Failed to convert policy");
137 // Create a copy of the properties object
139 Properties newProperties = this.getProperties();
141 // Construct the filename
143 Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(xacmlPolicy, this.getDataPath());
145 // Write the policy to disk
146 // Maybe check for an error
148 XACMLPolicyWriter.writePolicyFile(refPath, xacmlPolicy);
149 if (LOGGER.isDebugEnabled()) {
150 LOGGER.debug("Xacml Policy is {}{}", System.lineSeparator(), new String(Files.readAllBytes(refPath)));
153 // Add root policy to properties object
155 XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
157 // Write the properties to disk
159 XacmlPolicyUtils.storeXacmlProperties(newProperties,
160 XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
164 this.createEngine(newProperties);
166 // Save the properties
168 this.pdpProperties = newProperties;
169 } catch (IOException | ToscaPolicyConversionException e) {
170 LOGGER.error("Failed to loadPolicies {}", e);
175 public synchronized DecisionResponse makeDecision(DecisionRequest request) {
177 // Convert to a XacmlRequest
179 Request xacmlRequest = this.getTranslator().convertRequest(request);
181 // Now get a decision
183 Response xacmlResponse = this.xacmlDecision(xacmlRequest);
185 // Convert to a DecisionResponse
187 return this.getTranslator().convertResponse(xacmlResponse);
191 protected abstract ToscaPolicyTranslator getTranslator();
193 protected synchronized PDPEngine getEngine() {
194 return this.pdpEngine;
197 protected synchronized Properties getProperties() {
198 Properties newProperties = new Properties();
199 newProperties.putAll(pdpProperties);
200 return newProperties;
203 protected synchronized Path getDataPath() {
208 * Load properties from given file.
210 * @throws IOException If unable to read file
212 protected synchronized Properties loadXacmlProperties() throws IOException {
213 LOGGER.debug("Loading xacml properties {}", pathForData);
214 try (InputStream is = Files.newInputStream(pathForData)) {
215 Properties properties = new Properties();
222 * Stores the XACML Properties to the given file location.
224 * @throws IOException If unable to store the file.
226 protected synchronized void storeXacmlProperties() throws IOException {
227 try (OutputStream os = Files.newOutputStream(pathForData)) {
228 String strComments = "#";
229 pdpProperties.store(os, strComments);
234 * Appends 'xacml.properties' to a root Path object
236 * @return Path to rootPath/xacml.properties file
238 protected synchronized Path getPropertiesPath() {
239 return Paths.get(pathForData.toAbsolutePath().toString(), "xacml.properties");
243 * Creates an instance of PDP engine given the Properties object.
245 protected synchronized void createEngine(Properties properties) {
247 // Now initialize the XACML PDP Engine
250 PDPEngineFactory factory = PDPEngineFactory.newInstance();
251 PDPEngine engine = factory.newEngine(properties);
252 if (engine != null) {
253 this.pdpEngine = engine;
254 // this.pdpProperties = new Properties(properties);
256 } catch (FactoryException e) {
257 LOGGER.error("Failed to create XACML PDP Engine {}", e);
262 * Make a decision call.
264 * @param request Incoming request object
265 * @return Response object
267 protected synchronized Response xacmlDecision(Request request) {
269 // This is what we need to return
271 Response response = null;
275 long timeStart = System.currentTimeMillis();
277 response = this.pdpEngine.decide(request);
278 } catch (PDPException e) {
279 LOGGER.error("Xacml PDP Engine failed {}", e);
282 // Track the end of timing
284 long timeEnd = System.currentTimeMillis();
285 LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));