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.pdp.xacml.application.common.XacmlApplicationServiceProvider;
 
  46 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
 
  47 import org.slf4j.Logger;
 
  48 import org.slf4j.LoggerFactory;
 
  50 public class StdXacmlApplicationServiceProvider implements XacmlApplicationServiceProvider {
 
  52     private static final Logger LOGGER = LoggerFactory.getLogger(StdXacmlApplicationServiceProvider.class);
 
  53     private Path pathForData = null;
 
  54     private Properties pdpProperties = null;
 
  55     private PDPEngine pdpEngine = null;
 
  57     public StdXacmlApplicationServiceProvider() {
 
  62     public String applicationName() {
 
  63         return "Please Override";
 
  67     public List<String> actionDecisionsSupported() {
 
  68         return Collections.emptyList();
 
  72     public void initialize(Path pathForData) {
 
  76         this.pathForData = pathForData;
 
  77         LOGGER.debug("New Path is {}", this.pathForData.toAbsolutePath());
 
  79         // Look for and load the properties object
 
  82             pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
 
  83             LOGGER.debug("{}", pdpProperties);
 
  84         } catch (IOException e) {
 
  85             LOGGER.error("{}", e);
 
  90         createEngine(pdpProperties);
 
  94     public List<String> supportedPolicyTypes() {
 
  95         return Collections.emptyList();
 
  99     public boolean canSupportPolicyType(String policyType, String policyTypeVersion) {
 
 104     public void loadPolicies(Map<String, Object> toscaPolicies) {
 
 105         throw new UnsupportedOperationException("Please override and implement loadPolicies");
 
 109     public DecisionResponse makeDecision(DecisionRequest request) {
 
 111         // We should have a standard error response to return
 
 116     protected synchronized PDPEngine getEngine() {
 
 117         return this.pdpEngine;
 
 120     protected synchronized Properties getProperties() {
 
 121         return new Properties(pdpProperties);
 
 124     protected synchronized Path getDataPath() {
 
 129      * Load properties from given file.
 
 131      * @throws IOException If unable to read file
 
 133     protected synchronized Properties loadXacmlProperties() throws IOException {
 
 134         LOGGER.debug("Loading xacml properties {}", pathForData);
 
 135         try (InputStream is = Files.newInputStream(pathForData)) {
 
 136             Properties properties = new Properties();
 
 143      * Stores the XACML Properties to the given file location.
 
 145      * @throws IOException If unable to store the file.
 
 147     protected synchronized void storeXacmlProperties() throws IOException {
 
 148         try (OutputStream os = Files.newOutputStream(pathForData)) {
 
 149             String strComments = "#";
 
 150             pdpProperties.store(os, strComments);
 
 155      * Appends 'xacml.properties' to a root Path object
 
 157      * @return Path to rootPath/xacml.properties file
 
 159     protected synchronized Path getPropertiesPath() {
 
 160         return Paths.get(pathForData.toAbsolutePath().toString(), "xacml.properties");
 
 164      * Creates an instance of PDP engine given the Properties object.
 
 166     protected synchronized void createEngine(Properties properties) {
 
 168         // Now initialize the XACML PDP Engine
 
 171             PDPEngineFactory factory = PDPEngineFactory.newInstance();
 
 172             PDPEngine engine = factory.newEngine(properties);
 
 173             if (engine != null) {
 
 174                 this.pdpEngine = engine;
 
 175                 this.pdpProperties = new Properties(properties);
 
 177         } catch (FactoryException e) {
 
 178             LOGGER.error("Failed to create XACML PDP Engine {}", e);
 
 183      * Make a decision call.
 
 185      * @param request Incoming request object
 
 186      * @return Response object
 
 188     protected synchronized Response xacmlDecision(Request request) {
 
 190         // This is what we need to return
 
 192         Response response = null;
 
 196         long timeStart = System.currentTimeMillis();
 
 198             response = this.pdpEngine.decide(request);
 
 199         } catch (PDPException e) {
 
 200             LOGGER.error("Xacml PDP Engine failed {}", e);
 
 203             // Track the end of timing
 
 205             long timeEnd = System.currentTimeMillis();
 
 206             LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));