Merge "Add Control Loop Coordination policy."
[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 import com.att.research.xacml.util.XACMLPolicyWriter;
32
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.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Properties;
44
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
46
47 import org.onap.policy.models.decisions.concepts.DecisionRequest;
48 import org.onap.policy.models.decisions.concepts.DecisionResponse;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
51 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
52 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
53 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
54 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
55 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public abstract class StdXacmlApplicationServiceProvider implements XacmlApplicationServiceProvider {
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(StdXacmlApplicationServiceProvider.class);
62     private Path pathForData = null;
63     private Properties pdpProperties = null;
64     private PDPEngine pdpEngine = null;
65     private Map<ToscaPolicy, Path> mapLoadedPolicies = new HashMap<>();
66
67     public StdXacmlApplicationServiceProvider() {
68         super();
69     }
70
71     @Override
72     public String applicationName() {
73         return "Please Override";
74     }
75
76     @Override
77     public List<String> actionDecisionsSupported() {
78         return Collections.emptyList();
79     }
80
81     @Override
82     public void initialize(Path pathForData) throws XacmlApplicationException {
83         //
84         // Save our path
85         //
86         this.pathForData = pathForData;
87         LOGGER.info("New Path is {}", this.pathForData.toAbsolutePath());
88         //
89         // Ensure properties exist
90         //
91         Path propertiesPath = XacmlPolicyUtils.getPropertiesPath(pathForData);
92         if (! propertiesPath.toFile().exists()) {
93             LOGGER.info("Copying src/main/resources/xacml.properties to path");
94             //
95             // Properties do not exist, by default we will copy ours over
96             // from src/main/resources
97             //
98             try {
99                 Files.copy(Paths.get("src/main/resources/xacml.properties"), propertiesPath);
100             } catch (IOException e) {
101                 throw new XacmlApplicationException("Failed to copy xacml.propertis", e);
102             }
103         }
104         //
105         // Look for and load the properties object
106         //
107         try {
108             pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
109             LOGGER.debug("{}", pdpProperties);
110         } catch (IOException e) {
111             throw new XacmlApplicationException("Failed to load xacml.propertis", e);
112         }
113         //
114         // Create an engine
115         //
116         createEngine(pdpProperties);
117     }
118
119     @Override
120     public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
121         throw new UnsupportedOperationException("Please override and implement supportedPolicyTypes");
122     }
123
124     @Override
125     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
126         throw new UnsupportedOperationException("Please override and implement canSupportPolicyType");
127     }
128
129     @Override
130     public synchronized boolean loadPolicy(ToscaPolicy toscaPolicy) {
131         try {
132             //
133             // Convert the policies first
134             //
135             PolicyType xacmlPolicy = this.getTranslator(toscaPolicy.getType())
136                 .convertPolicy(toscaPolicy);
137             if (xacmlPolicy == null) {
138                 throw new ToscaPolicyConversionException("Failed to convert policy");
139             }
140             //
141             // Create a copy of the properties object
142             //
143             Properties newProperties = this.getProperties();
144             //
145             // Construct the filename
146             //
147             Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(xacmlPolicy, this.getDataPath());
148             //
149             // Write the policy to disk
150             // Maybe check for an error
151             //
152             XACMLPolicyWriter.writePolicyFile(refPath, xacmlPolicy);
153             if (LOGGER.isDebugEnabled()) {
154                 LOGGER.debug("Xacml Policy is {}{}", System.lineSeparator(), new String(Files.readAllBytes(refPath)));
155             }
156             //
157             // Add root policy to properties object
158             //
159             XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
160             //
161             // Write the properties to disk
162             //
163             XacmlPolicyUtils.storeXacmlProperties(newProperties,
164                     XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
165             //
166             // Reload the engine
167             //
168             this.createEngine(newProperties);
169             //
170             // Save the properties
171             //
172             this.pdpProperties = newProperties;
173             //
174             // Save in our map
175             //
176             this.mapLoadedPolicies.put(toscaPolicy, refPath);
177         } catch (IOException | ToscaPolicyConversionException e) {
178             LOGGER.error("Failed to loadPolicies {}", e);
179             return false;
180         }
181         return true;
182     }
183
184     @Override
185     public synchronized boolean unloadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException {
186         //
187         // Find it in our map
188         //
189         Path refPolicy = this.mapLoadedPolicies.get(toscaPolicy);
190         if (refPolicy == null) {
191             LOGGER.error("Failed to find ToscaPolicy {} in our map size {}", toscaPolicy.getMetadata(),
192                     this.mapLoadedPolicies.size());
193             return false;
194         }
195         //
196         // Create a copy of the properties object
197         //
198         Properties newProperties = this.getProperties();
199         //
200         // Remove it from the properties
201         //
202         XacmlPolicyUtils.removeRootPolicy(newProperties, refPolicy);
203         //
204         // We can delete the file
205         //
206         try {
207             Files.delete(refPolicy);
208         } catch (IOException e) {
209             LOGGER.error("Failed to delete policy {} from disk {}", toscaPolicy.getMetadata(),
210                     refPolicy.toAbsolutePath().toString(), e);
211         }
212         //
213         // Write the properties to disk
214         //
215         try {
216             XacmlPolicyUtils.storeXacmlProperties(newProperties,
217                     XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
218         } catch (IOException e) {
219             LOGGER.error("Failed to save the properties to disk {}", newProperties);
220         }
221         //
222         // Reload the engine
223         //
224         this.createEngine(newProperties);
225         //
226         // Save the properties
227         //
228         this.pdpProperties = newProperties;
229         //
230         // Save in our map
231         //
232         if (this.mapLoadedPolicies.remove(toscaPolicy) == null) {
233             LOGGER.error("Failed to remove toscaPolicy {} from internal map size {}", toscaPolicy.getMetadata(),
234                     this.mapLoadedPolicies.size());
235         }
236         //
237         // Not sure if any of the errors above warrant returning false
238         //
239         return true;
240     }
241
242     @Override
243     public DecisionResponse makeDecision(DecisionRequest request) {
244         //
245         // Convert to a XacmlRequest
246         //
247         Request xacmlRequest = this.getTranslator().convertRequest(request);
248         //
249         // Now get a decision
250         //
251         Response xacmlResponse = this.xacmlDecision(xacmlRequest);
252         //
253         // Convert to a DecisionResponse
254         //
255         return this.getTranslator().convertResponse(xacmlResponse);
256     }
257
258     protected abstract ToscaPolicyTranslator getTranslator(String type);
259
260     protected ToscaPolicyTranslator getTranslator() {
261         return this.getTranslator("");
262     }
263
264     protected synchronized PDPEngine getEngine() {
265         return this.pdpEngine;
266     }
267
268     protected synchronized Properties getProperties() {
269         Properties newProperties = new Properties();
270         newProperties.putAll(pdpProperties);
271         return newProperties;
272     }
273
274     protected synchronized Path getDataPath() {
275         return pathForData;
276     }
277
278     /**
279      * Load properties from given file.
280      *
281      * @throws IOException If unable to read file
282      */
283     protected synchronized Properties loadXacmlProperties() throws IOException {
284         LOGGER.debug("Loading xacml properties {}", pathForData);
285         try (InputStream is = Files.newInputStream(pathForData)) {
286             Properties properties = new Properties();
287             properties.load(is);
288             return properties;
289         }
290     }
291
292     /**
293      * Stores the XACML Properties to the given file location.
294      *
295      * @throws IOException If unable to store the file.
296      */
297     protected synchronized void storeXacmlProperties() throws IOException {
298         try (OutputStream os = Files.newOutputStream(pathForData)) {
299             String strComments = "#";
300             pdpProperties.store(os, strComments);
301         }
302     }
303
304     /**
305      * Appends 'xacml.properties' to a root Path object
306      *
307      * @return Path to rootPath/xacml.properties file
308      */
309     protected synchronized Path getPropertiesPath() {
310         return Paths.get(pathForData.toAbsolutePath().toString(), "xacml.properties");
311     }
312
313     /**
314      * Creates an instance of PDP engine given the Properties object.
315      */
316     protected synchronized void createEngine(Properties properties) {
317         //
318         // Now initialize the XACML PDP Engine
319         //
320         try {
321             PDPEngineFactory factory = PDPEngineFactory.newInstance();
322             PDPEngine engine = factory.newEngine(properties);
323             if (engine != null) {
324                 this.pdpEngine = engine;
325             }
326         } catch (FactoryException e) {
327             LOGGER.error("Failed to create XACML PDP Engine {}", e);
328         }
329     }
330
331     /**
332      * Make a decision call.
333      *
334      * @param request Incoming request object
335      * @return Response object
336      */
337     protected synchronized Response xacmlDecision(Request request) {
338         //
339         // This is what we need to return
340         //
341         Response response = null;
342         //
343         // Track some timing
344         //
345         long timeStart = System.currentTimeMillis();
346         try {
347             response = this.pdpEngine.decide(request);
348         } catch (PDPException e) {
349             LOGGER.error("Xacml PDP Engine failed {}", e);
350         } finally {
351             //
352             // Track the end of timing
353             //
354             long timeEnd = System.currentTimeMillis();
355             LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));
356         }
357         return response;
358     }
359
360 }