Bug 7521: Convert byte[] to ShardManagerSnapshot in DatastoreSnapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / FrontendClientMetadata.java
1 /*
2  * Copyright (c) 2016 Cisco 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.persisted;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableRangeSet;
14 import com.google.common.collect.ImmutableRangeSet.Builder;
15 import com.google.common.collect.Range;
16 import com.google.common.collect.RangeSet;
17 import com.google.common.primitives.UnsignedLong;
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Set;
24 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
25 import org.opendaylight.yangtools.concepts.Identifiable;
26 import org.opendaylight.yangtools.concepts.WritableObject;
27 import org.opendaylight.yangtools.concepts.WritableObjects;
28
29 public final class FrontendClientMetadata implements Identifiable<ClientIdentifier>, WritableObject {
30     private final Collection<FrontendHistoryMetadata> currentHistories;
31     private final RangeSet<UnsignedLong> purgedHistories;
32     private final ClientIdentifier identifier;
33
34     public FrontendClientMetadata(final ClientIdentifier identifier, final RangeSet<UnsignedLong> purgedHistories,
35             final Collection<FrontendHistoryMetadata> currentHistories) {
36         this.identifier = Preconditions.checkNotNull(identifier);
37         this.purgedHistories = ImmutableRangeSet.copyOf(purgedHistories);
38         this.currentHistories = ImmutableList.copyOf(currentHistories);
39     }
40
41     public Collection<FrontendHistoryMetadata> getCurrentHistories() {
42         return currentHistories;
43     }
44
45     public RangeSet<UnsignedLong> getPurgedHistories() {
46         return purgedHistories;
47     }
48
49     @Override
50     public ClientIdentifier getIdentifier() {
51         return identifier;
52     }
53
54     @Override
55     public void writeTo(final DataOutput out) throws IOException {
56         identifier.writeTo(out);
57
58         final Set<Range<UnsignedLong>> ranges = purgedHistories.asRanges();
59         out.writeInt(ranges.size());
60         for (final Range<UnsignedLong> r : ranges) {
61             WritableObjects.writeLongs(out, r.lowerEndpoint().longValue(), r.upperEndpoint().longValue());
62         }
63
64         out.writeInt(currentHistories.size());
65         for (final FrontendHistoryMetadata h : currentHistories) {
66             h.writeTo(out);
67         }
68     }
69
70     public static FrontendClientMetadata readFrom(final DataInput in) throws IOException, ClassNotFoundException {
71         final ClientIdentifier id = ClientIdentifier.readFrom(in);
72
73         final int purgedSize = in.readInt();
74         final Builder<UnsignedLong> b = ImmutableRangeSet.builder();
75         for (int i = 0; i < purgedSize; ++i) {
76             final byte header = WritableObjects.readLongHeader(in);
77             final UnsignedLong lower = UnsignedLong.fromLongBits(WritableObjects.readFirstLong(in, header));
78             final UnsignedLong upper = UnsignedLong.fromLongBits(WritableObjects.readSecondLong(in, header));
79
80             b.add(Range.closed(lower, upper));
81         }
82
83         final int currentSize = in.readInt();
84         final Collection<FrontendHistoryMetadata> currentHistories = new ArrayList<>(currentSize);
85         for (int i = 0; i < currentSize; ++i) {
86             currentHistories.add(FrontendHistoryMetadata.readFrom(in));
87         }
88
89         return new FrontendClientMetadata(id, b.build(), currentHistories);
90     }
91
92     @Override
93     public String toString() {
94         return MoreObjects.toStringHelper(FrontendClientMetadata.class).add("Identifier", identifier)
95                 .add("CurrentHistory", currentHistories).add("Range", purgedHistories).toString();
96     }
97 }