Track skipped transactions
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / SkipTransactionsRequestV1.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.access.commands;
9
10 import akka.actor.ActorRef;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.primitives.UnsignedLong;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.List;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.yangtools.concepts.WritableObjects;
19
20 /**
21  * Externalizable proxy for use with {@link SkipTransactionsRequest}. It implements the initial
22  * (Phosphorus SR1) serialization format.
23  */
24 final class SkipTransactionsRequestV1 extends AbstractTransactionRequestProxy<SkipTransactionsRequest> {
25     private List<UnsignedLong> others;
26
27     // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
28     // be able to create instances via reflection.
29     @SuppressWarnings("checkstyle:RedundantModifier")
30     public SkipTransactionsRequestV1() {
31         // For Externalizable
32     }
33
34     SkipTransactionsRequestV1(final SkipTransactionsRequest request) {
35         super(request);
36         others = request.getOthers();
37     }
38
39     @Override
40     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
41         super.readExternal(in);
42
43         final int size = in.readInt();
44         final var builder = ImmutableList.<UnsignedLong>builderWithExpectedSize(size);
45         int idx;
46         if (size % 2 != 0) {
47             builder.add(UnsignedLong.fromLongBits(WritableObjects.readLong(in)));
48             idx = 1;
49         } else {
50             idx = 0;
51         }
52         for (; idx < size; idx += 2) {
53             final byte hdr = WritableObjects.readLongHeader(in);
54             builder.add(UnsignedLong.fromLongBits(WritableObjects.readFirstLong(in, hdr)));
55             builder.add(UnsignedLong.fromLongBits(WritableObjects.readSecondLong(in, hdr)));
56         }
57         others = builder.build();
58     }
59
60     @Override
61     public void writeExternal(final ObjectOutput out) throws IOException {
62         super.writeExternal(out);
63
64         final int size = others.size();
65         out.writeInt(size);
66
67         int idx;
68         if (size % 2 != 0) {
69             WritableObjects.writeLong(out, others.get(0).longValue());
70             idx = 1;
71         } else {
72             idx = 0;
73         }
74         for (; idx < size; idx += 2) {
75             WritableObjects.writeLongs(out, others.get(idx).longValue(), others.get(idx + 1).longValue());
76         }
77     }
78
79     @Override
80     protected SkipTransactionsRequest createRequest(final TransactionIdentifier target, final long sequence,
81             final ActorRef replyToActor) {
82         return new SkipTransactionsRequest(target, sequence, replyToActor, others);
83     }
84 }