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

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