2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.persist.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13 import org.opendaylight.controller.config.persist.api.Persister;
14 import org.opendaylight.controller.config.persist.api.StorageAdapter;
15 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
16 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.ListIterator;
27 * {@link Persister} implementation that delegates persisting functionality to
28 * underlying {@link Persister} storages. Each storage has unique id, class, readonly value.
30 * Storage adapters are low level persisters that do the heavy lifting for this
31 * class. Instances of storage adapters can be injected directly via constructor
32 * or instantiated from a full name of its class provided in a properties file.
34 * Example configuration:<pre>
35 netconf.config.persister.active=2,3
36 # read startup configuration
37 netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.config.persist.storage.directory.xml.XmlDirectoryStorageAdapter
38 netconf.config.persister.1.properties.fileStorage=configuration/initial/
40 netconf.config.persister.2.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter
41 netconf.config.persister.2.readonly=true
42 netconf.config.persister.2.properties.fileStorage=configuration/current/controller.config.1.txt
44 netconf.config.persister.3.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter
45 netconf.config.persister.3.properties.fileStorage=configuration/current/controller.config.2.txt
46 netconf.config.persister.3.properties.numberOfBackups=3
49 * During server startup {@link ConfigPersisterNotificationHandler} requests last snapshot from underlying storages.
50 * Each storage can respond by giving snapshot or absent response.
51 * The {@link #loadLastConfigs()} will search for first non-absent response from storages ordered backwards as user
52 * specified (first '3', then '2').
54 * When a commit notification is received, '2' will be omitted because readonly flag is set to true, so
55 * only '3' will have a chance to persist new configuration. If readonly was false or not specified, both storage adapters
56 * would be called in order specified by 'netconf.config.persister' property.
59 public final class PersisterAggregator implements Persister {
60 private static final Logger logger = LoggerFactory.getLogger(PersisterAggregator.class);
62 public static class PersisterWithConfiguration {
64 private final Persister storage;
65 private final boolean readOnly;
67 public PersisterWithConfiguration(Persister storage, boolean readOnly) {
68 this.storage = storage;
69 this.readOnly = readOnly;
73 public Persister getStorage() {
78 public boolean isReadOnly() {
83 public String toString() {
84 return "PersisterWithConfiguration{" +
85 "storage=" + storage +
86 ", readOnly=" + readOnly +
91 private static PersisterWithConfiguration loadConfiguration(final String index, final PropertiesProviderBaseImpl propertiesProvider) {
93 String classKey = index + "." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX;
94 String storageAdapterClass = propertiesProvider.getProperty(classKey);
95 StorageAdapter storageAdapter;
96 if (storageAdapterClass == null || storageAdapterClass.equals("")) {
97 throw new IllegalStateException("No persister is defined in " +
98 propertiesProvider.getFullKeyForReporting(classKey)
99 + " property. Persister is not operational");
103 Class<?> clazz = Class.forName(storageAdapterClass);
104 boolean implementsCorrectIfc = StorageAdapter.class.isAssignableFrom(clazz);
105 if (implementsCorrectIfc == false) {
106 throw new IllegalArgumentException("Storage adapter " + clazz + " does not implement " + StorageAdapter.class);
108 storageAdapter = StorageAdapter.class.cast(clazz.newInstance());
110 boolean readOnly = false;
111 String readOnlyProperty = propertiesProvider.getProperty(index + "." + "readonly");
112 if (readOnlyProperty != null && readOnlyProperty.equals("true")) {
116 PropertiesProviderAdapterImpl innerProvider = new PropertiesProviderAdapterImpl(propertiesProvider, index);
117 Persister storage = storageAdapter.instantiate(innerProvider);
118 return new PersisterWithConfiguration(storage, readOnly);
119 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
120 throw new IllegalArgumentException("Unable to instantiate storage adapter from " + storageAdapterClass, e);
125 * Persisters ordered by 'netconf.config.persister' property.
127 private final List<PersisterWithConfiguration> persisterWithConfigurations;
129 public PersisterAggregator(List<PersisterWithConfiguration> persisterWithConfigurations) {
130 this.persisterWithConfigurations = persisterWithConfigurations;
134 public static PersisterAggregator createFromProperties(PropertiesProviderBaseImpl propertiesProvider) {
135 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
136 String prefixes = propertiesProvider.getProperty("active");
137 if (prefixes.isEmpty() == false) {
138 String [] keys = prefixes.split(",");
139 for (String index: keys) {
140 persisterWithConfigurations.add(PersisterAggregator.loadConfiguration(index, propertiesProvider));
143 logger.debug("Initialized persister with following adapters {}", persisterWithConfigurations);
144 return new PersisterAggregator(persisterWithConfigurations);
148 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
149 for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
150 if (!persisterWithConfiguration.readOnly){
151 logger.debug("Calling {}.persistConfig",persisterWithConfiguration.storage);
152 persisterWithConfiguration.storage.persistConfig(holder);
158 * @return last non-empty result from input persisters
161 public List<ConfigSnapshotHolder> loadLastConfigs() {
162 // iterate in reverse order
163 ListIterator<PersisterWithConfiguration> li = persisterWithConfigurations.listIterator(persisterWithConfigurations.size());
164 while(li.hasPrevious()) {
165 PersisterWithConfiguration persisterWithConfiguration = li.previous();
166 List<ConfigSnapshotHolder> configs = null;
168 configs = persisterWithConfiguration.storage.loadLastConfigs();
169 } catch (IOException e) {
170 throw new RuntimeException("Error while calling loadLastConfig on " + persisterWithConfiguration, e);
172 if (configs.isEmpty() == false) {
173 logger.debug("Found non empty configs using {}:{}", persisterWithConfiguration, configs);
177 // no storage had an answer
178 logger.debug("No non-empty list of configuration snapshots found");
179 return Collections.emptyList();
183 List<PersisterWithConfiguration> getPersisterWithConfigurations() {
184 return persisterWithConfigurations;
188 public void close() {
189 RuntimeException lastException = null;
190 for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
192 persisterWithConfiguration.storage.close();
193 }catch(RuntimeException e) {
194 logger.error("Error while closing {}", persisterWithConfiguration.storage, e);
195 if (lastException == null){
198 lastException.addSuppressed(e);
202 if (lastException != null){
208 public String toString() {
209 return "PersisterAggregator{" +
210 "persisterWithConfigurations=" + persisterWithConfigurations +