Fix incorrect remove call in ShardManager
[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.messages.DatastoreSnapshot;
20 import org.opendaylight.controller.cluster.datastore.messages.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     private synchronized void initialize() {
50
51         File restoreDirectoryFile = new File(restoreDirectoryPath);
52
53         String[] files = restoreDirectoryFile.list();
54         if(files == null || files.length == 0) {
55             LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
56             return;
57         }
58
59         if(files.length > 1) {
60             LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
61                     files.length, restoreDirectoryFile);
62             return;
63         }
64
65         File restoreFile = new File(restoreDirectoryFile, files[0]);
66
67         LOG.info("Clustered datastore will be restored from file {}", restoreFile);
68
69         try(FileInputStream fis = new FileInputStream(restoreFile)) {
70             DatastoreSnapshotList snapshots = deserialize(fis);
71             LOG.debug("Deserialized {} snapshots", snapshots.size());
72
73             for(DatastoreSnapshot snapshot: snapshots) {
74                 datastoreSnapshots.put(snapshot.getType(), snapshot);
75             }
76         } catch (Exception e) {
77             LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
78         } finally {
79             if(!restoreFile.delete()) {
80                 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
81             }
82         }
83     }
84
85     private static DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
86         try(ObjectInputStream ois = new ObjectInputStream(inputStream)) {
87             return (DatastoreSnapshotList) ois.readObject();
88         }
89     }
90
91     public DatastoreSnapshot getAndRemove(String datastoreType) {
92         initialize();
93         return datastoreSnapshots.remove(datastoreType);
94     }
95 }