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;
12 import com.google.common.annotations.Beta;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.ObjectInputStream;
19 import java.util.Optional;
20 import java.util.concurrent.ConcurrentHashMap;
21 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
22 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList;
23 import org.osgi.service.component.annotations.Activate;
24 import org.osgi.service.component.annotations.Component;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * This class looks for a previously saved data store backup file in a directory and, if found, de-serializes
30 * the DatastoreSnapshot instances. This class has a static singleton that is created on bundle activation.
32 * @author Thomas Pantelis
35 @Component(immediate = true)
36 public final class DefaultDatastoreSnapshotRestore implements DatastoreSnapshotRestore {
37 private static final Logger LOG = LoggerFactory.getLogger(DefaultDatastoreSnapshotRestore.class);
39 private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
40 private final String restoreDirectoryPath;
42 public DefaultDatastoreSnapshotRestore() {
43 this("./clustered-datastore-restore");
46 public DefaultDatastoreSnapshotRestore(final String restoreDirectoryPath) {
47 this.restoreDirectoryPath = requireNonNull(restoreDirectoryPath);
51 public Optional<DatastoreSnapshot> getAndRemove(final String datastoreType) {
52 return Optional.ofNullable(datastoreSnapshots.remove(datastoreType));
56 @SuppressWarnings("checkstyle:IllegalCatch")
58 final File restoreDirectoryFile = new File(restoreDirectoryPath);
59 final String[] files = restoreDirectoryFile.list();
60 if (files == null || files.length == 0) {
61 LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
65 if (files.length > 1) {
67 "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
68 files.length, restoreDirectoryFile);
72 final File restoreFile = new File(restoreDirectoryFile, files[0]);
73 LOG.info("Clustered datastore will be restored from file {}", restoreFile);
75 try (FileInputStream fis = new FileInputStream(restoreFile)) {
76 DatastoreSnapshotList snapshots = deserialize(fis);
77 LOG.debug("Deserialized {} snapshots", snapshots.size());
79 for (DatastoreSnapshot snapshot: snapshots) {
80 datastoreSnapshots.put(snapshot.getType(), snapshot);
82 } catch (ClassNotFoundException | IOException e) {
83 LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
85 if (!restoreFile.delete()) {
86 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
91 private static DatastoreSnapshotList deserialize(final InputStream inputStream)
92 throws IOException, ClassNotFoundException {
93 try (ObjectInputStream ois = new ObjectInputStream(inputStream)) {
94 return (DatastoreSnapshotList) ois.readObject();