Merge "Use Policy Translator abstract class"
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdXacmlApplicationServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.pdp.xacml.application.common.std;
24
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
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;
40 import java.util.Map;
41 import java.util.Properties;
42
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;
49
50 public class StdXacmlApplicationServiceProvider implements XacmlApplicationServiceProvider {
51
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;
56
57     public StdXacmlApplicationServiceProvider() {
58         super();
59     }
60
61     @Override
62     public String applicationName() {
63         return "Please Override";
64     }
65
66     @Override
67     public List<String> actionDecisionsSupported() {
68         return Collections.emptyList();
69     }
70
71     @Override
72     public void initialize(Path pathForData) {
73         //
74         // Save our path
75         //
76         this.pathForData = pathForData;
77         LOGGER.debug("New Path is {}", this.pathForData.toAbsolutePath());
78         //
79         // Look for and load the properties object
80         //
81         try {
82             pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
83             LOGGER.debug("{}", pdpProperties);
84         } catch (IOException e) {
85             LOGGER.error("{}", e);
86         }
87         //
88         // Create an engine
89         //
90         createEngine(pdpProperties);
91     }
92
93     @Override
94     public List<String> supportedPolicyTypes() {
95         return Collections.emptyList();
96     }
97
98     @Override
99     public boolean canSupportPolicyType(String policyType, String policyTypeVersion) {
100         return false;
101     }
102
103     @Override
104     public void loadPolicies(Map<String, Object> toscaPolicies) {
105         throw new UnsupportedOperationException("Please override and implement loadPolicies");
106     }
107
108     @Override
109     public DecisionResponse makeDecision(DecisionRequest request) {
110         //
111         // We should have a standard error response to return
112         //
113         return null;
114     }
115
116     protected synchronized PDPEngine getEngine() {
117         return this.pdpEngine;
118     }
119
120     protected synchronized Properties getProperties() {
121         return new Properties(pdpProperties);
122     }
123
124     protected synchronized Path getDataPath() {
125         return pathForData;
126     }
127
128     /**
129      * Load properties from given file.
130      *
131      * @throws IOException If unable to read file
132      */
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();
137             properties.load(is);
138             return properties;
139         }
140     }
141
142     /**
143      * Stores the XACML Properties to the given file location.
144      *
145      * @throws IOException If unable to store the file.
146      */
147     protected synchronized void storeXacmlProperties() throws IOException {
148         try (OutputStream os = Files.newOutputStream(pathForData)) {
149             String strComments = "#";
150             pdpProperties.store(os, strComments);
151         }
152     }
153
154     /**
155      * Appends 'xacml.properties' to a root Path object
156      *
157      * @return Path to rootPath/xacml.properties file
158      */
159     protected synchronized Path getPropertiesPath() {
160         return Paths.get(pathForData.toAbsolutePath().toString(), "xacml.properties");
161     }
162
163     /**
164      * Creates an instance of PDP engine given the Properties object.
165      */
166     protected synchronized void createEngine(Properties properties) {
167         //
168         // Now initialize the XACML PDP Engine
169         //
170         try {
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);
176             }
177         } catch (FactoryException e) {
178             LOGGER.error("Failed to create XACML PDP Engine {}", e);
179         }
180     }
181
182     /**
183      * Make a decision call.
184      *
185      * @param request Incoming request object
186      * @return Response object
187      */
188     protected synchronized Response xacmlDecision(Request request) {
189         //
190         // This is what we need to return
191         //
192         Response response = null;
193         //
194         // Track some timing
195         //
196         long timeStart = System.currentTimeMillis();
197         try {
198             response = this.pdpEngine.decide(request);
199         } catch (PDPException e) {
200             LOGGER.error("Xacml PDP Engine failed {}", e);
201         } finally {
202             //
203             // Track the end of timing
204             //
205             long timeEnd = System.currentTimeMillis();
206             LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));
207         }
208         return response;
209     }
210
211 }