Convert DatastoreSnapshotRestore to OSGi DS
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DefaultDatastoreSnapshotRestore.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.ObjectInputStream;
18 import java.util.Map;
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;
27
28 /**
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.
31  *
32  * @author Thomas Pantelis
33  */
34 @Beta
35 @Component(immediate = true)
36 public final class DefaultDatastoreSnapshotRestore implements DatastoreSnapshotRestore {
37     private static final Logger LOG = LoggerFactory.getLogger(DefaultDatastoreSnapshotRestore.class);
38
39     private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
40     private final String restoreDirectoryPath;
41
42     public DefaultDatastoreSnapshotRestore() {
43         this("./clustered-datastore-restore");
44     }
45
46     public DefaultDatastoreSnapshotRestore(final String restoreDirectoryPath) {
47         this.restoreDirectoryPath = requireNonNull(restoreDirectoryPath);
48     }
49
50     @Override
51     public Optional<DatastoreSnapshot> getAndRemove(final String datastoreType) {
52         return Optional.ofNullable(datastoreSnapshots.remove(datastoreType));
53     }
54
55     @Activate
56     @SuppressWarnings("checkstyle:IllegalCatch")
57     void activate() {
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);
62             return;
63         }
64
65         if (files.length > 1) {
66             LOG.error(
67                 "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
68                 files.length, restoreDirectoryFile);
69             return;
70         }
71
72         final File restoreFile = new File(restoreDirectoryFile, files[0]);
73         LOG.info("Clustered datastore will be restored from file {}", restoreFile);
74
75         try (FileInputStream fis = new FileInputStream(restoreFile)) {
76             DatastoreSnapshotList snapshots = deserialize(fis);
77             LOG.debug("Deserialized {} snapshots", snapshots.size());
78
79             for (DatastoreSnapshot snapshot: snapshots) {
80                 datastoreSnapshots.put(snapshot.getType(), snapshot);
81             }
82         } catch (ClassNotFoundException | IOException e) {
83             LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
84         } finally {
85             if (!restoreFile.delete()) {
86                 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
87             }
88         }
89     }
90
91     private static DatastoreSnapshotList deserialize(final InputStream inputStream)
92             throws IOException, ClassNotFoundException {
93         try (ObjectInputStream ois = new ObjectInputStream(inputStream)) {
94             return (DatastoreSnapshotList) ois.readObject();
95         }
96     }
97 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.