Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / DatastoreSnapshotList.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.messages;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
16 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
17 import org.opendaylight.controller.cluster.raft.Snapshot;
18 import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
19
20 /**
21  * Stores a list of DatastoreSnapshot instances.
22  *
23  * @deprecated Use {@link org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList} instead.
24  */
25 @Deprecated
26 public class DatastoreSnapshotList extends ArrayList<DatastoreSnapshot> {
27     private static final long serialVersionUID = 1L;
28
29     public DatastoreSnapshotList() {
30     }
31
32     public DatastoreSnapshotList(List<DatastoreSnapshot> snapshots) {
33         super(snapshots);
34     }
35
36     private Object readResolve() throws IOException, ClassNotFoundException {
37         List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot> snapshots =
38                 new ArrayList<>(size());
39         for (DatastoreSnapshot legacy: this) {
40             snapshots.add(new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot(
41                     legacy.getType(), legacy.getShardManagerSnapshot(), fromLegacy(legacy.getShardSnapshots())));
42         }
43
44         return new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList(snapshots);
45     }
46
47     private static List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot>
48             fromLegacy(List<DatastoreSnapshot.ShardSnapshot> from) throws IOException, ClassNotFoundException {
49         List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot> snapshots =
50                 new ArrayList<>(from.size());
51         for (DatastoreSnapshot.ShardSnapshot legacy: from) {
52             snapshots.add(new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot(
53                     legacy.getName(), deserialize(legacy.getSnapshot())));
54         }
55
56         return snapshots;
57     }
58
59     private static org.opendaylight.controller.cluster.raft.persisted.Snapshot deserialize(byte[] bytes)
60             throws IOException, ClassNotFoundException {
61         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
62             Snapshot legacy = (Snapshot) ois.readObject();
63
64             org.opendaylight.controller.cluster.raft.persisted.Snapshot.State state = EmptyState.INSTANCE;
65             if (legacy.getState().length > 0) {
66                 state = new ShardSnapshotState(ShardDataTreeSnapshot.deserializePreCarbon(legacy.getState()));
67             }
68
69             return org.opendaylight.controller.cluster.raft.persisted.Snapshot.create(
70                     state, legacy.getUnAppliedEntries(), legacy.getLastIndex(),
71                     legacy.getLastTerm(), legacy.getLastAppliedIndex(), legacy.getLastAppliedTerm(),
72                     legacy.getElectionTerm(), legacy.getElectionVotedFor(), legacy.getServerConfiguration());
73         }
74     }
75 }

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