13e1cf0d5a48709b239ccb6ea5f146f08d0e8cf4
[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     public ReadTransactionSuccessProxyV1() {
30         // For Externalizable
31     }
32
33     ReadTransactionSuccessProxyV1(final ReadTransactionSuccess request) {
34         super(request);
35         this.data = request.getData();
36     }
37
38     @Override
39     public void writeExternal(final ObjectOutput out) throws IOException {
40         super.writeExternal(out);
41
42         if (data.isPresent()) {
43             out.writeBoolean(true);
44             try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out)) {
45                 nnout.writeNormalizedNode(data.get());
46             }
47         } else {
48             out.writeBoolean(false);
49         }
50
51         out.writeObject(data);
52     }
53
54     @Override
55     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
56         super.readExternal(in);
57
58         if (in.readBoolean()) {
59             data = Optional.of(NormalizedNodeInputOutput.newDataInput(in).readNormalizedNode());
60         } else {
61             data = Optional.absent();
62         }
63     }
64
65     @Override
66     protected ReadTransactionSuccess createSuccess(final TransactionIdentifier target, final long sequence,
67             final long retry) {
68         return new ReadTransactionSuccess(target, sequence, retry, data);
69     }
70 }