2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore;
10 import com.google.common.base.Preconditions;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.ObjectInputStream;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.atomic.AtomicReference;
19 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
20 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * This class looks for a previously saved data store backup file in a directory and, if found, de-serializes
26 * the DatastoreSnapshot instances. This class has a static singleton that is created on bundle activation.
28 * @author Thomas Pantelis
30 public class DatastoreSnapshotRestore {
31 private static final Logger LOG = LoggerFactory.getLogger(DatastoreSnapshotRestore.class);
33 private static AtomicReference<DatastoreSnapshotRestore> instance = new AtomicReference<>();
35 private final String restoreDirectoryPath;
36 private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
38 public static DatastoreSnapshotRestore instance(String restoreDirectoryPath) {
39 instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
40 return instance.get();
43 private DatastoreSnapshotRestore(String restoreDirectoryPath) {
44 this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
47 // synchronize this method so that, in case of concurrent access to getAndRemove(),
48 // no one ends up with partially initialized data
49 @SuppressWarnings("checkstyle:IllegalCatch")
50 private synchronized void initialize() {
52 File restoreDirectoryFile = new File(restoreDirectoryPath);
54 String[] files = restoreDirectoryFile.list();
55 if (files == null || files.length == 0) {
56 LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
60 if (files.length > 1) {
62 "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
63 files.length, restoreDirectoryFile);
67 File restoreFile = new File(restoreDirectoryFile, files[0]);
69 LOG.info("Clustered datastore will be restored from file {}", restoreFile);
71 try (FileInputStream fis = new FileInputStream(restoreFile)) {
72 DatastoreSnapshotList snapshots = deserialize(fis);
73 LOG.debug("Deserialized {} snapshots", snapshots.size());
75 for (DatastoreSnapshot snapshot: snapshots) {
76 datastoreSnapshots.put(snapshot.getType(), snapshot);
78 } catch (Exception e) {
79 LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
81 if (!restoreFile.delete()) {
82 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
87 private static DatastoreSnapshotList deserialize(InputStream inputStream)
88 throws IOException, ClassNotFoundException {
89 try (ObjectInputStream ois = new ObjectInputStream(inputStream)) {
90 return (DatastoreSnapshotList) ois.readObject();
94 public DatastoreSnapshot getAndRemove(String datastoreType) {
96 return datastoreSnapshots.remove(datastoreType);