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 static java.util.Objects.requireNonNull;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.ObjectInputStream;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.atomic.AtomicReference;
20 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
21 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * This class looks for a previously saved data store backup file in a directory and, if found, de-serializes
27 * the DatastoreSnapshot instances. This class has a static singleton that is created on bundle activation.
29 * @author Thomas Pantelis
31 public final class DatastoreSnapshotRestore {
32 private static final Logger LOG = LoggerFactory.getLogger(DatastoreSnapshotRestore.class);
34 private static AtomicReference<DatastoreSnapshotRestore> instance = new AtomicReference<>();
36 private final String restoreDirectoryPath;
37 private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
39 public static DatastoreSnapshotRestore instance(final String restoreDirectoryPath) {
40 instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
41 return instance.get();
44 private DatastoreSnapshotRestore(final String restoreDirectoryPath) {
45 this.restoreDirectoryPath = requireNonNull(restoreDirectoryPath);
48 // synchronize this method so that, in case of concurrent access to getAndRemove(),
49 // no one ends up with partially initialized data
50 @SuppressWarnings("checkstyle:IllegalCatch")
51 private synchronized void initialize() {
53 File restoreDirectoryFile = new File(restoreDirectoryPath);
55 String[] files = restoreDirectoryFile.list();
56 if (files == null || files.length == 0) {
57 LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
61 if (files.length > 1) {
63 "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
64 files.length, restoreDirectoryFile);
68 File restoreFile = new File(restoreDirectoryFile, files[0]);
70 LOG.info("Clustered datastore will be restored from file {}", restoreFile);
72 try (FileInputStream fis = new FileInputStream(restoreFile)) {
73 DatastoreSnapshotList snapshots = deserialize(fis);
74 LOG.debug("Deserialized {} snapshots", snapshots.size());
76 for (DatastoreSnapshot snapshot: snapshots) {
77 datastoreSnapshots.put(snapshot.getType(), snapshot);
79 } catch (ClassNotFoundException | IOException e) {
80 LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
82 if (!restoreFile.delete()) {
83 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
88 private static DatastoreSnapshotList deserialize(final InputStream inputStream)
89 throws IOException, ClassNotFoundException {
90 try (ObjectInputStream ois = new ObjectInputStream(inputStream)) {
91 return (DatastoreSnapshotList) ois.readObject();
95 public DatastoreSnapshot getAndRemove(final String datastoreType) {
97 return datastoreSnapshots.remove(datastoreType);