bed23895d3e5dbdd9681631c494855130f0d5a9a
[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 com.google.common.base.Preconditions;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.ObjectInputStream;
16 import java.util.Map;
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;
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 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     private final AtomicBoolean initialized = new AtomicBoolean();
39
40     public static void createInstance(String restoreDirectoryPath) {
41         instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
42     }
43
44     public static void removeInstance() {
45         instance.set(null);
46     }
47
48     public static DatastoreSnapshotRestore instance() {
49         DatastoreSnapshotRestore localInstance = instance.get();
50         return Preconditions.checkNotNull(localInstance, "DatastoreSnapshotRestore instance was not created");
51     }
52
53     private DatastoreSnapshotRestore(String restoreDirectoryPath) {
54         this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
55     }
56
57     private void initialize() {
58         if(!initialized.compareAndSet(false, true)) {
59             return;
60         }
61
62         File restoreDirectoryFile = new File(restoreDirectoryPath);
63
64         String[] files = restoreDirectoryFile.list();
65         if(files == null || files.length == 0) {
66             LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
67             return;
68         }
69
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);
73             return;
74         }
75
76         File restoreFile = new File(restoreDirectoryFile, files[0]);
77
78         LOG.info("Clustered datastore will be restored from file {}", restoreFile);
79
80         try(FileInputStream fis = new FileInputStream(restoreFile)) {
81             DatastoreSnapshotList snapshots = deserialize(fis);
82             LOG.debug("Deserialized {} snapshots", snapshots.size());
83
84             for(DatastoreSnapshot snapshot: snapshots) {
85                 datastoreSnapshots.put(snapshot.getType(), snapshot);
86             }
87         } catch (Exception e) {
88             LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
89         } finally {
90             if(!restoreFile.delete()) {
91                 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
92             }
93         }
94     }
95
96     private DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
97         try(ObjectInputStream ois = new ObjectInputStream(inputStream)) {
98             return (DatastoreSnapshotList) ois.readObject();
99         }
100     }
101
102     public DatastoreSnapshot getAndRemove(String datastoreType) {
103         initialize();
104         return datastoreSnapshots.remove(datastoreType);
105     }
106 }