Add support for reusable streaming
[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 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
26
27 /**
28  * Externalizable proxy for use with {@link ExistsTransactionRequest}. It implements the initial (Boron) serialization
29  * format.
30  *
31  * @author Robert Varga
32  */
33 final class ModifyTransactionRequestProxyV1 extends AbstractTransactionRequestProxy<ModifyTransactionRequest> {
34     private static final long serialVersionUID = 1L;
35     private List<TransactionModification> modifications;
36     private Optional<PersistenceProtocol> protocol;
37     private transient NormalizedNodeStreamVersion streamVersion;
38
39     // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
40     // be able to create instances via reflection.
41     @SuppressWarnings("checkstyle:RedundantModifier")
42     public ModifyTransactionRequestProxyV1() {
43         // For Externalizable
44     }
45
46     ModifyTransactionRequestProxyV1(final ModifyTransactionRequest request) {
47         super(request);
48         this.modifications = requireNonNull(request.getModifications());
49         this.protocol = request.getPersistenceProtocol();
50         this.streamVersion = request.getVersion().getStreamVersion();
51     }
52
53     @Override
54     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
55         super.readExternal(in);
56
57         protocol = Optional.ofNullable(PersistenceProtocol.readFrom(in));
58
59         final int size = in.readInt();
60         if (size != 0) {
61             modifications = new ArrayList<>(size);
62             final NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(in);
63             final ReusableImmutableNormalizedNodeStreamWriter writer =
64                     ReusableImmutableNormalizedNodeStreamWriter.create();
65             for (int i = 0; i < size; ++i) {
66                 modifications.add(TransactionModification.readFrom(nnin, writer));
67             }
68         } else {
69             modifications = ImmutableList.of();
70         }
71     }
72
73     @Override
74     public void writeExternal(final ObjectOutput out) throws IOException {
75         super.writeExternal(out);
76
77         out.writeByte(PersistenceProtocol.byteValue(protocol.orElse(null)));
78         out.writeInt(modifications.size());
79         if (!modifications.isEmpty()) {
80             try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out, streamVersion)) {
81                 for (TransactionModification op : modifications) {
82                     op.writeTo(nnout);
83                 }
84             }
85         }
86     }
87
88     @Override
89     protected ModifyTransactionRequest createRequest(final TransactionIdentifier target, final long sequence,
90             final ActorRef replyTo) {
91         return new ModifyTransactionRequest(target, sequence, replyTo, modifications, protocol.orElse(null));
92     }
93 }

©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.