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.config.persist.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.ListIterator;
17 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
18 import org.opendaylight.controller.config.persist.api.Persister;
19 import org.opendaylight.controller.config.persist.api.PropertiesProvider;
20 import org.opendaylight.controller.config.persist.api.StorageAdapter;
21 import org.opendaylight.controller.config.persist.impl.osgi.ConfigPersisterActivator;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * {@link Persister} implementation that delegates persisting functionality to
27 * underlying {@link Persister} storages. Each storage has unique id, class, readonly value.
29 * Storage adapters are low level persisters that do the heavy lifting for this
30 * class. Instances of storage adapters can be injected directly via constructor
31 * or instantiated from a full name of its class provided in a properties file.
33 * Example configuration:<pre>
34 netconf.config.persister.active=2,3
35 # read startup configuration
36 netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.config.persist.storage.directory.xml.XmlDirectoryStorageAdapter
37 netconf.config.persister.1.properties.fileStorage=configuration/initial/
39 netconf.config.persister.2.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter
40 netconf.config.persister.2.readonly=true
41 netconf.config.persister.2.properties.fileStorage=configuration/current/controller.config.1.xml
43 netconf.config.persister.3.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter
44 netconf.config.persister.3.properties.fileStorage=configuration/current/controller.config.2.xml
45 netconf.config.persister.3.properties.numberOfBackups=3
48 * During server startup {@link ConfigPersisterNotificationHandler} requests last snapshot from underlying storages.
49 * Each storage can respond by giving snapshot or absent response.
50 * The {@link #loadLastConfigs()} will search for first non-absent response from storages ordered backwards as user
51 * specified (first '3', then '2').
53 * When a commit notification is received, '2' will be omitted because readonly flag is set to true, so
54 * only '3' will have a chance to persist new configuration. If readonly was false or not specified, both storage adapters
55 * would be called in order specified by 'netconf.config.persister' property.
58 public final class PersisterAggregator implements Persister {
59 private static final Logger LOG = LoggerFactory.getLogger(PersisterAggregator.class);
61 public static class PersisterWithConfiguration {
63 private final Persister storage;
64 private final boolean readOnly;
66 public PersisterWithConfiguration(final Persister storage, final boolean readOnly) {
67 this.storage = storage;
68 this.readOnly = readOnly;
72 public Persister getStorage() {
77 public boolean isReadOnly() {
82 public String toString() {
83 return "PersisterWithConfiguration{" +
84 "storage=" + storage +
85 ", readOnly=" + readOnly +
91 * Persisters ordered by 'netconf.config.persister' property.
93 private final List<PersisterWithConfiguration> persisterWithConfigurations;
95 public PersisterAggregator(final List<PersisterWithConfiguration> persisterWithConfigurations) {
96 this.persisterWithConfigurations = persisterWithConfigurations;
99 private static PersisterWithConfiguration loadConfiguration(final String index, final PropertiesProvider propertiesProvider) {
101 String classKey = index + "." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX;
102 String storageAdapterClass = propertiesProvider.getProperty(classKey);
103 StorageAdapter storageAdapter;
104 if (storageAdapterClass == null || storageAdapterClass.isEmpty()) {
105 throw new IllegalStateException("No persister is defined in " +
106 propertiesProvider.getFullKeyForReporting(classKey)
107 + " property. Persister is not operational");
111 Class<?> clazz = Class.forName(storageAdapterClass);
112 boolean implementsCorrectIfc = StorageAdapter.class.isAssignableFrom(clazz);
113 if (!implementsCorrectIfc) {
114 throw new IllegalArgumentException("Storage adapter " + clazz + " does not implement " + StorageAdapter.class);
116 storageAdapter = StorageAdapter.class.cast(clazz.newInstance());
118 String readOnlyProperty = propertiesProvider.getProperty(index + "." + "readonly");
119 boolean readOnly = Boolean.parseBoolean(readOnlyProperty);
121 PropertiesProviderAdapterImpl innerProvider = new PropertiesProviderAdapterImpl(propertiesProvider, index);
122 Persister storage = storageAdapter.instantiate(innerProvider);
123 return new PersisterWithConfiguration(storage, readOnly);
124 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
125 throw new IllegalArgumentException("Unable to instantiate storage adapter from " + storageAdapterClass, e);
129 public static PersisterAggregator createFromProperties(final PropertiesProvider propertiesProvider) {
130 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
131 String prefixes = propertiesProvider.getProperty("active");
132 if (prefixes!=null && !prefixes.isEmpty()) {
133 String [] keys = prefixes.split(",");
134 for (String index: keys) {
135 persisterWithConfigurations.add(PersisterAggregator.loadConfiguration(index, propertiesProvider));
138 LOG.debug("Initialized persister with following adapters {}", persisterWithConfigurations);
139 return new PersisterAggregator(persisterWithConfigurations);
143 public void persistConfig(final ConfigSnapshotHolder holder) throws IOException {
144 for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
145 if (!persisterWithConfiguration.readOnly){
146 LOG.debug("Calling {}.persistConfig", persisterWithConfiguration.getStorage());
147 persisterWithConfiguration.getStorage().persistConfig(holder);
153 * @return last non-empty result from input persisters
156 public List<ConfigSnapshotHolder> loadLastConfigs() {
157 // iterate in reverse order
158 ListIterator<PersisterWithConfiguration> li = persisterWithConfigurations.listIterator(persisterWithConfigurations.size());
159 while(li.hasPrevious()) {
160 PersisterWithConfiguration persisterWithConfiguration = li.previous();
161 List<ConfigSnapshotHolder> configs = null;
163 configs = persisterWithConfiguration.storage.loadLastConfigs();
164 } catch (final IOException e) {
165 throw new RuntimeException("Error while calling loadLastConfig on " + persisterWithConfiguration, e);
167 if (!configs.isEmpty()) {
168 LOG.debug("Found non empty configs using {}:{}", persisterWithConfiguration, configs);
172 // no storage had an answer
173 LOG.debug("No non-empty list of configuration snapshots found");
174 return Collections.emptyList();
178 List<PersisterWithConfiguration> getPersisterWithConfigurations() {
179 return persisterWithConfigurations;
183 public void close() {
184 RuntimeException lastException = null;
185 for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
187 persisterWithConfiguration.storage.close();
188 }catch(final RuntimeException e) {
189 LOG.error("Error while closing {}", persisterWithConfiguration.storage, e);
190 if (lastException == null){
193 lastException.addSuppressed(e);
197 if (lastException != null){
203 public String toString() {
204 return "PersisterAggregator{" +
205 "persisterWithConfigurations=" + persisterWithConfigurations +