X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDatastoreSnapshotRestore.java;h=995401c82c681d9e184c22ee3b45277963da0ed0;hb=55a9b9f42a14c56060f74b38f84d444c0fbfecc4;hp=7ef05e273fb3ab54099c74e86570c785821e1be8;hpb=fd6fce5918121f6c802acdbe67ba9ac006049b94;p=controller.git 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 7ef05e273f..995401c82c 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,7 +7,8 @@ */ package org.opendaylight.controller.cluster.datastore; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -16,8 +17,8 @@ import java.io.ObjectInputStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList; +import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +28,7 @@ import org.slf4j.LoggerFactory; * * @author Thomas Pantelis */ -public class DatastoreSnapshotRestore { +public final class DatastoreSnapshotRestore { private static final Logger LOG = LoggerFactory.getLogger(DatastoreSnapshotRestore.class); private static AtomicReference instance = new AtomicReference<>(); @@ -35,38 +36,32 @@ public class DatastoreSnapshotRestore { private final String restoreDirectoryPath; private final Map datastoreSnapshots = new ConcurrentHashMap<>(); - public static void createInstance(String restoreDirectoryPath) { + public static DatastoreSnapshotRestore instance(final String restoreDirectoryPath) { instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath)); + return instance.get(); } - public static void removeInstance() { - instance.set(null); - } - - public static DatastoreSnapshotRestore instance() { - DatastoreSnapshotRestore localInstance = instance.get(); - return Preconditions.checkNotNull(localInstance, "DatastoreSnapshotRestore instance was not created"); - } - - private DatastoreSnapshotRestore(String restoreDirectoryPath) { - this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath); + private DatastoreSnapshotRestore(final String restoreDirectoryPath) { + this.restoreDirectoryPath = requireNonNull(restoreDirectoryPath); } - // sychronize this method so that, in case of concurrent access to getAndRemove(), + // 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) { + 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); + if (files.length > 1) { + LOG.error( + "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted", + files.length, restoreDirectoryFile); return; } @@ -74,29 +69,30 @@ public class DatastoreSnapshotRestore { LOG.info("Clustered datastore will be restored from file {}", restoreFile); - try(FileInputStream fis = new FileInputStream(restoreFile)) { + try (FileInputStream fis = new FileInputStream(restoreFile)) { DatastoreSnapshotList snapshots = deserialize(fis); LOG.debug("Deserialized {} snapshots", snapshots.size()); - for(DatastoreSnapshot snapshot: snapshots) { + for (DatastoreSnapshot snapshot: snapshots) { datastoreSnapshots.put(snapshot.getType(), snapshot); } - } catch (Exception e) { + } catch (ClassNotFoundException | IOException e) { LOG.error("Error reading clustered datastore restore file {}", restoreFile, e); } finally { - if(!restoreFile.delete()) { + if (!restoreFile.delete()) { LOG.error("Could not delete clustered datastore restore file {}", restoreFile); } } } - private DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException { - try(ObjectInputStream ois = new ObjectInputStream(inputStream)) { + private static DatastoreSnapshotList deserialize(final InputStream inputStream) + throws IOException, ClassNotFoundException { + try (ObjectInputStream ois = new ObjectInputStream(inputStream)) { return (DatastoreSnapshotList) ois.readObject(); } } - public DatastoreSnapshot getAndRemove(String datastoreType) { + public DatastoreSnapshot getAndRemove(final String datastoreType) { initialize(); return datastoreSnapshots.remove(datastoreType); }