BUG-5280: remove support for talking to pre-Boron clients
[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 void createInstance(String restoreDirectoryPath) {
39         instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
40     }
41
42     public static void removeInstance() {
43         instance.set(null);
44     }
45
46     public static DatastoreSnapshotRestore instance() {
47         DatastoreSnapshotRestore localInstance = instance.get();
48         return Preconditions.checkNotNull(localInstance, "DatastoreSnapshotRestore instance was not created");
49     }
50
51     private DatastoreSnapshotRestore(String restoreDirectoryPath) {
52         this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
53     }
54
55     // sychronize this method so that, in case of concurrent access to getAndRemove(),
56     // no one ends up with partially initialized data
57     private synchronized void initialize() {
58
59         File restoreDirectoryFile = new File(restoreDirectoryPath);
60
61         String[] files = restoreDirectoryFile.list();
62         if(files == null || files.length == 0) {
63             LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile);
64             return;
65         }
66
67         if(files.length > 1) {
68             LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted",
69                     files.length, restoreDirectoryFile);
70             return;
71         }
72
73         File restoreFile = new File(restoreDirectoryFile, files[0]);
74
75         LOG.info("Clustered datastore will be restored from file {}", restoreFile);
76
77         try(FileInputStream fis = new FileInputStream(restoreFile)) {
78             DatastoreSnapshotList snapshots = deserialize(fis);
79             LOG.debug("Deserialized {} snapshots", snapshots.size());
80
81             for(DatastoreSnapshot snapshot: snapshots) {
82                 datastoreSnapshots.put(snapshot.getType(), snapshot);
83             }
84         } catch (Exception e) {
85             LOG.error("Error reading clustered datastore restore file {}", restoreFile, e);
86         } finally {
87             if(!restoreFile.delete()) {
88                 LOG.error("Could not delete clustered datastore restore file {}", restoreFile);
89             }
90         }
91     }
92
93     private static DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
94         try(ObjectInputStream ois = new ObjectInputStream(inputStream)) {
95             return (DatastoreSnapshotList) ois.readObject();
96         }
97     }
98
99     public DatastoreSnapshot getAndRemove(String datastoreType) {
100         initialize();
101         return datastoreSnapshots.remove(datastoreType);
102     }
103 }