[POLICY-72] replace openecomp for drools-pdp
[policy/drools-pdp.git] / policy-management / src / main / java / org / onap / policy / drools / persistence / SystemPersistence.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-management
4  * ================================================================================
5  * Copyright (C) 2017 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.persistence;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.nio.file.StandardCopyOption;
29 import java.util.Properties;
30
31 import org.onap.policy.drools.utils.PropertyUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public interface SystemPersistence {
36         
37         /**
38          * configuration directory
39          */
40         public static String CONFIG_DIR_NAME = "config";
41         
42         /**
43          * policy controllers suffix
44          */
45         public final static String CONTROLLER_SUFFIX_IDENTIFIER = "-controller";
46                 
47         /**
48          * policy controller properties file suffix
49          */
50         public final static String PROPERTIES_FILE_CONTROLLER_SUFFIX = 
51                                                         CONTROLLER_SUFFIX_IDENTIFIER + ".properties"; 
52         
53         /**
54          * policy engine properties file name
55          */
56         public final static String PROPERTIES_FILE_ENGINE = "policy-engine.properties";
57         
58         
59         /**
60          * backs up a controller configuration.
61          * 
62          * @param controllerName the controller name
63          * @return true if the configuration is backed up
64          */
65         public boolean backupController(String controllerName);
66         
67         /**
68          * persists controller configuration
69          * 
70          * @param controllerName the controller name
71          * @param configuration object containing the configuration
72          * @return true if storage is succesful, false otherwise
73          * @throws IllegalArgumentException if the configuration cannot be handled by the persistence manager
74          */
75         public boolean storeController(String controllerName, Object configuration)
76                 throws IllegalArgumentException;
77         
78         /**
79          * delete controller configuration
80          * 
81          * @param controllerName the controller name
82          * @return true if storage is succesful, false otherwise
83          */
84         public boolean deleteController(String controllerName);
85         
86         /**
87          * get controller properties
88          * 
89          * @param controllerName controller name
90          * @return properties for this controller
91          * @throws IllegalArgumentException if the controller name does not lead to a properties configuration
92          */
93         public Properties getControllerProperties(String controllerName)
94                         throws IllegalArgumentException;
95         
96         /**
97          * get properties by name
98          * 
99          * @param name 
100          * @return properties 
101          * @throws IllegalArgumentException if the name does not lead to a properties configuration
102          */
103         public Properties getProperties(String name) throws IllegalArgumentException;
104         
105         /**
106          * Persistence Manager.   For now it is a file-based properties management,
107          * In the future, it will probably be DB based, so manager implementation
108          * will change.
109          */
110         public static final SystemPersistence manager = new SystemPropertiesPersistence();
111 }
112
113 /** 
114  * Properties based Persistence
115  */
116 class SystemPropertiesPersistence implements SystemPersistence {
117         
118         /**
119          * logger 
120          */
121         private static Logger logger = LoggerFactory.getLogger(SystemPropertiesPersistence.class);
122         
123         /**
124          * backs up the properties-based controller configuration
125          * @param controllerName the controller name
126          * @return true if the configuration is backed up in disk or back up does not apply, false otherwise.
127          */
128         @Override
129         public boolean backupController(String controllerName) {
130                 Path controllerPropertiesPath = 
131                                 Paths.get(CONFIG_DIR_NAME, controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
132                 
133                 if (Files.exists(controllerPropertiesPath)) {
134                         try {
135                                 logger.warn("There is an existing configuration file at " + 
136                                         controllerPropertiesPath.toString() +
137                                                     " with contents: " + Files.readAllBytes(controllerPropertiesPath));
138                                 Path controllerPropertiesBakPath = 
139                                                 Paths.get(CONFIG_DIR_NAME, controllerName + 
140                                   PROPERTIES_FILE_CONTROLLER_SUFFIX + ".bak");
141                                 Files.copy(controllerPropertiesPath, 
142                                                    controllerPropertiesBakPath, StandardCopyOption.REPLACE_EXISTING);
143                         } catch (Exception e) {
144                                 logger.warn("{}: cannot be backed up", controllerName, e);
145                         return false;
146                         }
147                 } 
148                 
149                 return true;
150         }
151         
152         /**
153          * persists properties-based controller configuration and makes a backup if necessary
154          * @param controllerName the controller name
155          * @return true if the properties has been stored in disk, false otherwise
156          */
157         @Override
158         public boolean storeController(String controllerName, Object configuration) {
159                 if (!(configuration instanceof Properties)) {
160                         throw new IllegalArgumentException("configuration must be of type properties to be handled by this manager");
161                 }
162                 
163                 Properties properties = (Properties) configuration;
164                 
165                 Path controllerPropertiesPath = 
166                                 Paths.get(CONFIG_DIR_NAME, controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
167         if (Files.exists(controllerPropertiesPath)) {
168                 try {
169                                 Properties oldProperties = PropertyUtil.getProperties(controllerPropertiesPath.toFile());
170                                 if (oldProperties.equals(properties)) {
171                                         logger.info("A properties file with the same contents already exists for controller " + 
172                                                 controllerName + 
173                                                 ". No action is taken");
174                                         return true;
175                                 } else {
176                                         this.backupController(controllerName);
177                                 }
178                         } catch (Exception e) {
179                                 logger.info("{}: no existing Properties", controllerName);
180                                 // continue
181                         }
182         }
183                 
184                 try {
185                 File controllerPropertiesFile = controllerPropertiesPath.toFile();
186                 FileWriter writer = new FileWriter(controllerPropertiesFile);
187                 properties.store(writer, "Machine created Policy Controller Configuration");
188                 } catch (Exception e) {
189                         logger.warn("{}: cannot be STORED", controllerName, e);
190                         return false;
191                 }
192                 
193                 return true;
194         }
195         
196         /**
197          * deletes properties-based controller configuration
198          * @param controllerName the controller name
199          * @return true if the properties has been deleted from disk, false otherwise
200          */
201         @Override
202         public boolean deleteController(String controllerName) {
203                 
204                 Path controllerPropertiesPath = 
205                                 Paths.get(CONFIG_DIR_NAME, 
206                                                   controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
207
208                 if (Files.exists(controllerPropertiesPath)) {
209                         try {
210                                 Path controllerPropertiesBakPath = 
211                                                 Paths.get(CONFIG_DIR_NAME, controllerName + 
212                                   PROPERTIES_FILE_CONTROLLER_SUFFIX + ".bak");
213                                 Files.move(controllerPropertiesPath, 
214                                                    controllerPropertiesBakPath, 
215                                                    StandardCopyOption.REPLACE_EXISTING);
216                         } catch (Exception e) {
217                                 logger.warn("{}: cannot be DELETED", controllerName, e);
218                                 return false;
219                         }
220                 } 
221                 
222                 return true;
223         }
224
225         @Override
226         public Properties getControllerProperties(String controllerName) throws IllegalArgumentException {
227                 return this.getProperties(controllerName + CONTROLLER_SUFFIX_IDENTIFIER);
228         }
229         
230         @Override
231         public Properties getProperties(String name) throws IllegalArgumentException {
232                 Path propertiesPath = 
233                                 Paths.get(CONFIG_DIR_NAME, name + ".properties");
234                 
235                 if (!Files.exists(propertiesPath)) {
236                         throw new IllegalArgumentException("properties for " + name + " are not persisted.");
237                 }
238
239                 try {
240                         return PropertyUtil.getProperties(propertiesPath.toFile());
241                 } catch (Exception e) {
242                         logger.warn("{}: can't read properties @ {}", name, propertiesPath);
243                         throw new IllegalArgumentException("can't read properties for " + 
244                                                            name + " @ " + 
245                                                                    propertiesPath);
246                 }
247         }
248 }