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 static java.util.Objects.requireNonNull;
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;
25 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
26 import org.opendaylight.yangtools.concepts.Identifiable;
27 import org.opendaylight.yangtools.concepts.WritableObject;
28 import org.opendaylight.yangtools.concepts.WritableObjects;
30 public final class FrontendClientMetadata implements Identifiable<ClientIdentifier>, WritableObject {
31 private final Collection<FrontendHistoryMetadata> currentHistories;
32 private final RangeSet<UnsignedLong> purgedHistories;
33 private final ClientIdentifier identifier;
35 public FrontendClientMetadata(final ClientIdentifier identifier, final RangeSet<UnsignedLong> purgedHistories,
36 final Collection<FrontendHistoryMetadata> currentHistories) {
37 this.identifier = requireNonNull(identifier);
38 this.purgedHistories = ImmutableRangeSet.copyOf(purgedHistories);
39 this.currentHistories = ImmutableList.copyOf(currentHistories);
42 public Collection<FrontendHistoryMetadata> getCurrentHistories() {
43 return currentHistories;
46 public RangeSet<UnsignedLong> getPurgedHistories() {
47 return purgedHistories;
51 public ClientIdentifier getIdentifier() {
56 public void writeTo(final DataOutput out) throws IOException {
57 identifier.writeTo(out);
59 final Set<Range<UnsignedLong>> ranges = purgedHistories.asRanges();
60 out.writeInt(ranges.size());
61 for (final Range<UnsignedLong> r : ranges) {
62 WritableObjects.writeLongs(out, r.lowerEndpoint().longValue(), r.upperEndpoint().longValue());
65 out.writeInt(currentHistories.size());
66 for (final FrontendHistoryMetadata h : currentHistories) {
71 public static FrontendClientMetadata readFrom(final DataInput in) throws IOException {
72 final ClientIdentifier id = ClientIdentifier.readFrom(in);
74 final int purgedSize = in.readInt();
75 final Builder<UnsignedLong> b = ImmutableRangeSet.builder();
76 for (int i = 0; i < purgedSize; ++i) {
77 final byte header = WritableObjects.readLongHeader(in);
78 final UnsignedLong lower = UnsignedLong.fromLongBits(WritableObjects.readFirstLong(in, header));
79 final UnsignedLong upper = UnsignedLong.fromLongBits(WritableObjects.readSecondLong(in, header));
81 b.add(Range.closedOpen(lower, upper));
84 final int currentSize = in.readInt();
85 final Collection<FrontendHistoryMetadata> currentHistories = new ArrayList<>(currentSize);
86 for (int i = 0; i < currentSize; ++i) {
87 currentHistories.add(FrontendHistoryMetadata.readFrom(in));
90 return new FrontendClientMetadata(id, b.build(), currentHistories);
94 public String toString() {
95 return MoreObjects.toStringHelper(FrontendClientMetadata.class).add("identifer", identifier)
96 .add("current", currentHistories).add("purged", purgedHistories).toString();