Bump cds-access-api ABIVersion
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ModifyTransactionRequestProxyV1.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.access.commands;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import com.google.common.collect.ImmutableList;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
23 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
24 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
25
26 /**
27  * Externalizable proxy for use with {@link ExistsTransactionRequest}. It implements the initial (Boron) serialization
28  * format.
29  *
30  * @author Robert Varga
31  */
32 final class ModifyTransactionRequestProxyV1 extends AbstractTransactionRequestProxy<ModifyTransactionRequest> {
33     private static final long serialVersionUID = 1L;
34     private List<TransactionModification> modifications;
35     private Optional<PersistenceProtocol> protocol;
36     private transient NormalizedNodeStreamVersion streamVersion;
37
38     // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
39     // be able to create instances via reflection.
40     @SuppressWarnings("checkstyle:RedundantModifier")
41     public ModifyTransactionRequestProxyV1() {
42         // For Externalizable
43     }
44
45     ModifyTransactionRequestProxyV1(final ModifyTransactionRequest request) {
46         super(request);
47         this.modifications = requireNonNull(request.getModifications());
48         this.protocol = request.getPersistenceProtocol();
49         this.streamVersion = request.getVersion().getStreamVersion();
50     }
51
52     @Override
53     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
54         super.readExternal(in);
55
56         protocol = Optional.ofNullable(PersistenceProtocol.readFrom(in));
57
58         final int size = in.readInt();
59         if (size != 0) {
60             modifications = new ArrayList<>(size);
61             final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(in);
62             for (int i = 0; i < size; ++i) {
63                 modifications.add(TransactionModification.readFrom(nnin));
64             }
65         } else {
66             modifications = ImmutableList.of();
67         }
68     }
69
70     @Override
71     public void writeExternal(final ObjectOutput out) throws IOException {
72         super.writeExternal(out);
73
74         out.writeByte(PersistenceProtocol.byteValue(protocol.orElse(null)));
75         out.writeInt(modifications.size());
76         if (!modifications.isEmpty()) {
77             try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out, streamVersion)) {
78                 for (TransactionModification op : modifications) {
79                     op.writeTo(nnout);
80                 }
81             }
82         }
83     }
84
85     @Override
86     protected ModifyTransactionRequest createRequest(final TransactionIdentifier target, final long sequence,
87             final ActorRef replyTo) {
88         return new ModifyTransactionRequest(target, sequence, replyTo, modifications, protocol.orElse(null));
89     }
90 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.