Migrate DatastoreContextIntrospectorFactory to OSGi DS
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreSnapshotRestore.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 java.io.File;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.ObjectInputStream;
17 import java.util.Map;
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;
24
25 /**
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.
28  *
29  * @author Thomas Pantelis
30  */
31 public final class DatastoreSnapshotRestore {
32     private static final Logger LOG = LoggerFactory.getLogger(DatastoreSnapshotRestore.class);
33
34     private static AtomicReference<DatastoreSnapshotRestore> instance = new AtomicReference<>();
35
36     private final String restoreDirectoryPath;
37     private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
38
39     public static DatastoreSnapshotRestore instance(final String restoreDirectoryPath) {
40         instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
41         return instance.get();
42     }
43
44     private DatastoreSnapshotRestore(final String restoreDirectoryPath) {
45         this.restoreDirectoryPath = requireNonNull(restoreDirectoryPath);
46     }
47
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() {
52
53         File restoreDirectoryFile = new File(restoreDirectoryPath);
54
55         String[] files = restoreDirectoryFile.list();
56         if (files == null || files.length == 0) {
57             LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
58             return;
59         }
60
61         if (files.length > 1) {
62             LOG.error(
63                 "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
64                 files.length, restoreDirectoryFile);
65             return;
66         }
67
68         File restoreFile = new File(restoreDirectoryFile, files[0]);
69
70         LOG.info("Clustered datastore will be restored from file {}", restoreFile);
71
72         try (FileInputStream fis = new FileInputStream(restoreFile)) {
73             DatastoreSnapshotList snapshots = deserialize(fis);
74             LOG.debug("Deserialized {} snapshots", snapshots.size());
75
76             for (DatastoreSnapshot snapshot: snapshots) {
77                 datastoreSnapshots.put(snapshot.getType(), snapshot);
78             }
79         } catch (ClassNotFoundException | IOException e) {
80             LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
81         } finally {
82             if (!restoreFile.delete()) {
83                 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
84             }
85         }
86     }
87
88     private static DatastoreSnapshotList deserialize(final InputStream inputStream)
89             throws IOException, ClassNotFoundException {
90         try (ObjectInputStream ois = new ObjectInputStream(inputStream)) {
91             return (DatastoreSnapshotList) ois.readObject();
92         }
93     }
94
95     public DatastoreSnapshot getAndRemove(final String datastoreType) {
96         initialize();
97         return datastoreSnapshots.remove(datastoreType);
98     }
99 }

©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.