c9b243052868bb7f0f8cd7c4b9461f05ad6a9ecc
[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.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;
29
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;
34
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);
40     }
41
42     public Collection<FrontendHistoryMetadata> getCurrentHistories() {
43         return currentHistories;
44     }
45
46     public RangeSet<UnsignedLong> getPurgedHistories() {
47         return purgedHistories;
48     }
49
50     @Override
51     public ClientIdentifier getIdentifier() {
52         return identifier;
53     }
54
55     @Override
56     public void writeTo(final DataOutput out) throws IOException {
57         identifier.writeTo(out);
58
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());
63         }
64
65         out.writeInt(currentHistories.size());
66         for (final FrontendHistoryMetadata h : currentHistories) {
67             h.writeTo(out);
68         }
69     }
70
71     public static FrontendClientMetadata readFrom(final DataInput in) throws IOException {
72         final ClientIdentifier id = ClientIdentifier.readFrom(in);
73
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));
80
81             b.add(Range.closedOpen(lower, upper));
82         }
83
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));
88         }
89
90         return new FrontendClientMetadata(id, b.build(), currentHistories);
91     }
92
93     @Override
94     public String toString() {
95         return MoreObjects.toStringHelper(FrontendClientMetadata.class).add("identifer", identifier)
96                 .add("current", currentHistories).add("purged", purgedHistories).toString();
97     }
98 }