2 * ============LICENSE_START=======================================================
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
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.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.persistence;
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;
31 import org.onap.policy.drools.utils.PropertyUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public interface SystemPersistence {
38 * configuration directory
40 public static String CONFIG_DIR_NAME = "config";
43 * policy controllers suffix
45 public final static String CONTROLLER_SUFFIX_IDENTIFIER = "-controller";
48 * policy controller properties file suffix
50 public final static String PROPERTIES_FILE_CONTROLLER_SUFFIX =
51 CONTROLLER_SUFFIX_IDENTIFIER + ".properties";
54 * policy engine properties file name
56 public final static String PROPERTIES_FILE_ENGINE = "policy-engine.properties";
60 * backs up a controller configuration.
62 * @param controllerName the controller name
63 * @return true if the configuration is backed up
65 public boolean backupController(String controllerName);
68 * persists controller configuration
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
75 public boolean storeController(String controllerName, Object configuration)
76 throws IllegalArgumentException;
79 * delete controller configuration
81 * @param controllerName the controller name
82 * @return true if storage is succesful, false otherwise
84 public boolean deleteController(String controllerName);
87 * get controller properties
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
93 public Properties getControllerProperties(String controllerName)
94 throws IllegalArgumentException;
97 * get properties by name
101 * @throws IllegalArgumentException if the name does not lead to a properties configuration
103 public Properties getProperties(String name) throws IllegalArgumentException;
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
110 public static final SystemPersistence manager = new SystemPropertiesPersistence();
114 * Properties based Persistence
116 class SystemPropertiesPersistence implements SystemPersistence {
121 private static Logger logger = LoggerFactory.getLogger(SystemPropertiesPersistence.class);
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.
129 public boolean backupController(String controllerName) {
130 Path controllerPropertiesPath =
131 Paths.get(CONFIG_DIR_NAME, controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
133 if (Files.exists(controllerPropertiesPath)) {
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);
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
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");
163 Properties properties = (Properties) configuration;
165 Path controllerPropertiesPath =
166 Paths.get(CONFIG_DIR_NAME, controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
167 if (Files.exists(controllerPropertiesPath)) {
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 " +
173 ". No action is taken");
176 this.backupController(controllerName);
178 } catch (Exception e) {
179 logger.info("{}: no existing Properties", controllerName);
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);
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
202 public boolean deleteController(String controllerName) {
204 Path controllerPropertiesPath =
205 Paths.get(CONFIG_DIR_NAME,
206 controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
208 if (Files.exists(controllerPropertiesPath)) {
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);
226 public Properties getControllerProperties(String controllerName) throws IllegalArgumentException {
227 return this.getProperties(controllerName + CONTROLLER_SUFFIX_IDENTIFIER);
231 public Properties getProperties(String name) throws IllegalArgumentException {
232 Path propertiesPath =
233 Paths.get(CONFIG_DIR_NAME, name + ".properties");
235 if (!Files.exists(propertiesPath)) {
236 throw new IllegalArgumentException("properties for " + name + " are not persisted.");
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 " +