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 void createInstance(String restoreDirectoryPath) {
39 instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
42 public static void removeInstance() {
46 public static DatastoreSnapshotRestore instance() {
47 DatastoreSnapshotRestore localInstance = instance.get();
48 return Preconditions.checkNotNull(localInstance, "DatastoreSnapshotRestore instance was not created");
51 private DatastoreSnapshotRestore(String restoreDirectoryPath) {
52 this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
55 // sychronize this method so that, in case of concurrent access to getAndRemove(),
56 // no one ends up with partially initialized data
57 private synchronized void initialize() {
59 File restoreDirectoryFile = new File(restoreDirectoryPath);
61 String[] files = restoreDirectoryFile.list();
62 if(files == null || files.length == 0) {
63 LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
67 if(files.length > 1) {
68 LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
69 files.length, restoreDirectoryFile);
73 File restoreFile = new File(restoreDirectoryFile, files[0]);
75 LOG.info("Clustered datastore will be restored from file {}", restoreFile);
77 try(FileInputStream fis = new FileInputStream(restoreFile)) {
78 DatastoreSnapshotList snapshots = deserialize(fis);
79 LOG.debug("Deserialized {} snapshots", snapshots.size());
81 for(DatastoreSnapshot snapshot: snapshots) {
82 datastoreSnapshots.put(snapshot.getType(), snapshot);
84 } catch (Exception e) {
85 LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
87 if(!restoreFile.delete()) {
88 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
93 private static DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
94 try(ObjectInputStream ois = new ObjectInputStream(inputStream)) {
95 return (DatastoreSnapshotList) ois.readObject();
99 public DatastoreSnapshot getAndRemove(String datastoreType) {
101 return datastoreSnapshots.remove(datastoreType);