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