2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
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;
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;
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;
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);
41 public Collection<FrontendHistoryMetadata> getCurrentHistories() {
42 return currentHistories;
45 public RangeSet<UnsignedLong> getPurgedHistories() {
46 return purgedHistories;
50 public ClientIdentifier getIdentifier() {
55 public void writeTo(final DataOutput out) throws IOException {
56 identifier.writeTo(out);
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());
64 out.writeInt(currentHistories.size());
65 for (final FrontendHistoryMetadata h : currentHistories) {
70 public static FrontendClientMetadata readFrom(final DataInput in) throws IOException {
71 final ClientIdentifier id = ClientIdentifier.readFrom(in);
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));
80 b.add(Range.closed(lower, upper));
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));
89 return new FrontendClientMetadata(id, b.build(), currentHistories);
93 public String toString() {
94 return MoreObjects.toStringHelper(FrontendClientMetadata.class).add("identifer", identifier)
95 .add("current", currentHistories).add("purged", purgedHistories).toString();