X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-persister-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fpersist%2Fimpl%2FPersisterAggregator.java;h=0c51166fe4ddedac16302cdc2b622d57d47b7c61;hp=e109ebec887b8e3c2a1579e5e2292fc970b25f1e;hb=8d9cdd0223be6d550d6f44dcb42fe7366bce3cf9;hpb=56a881b184f02588b88ebfbc541dcffe33fa0927 diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java index e109ebec88..0c51166fe4 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java @@ -9,20 +9,19 @@ package org.opendaylight.controller.netconf.persist.impl; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Optional; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import org.opendaylight.controller.config.persist.api.Persister; +import org.opendaylight.controller.config.persist.api.PropertiesProvider; import org.opendaylight.controller.config.persist.api.StorageAdapter; import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator; -import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.ListIterator; - /** * {@link Persister} implementation that delegates persisting functionality to * underlying {@link Persister} storages. Each storage has unique id, class, readonly value. @@ -34,21 +33,21 @@ import java.util.ListIterator; * Example configuration:
  netconf.config.persister.active=2,3
  # read startup configuration
- netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.config.persist.storage.directory.DirectoryStorageAdapter
+ netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.config.persist.storage.directory.xml.XmlDirectoryStorageAdapter
  netconf.config.persister.1.properties.fileStorage=configuration/initial/
 
- netconf.config.persister.2.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter
+ netconf.config.persister.2.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter
  netconf.config.persister.2.readonly=true
- netconf.config.persister.2.properties.fileStorage=configuration/current/controller.config.1.txt
+ netconf.config.persister.2.properties.fileStorage=configuration/current/controller.config.1.xml
 
- netconf.config.persister.3.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter
- netconf.config.persister.3.properties.fileStorage=configuration/current/controller.config.2.txt
+ netconf.config.persister.3.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter
+ netconf.config.persister.3.properties.fileStorage=configuration/current/controller.config.2.xml
  netconf.config.persister.3.properties.numberOfBackups=3
 
  
* During server startup {@link ConfigPersisterNotificationHandler} requests last snapshot from underlying storages. * Each storage can respond by giving snapshot or absent response. - * The {@link #loadLastConfig()} will search for first non-absent response from storages ordered backwards as user + * The {@link #loadLastConfigs()} will search for first non-absent response from storages ordered backwards as user * specified (first '3', then '2'). * * When a commit notification is received, '2' will be omitted because readonly flag is set to true, so @@ -57,7 +56,7 @@ import java.util.ListIterator; * */ public final class PersisterAggregator implements Persister { - private static final Logger logger = LoggerFactory.getLogger(PersisterAggregator.class); + private static final Logger LOG = LoggerFactory.getLogger(PersisterAggregator.class); public static class PersisterWithConfiguration { @@ -88,7 +87,7 @@ public final class PersisterAggregator implements Persister { } } - private static PersisterWithConfiguration loadConfiguration(final String index, final PropertiesProviderBaseImpl propertiesProvider) { + private static PersisterWithConfiguration loadConfiguration(final String index, final PropertiesProvider propertiesProvider) { String classKey = index + "." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX; String storageAdapterClass = propertiesProvider.getProperty(classKey); @@ -102,7 +101,7 @@ public final class PersisterAggregator implements Persister { try { Class clazz = Class.forName(storageAdapterClass); boolean implementsCorrectIfc = StorageAdapter.class.isAssignableFrom(clazz); - if (implementsCorrectIfc == false) { + if (!implementsCorrectIfc) { throw new IllegalArgumentException("Storage adapter " + clazz + " does not implement " + StorageAdapter.class); } storageAdapter = StorageAdapter.class.cast(clazz.newInstance()); @@ -131,16 +130,16 @@ public final class PersisterAggregator implements Persister { } - public static PersisterAggregator createFromProperties(PropertiesProviderBaseImpl propertiesProvider) { + public static PersisterAggregator createFromProperties(PropertiesProvider propertiesProvider) { List persisterWithConfigurations = new ArrayList<>(); String prefixes = propertiesProvider.getProperty("active"); - if (prefixes.isEmpty() == false) { + if (prefixes!=null && !prefixes.isEmpty()) { String [] keys = prefixes.split(","); for (String index: keys) { persisterWithConfigurations.add(PersisterAggregator.loadConfiguration(index, propertiesProvider)); } } - logger.debug("Initialized persister with following adapters {}", persisterWithConfigurations); + LOG.debug("Initialized persister with following adapters {}", persisterWithConfigurations); return new PersisterAggregator(persisterWithConfigurations); } @@ -148,25 +147,35 @@ public final class PersisterAggregator implements Persister { public void persistConfig(ConfigSnapshotHolder holder) throws IOException { for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){ if (!persisterWithConfiguration.readOnly){ - logger.debug("Calling {}.persistConfig",persisterWithConfiguration.storage); - persisterWithConfiguration.storage.persistConfig(holder); + LOG.debug("Calling {}.persistConfig", persisterWithConfiguration.getStorage()); + persisterWithConfiguration.getStorage().persistConfig(holder); } } } + /** + * @return last non-empty result from input persisters + */ @Override - public Optional loadLastConfig() throws IOException { + public List loadLastConfigs() { // iterate in reverse order ListIterator li = persisterWithConfigurations.listIterator(persisterWithConfigurations.size()); while(li.hasPrevious()) { PersisterWithConfiguration persisterWithConfiguration = li.previous(); - Optional configSnapshotHolderOptional = persisterWithConfiguration.storage.loadLastConfig(); - if (configSnapshotHolderOptional.isPresent()) { - return configSnapshotHolderOptional; + List configs = null; + try { + configs = persisterWithConfiguration.storage.loadLastConfigs(); + } catch (IOException e) { + throw new RuntimeException("Error while calling loadLastConfig on " + persisterWithConfiguration, e); + } + if (!configs.isEmpty()) { + LOG.debug("Found non empty configs using {}:{}", persisterWithConfiguration, configs); + return configs; } } // no storage had an answer - return Optional.absent(); + LOG.debug("No non-empty list of configuration snapshots found"); + return Collections.emptyList(); } @VisibleForTesting @@ -181,7 +190,7 @@ public final class PersisterAggregator implements Persister { try{ persisterWithConfiguration.storage.close(); }catch(RuntimeException e) { - logger.error("Error while closing {}", persisterWithConfiguration.storage, e); + LOG.error("Error while closing {}", persisterWithConfiguration.storage, e); if (lastException == null){ lastException = e; } else {