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.AtomicBoolean;
19 import java.util.concurrent.atomic.AtomicReference;
20 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
21 import org.opendaylight.controller.cluster.datastore.messages.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 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<>();
38 private final AtomicBoolean initialized = new AtomicBoolean();
40 public static void createInstance(String restoreDirectoryPath) {
41 instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
44 public static void removeInstance() {
48 public static DatastoreSnapshotRestore instance() {
49 DatastoreSnapshotRestore localInstance = instance.get();
50 return Preconditions.checkNotNull(localInstance, "DatastoreSnapshotRestore instance was not created");
53 private DatastoreSnapshotRestore(String restoreDirectoryPath) {
54 this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
57 private void initialize() {
58 if(!initialized.compareAndSet(false, true)) {
62 File restoreDirectoryFile = new File(restoreDirectoryPath);
64 String[] files = restoreDirectoryFile.list();
65 if(files == null || files.length == 0) {
66 LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
70 if(files.length > 1) {
71 LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
72 files.length, restoreDirectoryFile);
76 File restoreFile = new File(restoreDirectoryFile, files[0]);
78 LOG.info("Clustered datastore will be restored from file {}", restoreFile);
80 try(FileInputStream fis = new FileInputStream(restoreFile)) {
81 DatastoreSnapshotList snapshots = deserialize(fis);
82 LOG.debug("Deserialized {} snapshots", snapshots.size());
84 for(DatastoreSnapshot snapshot: snapshots) {
85 datastoreSnapshots.put(snapshot.getType(), snapshot);
87 } catch (Exception e) {
88 LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
90 if(!restoreFile.delete()) {
91 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
96 private DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
97 try(ObjectInputStream ois = new ObjectInputStream(inputStream)) {
98 return (DatastoreSnapshotList) ois.readObject();
102 public DatastoreSnapshot getAndRemove(String datastoreType) {
104 return datastoreSnapshots.remove(datastoreType);