3637aa8ac98acb2d1ba6f536f3e88b24e9353196
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ReadTransactionSuccessProxyV1.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 com.google.common.base.Optional;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
16 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 /**
20  * Externalizable proxy for use with {@link ReadTransactionSuccess}. It implements the initial (Boron) serialization
21  * format.
22  *
23  * @author Robert Varga
24  */
25 final class ReadTransactionSuccessProxyV1 extends AbstractTransactionSuccessProxy<ReadTransactionSuccess> {
26     private static final long serialVersionUID = 1L;
27     private Optional<NormalizedNode<?, ?>> data;
28
29     // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
30     // be able to create instances via reflection.
31     @SuppressWarnings("checkstyle:RedundantModifier")
32     public ReadTransactionSuccessProxyV1() {
33         // For Externalizable
34     }
35
36     ReadTransactionSuccessProxyV1(final ReadTransactionSuccess request) {
37         super(request);
38         this.data = request.getData();
39     }
40
41     @Override
42     public void writeExternal(final ObjectOutput out) throws IOException {
43         super.writeExternal(out);
44
45         if (data.isPresent()) {
46             out.writeBoolean(true);
47             try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out)) {
48                 nnout.writeNormalizedNode(data.get());
49             }
50         } else {
51             out.writeBoolean(false);
52         }
53
54         out.writeObject(data);
55     }
56
57     @Override
58     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
59         super.readExternal(in);
60
61         if (in.readBoolean()) {
62             data = Optional.of(NormalizedNodeInputOutput.newDataInput(in).readNormalizedNode());
63         } else {
64             data = Optional.absent();
65         }
66     }
67
68     @Override
69     protected ReadTransactionSuccess createSuccess(final TransactionIdentifier target, final long sequence) {
70         return new ReadTransactionSuccess(target, sequence, data);
71     }
72 }