aboutsummaryrefslogtreecommitdiffstats
path: root/policy-management/src/main/java/org/openecomp/policy/drools/persistence/SystemPersistence.java
blob: 6bb1185d7fafb97ac8f02c5d5a77a740a9ca5831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*-
 * ============LICENSE_START=======================================================
 * policy-management
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.drools.persistence;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Properties;

import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
import org.openecomp.policy.common.logging.flexlogger.Logger;
import org.openecomp.policy.common.logging.eelf.MessageCodes;
import org.openecomp.policy.drools.utils.PropertyUtil;

public interface SystemPersistence {
	
	/**
	 * configuration directory
	 */
	public static String CONFIG_DIR_NAME = "config";
	
	/**
	 * policy controllers suffix
	 */
	public final static String CONTROLLER_SUFFIX_IDENTIFIER = "-controller";
		
	/**
	 * policy controller properties file suffix
	 */
	public final static String PROPERTIES_FILE_CONTROLLER_SUFFIX = 
							CONTROLLER_SUFFIX_IDENTIFIER + ".properties"; 
	
	/**
	 * policy engine properties file name
	 */
	public final static String PROPERTIES_FILE_ENGINE = "policy-engine.properties";
	
	
	/**
	 * backs up a controller configuration.
	 * 
	 * @param controllerName the controller name
	 * @return true if the configuration is backed up
	 */
	public boolean backupController(String controllerName);
	
	/**
	 * persists controller configuration
	 * 
	 * @param controllerName the controller name
	 * @param configuration object containing the configuration
	 * @return true if storage is succesful, false otherwise
	 * @throws IllegalArgumentException if the configuration cannot be handled by the persistence manager
	 */
	public boolean storeController(String controllerName, Object configuration)
		throws IllegalArgumentException;
	
	/**
	 * delete controller configuration
	 * 
	 * @param controllerName the controller name
	 * @return true if storage is succesful, false otherwise
	 */
	public boolean deleteController(String controllerName);
	
	/**
	 * get controller properties
	 * 
	 * @param controllerName controller name
	 * @return properties for this controller
	 * @throws IllegalArgumentException if the controller name does not lead to a properties configuration
	 */
	public Properties getControllerProperties(String controllerName)
			throws IllegalArgumentException;
	
	/**
	 * get properties by name
	 * 
	 * @param name 
	 * @return properties 
	 * @throws IllegalArgumentException if the name does not lead to a properties configuration
	 */
	public Properties getProperties(String name) throws IllegalArgumentException;
	
	/**
	 * Persistence Manager.   For now it is a file-based properties management,
	 * In the future, it will probably be DB based, so manager implementation
	 * will change.
	 */
	public static final SystemPersistence manager = new SystemPropertiesPersistence();
}

/** 
 * Properties based Persistence
 */
class SystemPropertiesPersistence implements SystemPersistence {
	
	/**
	 * logger 
	 */
	private static Logger logger = FlexLogger.getLogger(SystemPropertiesPersistence.class);
	
	/**
	 * backs up the properties-based controller configuration
	 * @param controllerName the controller name
	 * @return true if the configuration is backed up in disk or back up does not apply, false otherwise.
	 */
	@Override
	public boolean backupController(String controllerName) {
	   	Path controllerPropertiesPath = 
	   			Paths.get(CONFIG_DIR_NAME, controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
	   	
		if (Files.exists(controllerPropertiesPath)) {
			try {
				logger.warn("There is an existing configuration file at " + 
			                controllerPropertiesPath.toString() +
						    " with contents: " + Files.readAllBytes(controllerPropertiesPath));
				Path controllerPropertiesBakPath = 
						Paths.get(CONFIG_DIR_NAME, controllerName + 
                                  PROPERTIES_FILE_CONTROLLER_SUFFIX + ".bak");
				Files.copy(controllerPropertiesPath, 
						   controllerPropertiesBakPath, StandardCopyOption.REPLACE_EXISTING);
			} catch (Exception e) {
				logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
		                    controllerName, "SystemPersistenceProperties");
	    		return false;
			}
		} 
		
		return true;
	}
	
	/**
	 * persists properties-based controller configuration and makes a backup if necessary
	 * @param controllerName the controller name
	 * @return true if the properties has been stored in disk, false otherwise
	 */
	@Override
	public boolean storeController(String controllerName, Object configuration) {
	   	if (!(configuration instanceof Properties)) {
	   		throw new IllegalArgumentException("configuration must be of type properties to be handled by this manager");
	   	}
	   	
	   	Properties properties = (Properties) configuration;
	   	
		Path controllerPropertiesPath = 
	   			Paths.get(CONFIG_DIR_NAME, controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);
    	if (Files.exists(controllerPropertiesPath)) {
    		try {
				Properties oldProperties = PropertyUtil.getProperties(controllerPropertiesPath.toFile());
				if (oldProperties.equals(properties)) {
					logger.info("A properties file with the same contents already exists for controller " + 
				                controllerName + 
				                ". No action is taken");
					return true;
				} else {
					this.backupController(controllerName);
				}
			} catch (Exception e) {
				logger.info("No existing Properties");
				// continue
			}
    	}
	  	
		try {
	    	File controllerPropertiesFile = controllerPropertiesPath.toFile();
	    	FileWriter writer = new FileWriter(controllerPropertiesFile);
	    	properties.store(writer, "Machine created Policy Controller Configuration");
		} catch (Exception e) {
			logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
	                    controllerName, "SystemPersistenceProperties");
			return false;
		}
		
		return true;
	}
	
	/**
	 * deletes properties-based controller configuration
	 * @param controllerName the controller name
	 * @return true if the properties has been deleted from disk, false otherwise
	 */
	@Override
	public boolean deleteController(String controllerName) {
		
	   	Path controllerPropertiesPath = 
	   			Paths.get(CONFIG_DIR_NAME, 
	   					  controllerName + PROPERTIES_FILE_CONTROLLER_SUFFIX);

	   	if (Files.exists(controllerPropertiesPath)) {
			try {
				Path controllerPropertiesBakPath = 
						Paths.get(CONFIG_DIR_NAME, controllerName + 
                                  PROPERTIES_FILE_CONTROLLER_SUFFIX + ".bak");
				Files.move(controllerPropertiesPath, 
						   controllerPropertiesBakPath, 
						   StandardCopyOption.REPLACE_EXISTING);
			} catch (Exception e) {
				logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
						    controllerName, "SystemPersistenceProperties");
				return false;
			}
		} 
	   	
	   	return true;
	}

	@Override
	public Properties getControllerProperties(String controllerName) throws IllegalArgumentException {
	   	return this.getProperties(controllerName + CONTROLLER_SUFFIX_IDENTIFIER);
	}
	
	@Override
	public Properties getProperties(String name) throws IllegalArgumentException {
	   	Path propertiesPath = 
	   			Paths.get(CONFIG_DIR_NAME, name + ".properties");
	   	
	   	if (!Files.exists(propertiesPath)) {
			throw new IllegalArgumentException("properties for " + name + " are not persisted.");
	   	}

		try {
			return PropertyUtil.getProperties(propertiesPath.toFile());
		} catch (Exception e) {
			logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
					    name, "SystemPersistenceProperties can't retrieved properties for " + 
			            propertiesPath);
			e.printStackTrace();
			throw new IllegalArgumentException("can't read properties for " + 
			                                   name + " @ " + 
					                           propertiesPath);
		}
	}
}