BUG 2138 - Do not fail on module-based default shard
[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.AtomicReference;
19 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
20 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
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.
27  *
28  * @author Thomas Pantelis
29  */
30 public class DatastoreSnapshotRestore {
31     private static final Logger LOG = LoggerFactory.getLogger(DatastoreSnapshotRestore.class);
32
33     private static AtomicReference<DatastoreSnapshotRestore> instance = new AtomicReference<>();
34
35     private final String restoreDirectoryPath;
36     private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
37
38     public static DatastoreSnapshotRestore instance(String restoreDirectoryPath) {
39         instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
40         return instance.get();
41     }
42
43     private DatastoreSnapshotRestore(String restoreDirectoryPath) {
44         this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
45     }
46
47     // synchronize this method so that, in case of concurrent access to getAndRemove(),
48     // no one ends up with partially initialized data
49     @SuppressWarnings("checkstyle:IllegalCatch")
50     private synchronized void initialize() {
51
52         File restoreDirectoryFile = new File(restoreDirectoryPath);
53
54         String[] files = restoreDirectoryFile.list();
55         if (files == null || files.length == 0) {
56             LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
57             return;
58         }
59
60         if (files.length > 1) {
61             LOG.error(
62                 "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
63                 files.length, restoreDirectoryFile);
64             return;
65         }
66
67         File restoreFile = new File(restoreDirectoryFile, files[0]);
68
69         LOG.info("Clustered datastore will be restored from file {}", restoreFile);
70
71         try (FileInputStream fis = new FileInputStream(restoreFile)) {
72             DatastoreSnapshotList snapshots = deserialize(fis);
73             LOG.debug("Deserialized {} snapshots", snapshots.size());
74
75             for (DatastoreSnapshot snapshot: snapshots) {
76                 datastoreSnapshots.put(snapshot.getType(), snapshot);
77             }
78         } catch (ClassNotFoundException | IOException e) {
79             LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
80         } finally {
81             if (!restoreFile.delete()) {
82                 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
83             }
84         }
85     }
86
87     private static DatastoreSnapshotList deserialize(InputStream inputStream)
88             throws IOException, ClassNotFoundException {
89         try (ObjectInputStream ois = new ObjectInputStream(inputStream)) {
90             return (DatastoreSnapshotList) ois.readObject();
91         }
92     }
93
94     public DatastoreSnapshot getAndRemove(String datastoreType) {
95         initialize();
96         return datastoreSnapshots.remove(datastoreType);
97     }
98 }