X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDatastoreSnapshotRestore.java;h=f6068dd539a0490bc3f74ae9a21ecc4e47bcd571;hp=db9acaebcdd3e39316d0cc67abd456f68fc922ea;hb=ccca30bbb1545643c427fc59c23329c5d49f8d4b;hpb=2d60632f7cf63712e8357a3cf3fc40d83366e5e6 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java index db9acaebcd..f6068dd539 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java @@ -7,19 +7,9 @@ */ package org.opendaylight.controller.cluster.datastore; -import com.google.common.base.Preconditions; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicReference; +import com.google.common.annotations.Beta; +import java.util.Optional; import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot; -import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * This class looks for a previously saved data store backup file in a directory and, if found, de-serializes @@ -27,72 +17,8 @@ import org.slf4j.LoggerFactory; * * @author Thomas Pantelis */ -public final class DatastoreSnapshotRestore { - private static final Logger LOG = LoggerFactory.getLogger(DatastoreSnapshotRestore.class); +@Beta +public interface DatastoreSnapshotRestore { - private static AtomicReference instance = new AtomicReference<>(); - - private final String restoreDirectoryPath; - private final Map datastoreSnapshots = new ConcurrentHashMap<>(); - - public static DatastoreSnapshotRestore instance(final String restoreDirectoryPath) { - instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath)); - return instance.get(); - } - - private DatastoreSnapshotRestore(final String restoreDirectoryPath) { - this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath); - } - - // synchronize this method so that, in case of concurrent access to getAndRemove(), - // no one ends up with partially initialized data - @SuppressWarnings("checkstyle:IllegalCatch") - private synchronized void initialize() { - - File restoreDirectoryFile = new File(restoreDirectoryPath); - - String[] files = restoreDirectoryFile.list(); - if (files == null || files.length == 0) { - LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile); - return; - } - - if (files.length > 1) { - LOG.error( - "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted", - files.length, restoreDirectoryFile); - return; - } - - File restoreFile = new File(restoreDirectoryFile, files[0]); - - LOG.info("Clustered datastore will be restored from file {}", restoreFile); - - try (FileInputStream fis = new FileInputStream(restoreFile)) { - DatastoreSnapshotList snapshots = deserialize(fis); - LOG.debug("Deserialized {} snapshots", snapshots.size()); - - for (DatastoreSnapshot snapshot: snapshots) { - datastoreSnapshots.put(snapshot.getType(), snapshot); - } - } catch (ClassNotFoundException | IOException e) { - LOG.error("Error reading clustered datastore restore file {}", restoreFile, e); - } finally { - if (!restoreFile.delete()) { - LOG.error("Could not delete clustered datastore restore file {}", restoreFile); - } - } - } - - private static DatastoreSnapshotList deserialize(final InputStream inputStream) - throws IOException, ClassNotFoundException { - try (ObjectInputStream ois = new ObjectInputStream(inputStream)) { - return (DatastoreSnapshotList) ois.readObject(); - } - } - - public DatastoreSnapshot getAndRemove(final String datastoreType) { - initialize(); - return datastoreSnapshots.remove(datastoreType); - } + Optional getAndRemove(String datastoreType); }