Remove use of thread-local input
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / modification / WriteModification.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore.modification;
10
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
16 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
17 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
22
23 /**
24  * WriteModification stores all the parameters required to write data to the specified path.
25  */
26 public class WriteModification extends AbstractModification {
27     private static final long serialVersionUID = 1L;
28
29     private NormalizedNode<?, ?> data;
30
31     public WriteModification() {
32         this(DataStoreVersions.CURRENT_VERSION);
33     }
34
35     public WriteModification(final short version) {
36         super(version);
37     }
38
39     WriteModification(final short version, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
40         super(version, path);
41         this.data = data;
42     }
43
44     public WriteModification(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
45         super(path);
46         this.data = data;
47     }
48
49     @Override
50     public void apply(final DOMStoreWriteTransaction transaction) {
51         transaction.write(getPath(), data);
52     }
53
54     @Override
55     public void apply(final DataTreeModification transaction) {
56         transaction.write(getPath(), data);
57     }
58
59     public NormalizedNode<?, ?> getData() {
60         return data;
61     }
62
63     @Override
64     public byte getType() {
65         return WRITE;
66     }
67
68     @Override
69     public void readExternal(final ObjectInput in) throws IOException {
70         SerializationUtils.readNodeAndPath(in, this, (instance, path, node) -> {
71             instance.setPath(path);
72             instance.data = node;
73         });
74     }
75
76     @Override
77     public void writeExternal(final ObjectOutput out) throws IOException {
78         SerializationUtils.writeNodeAndPath(out, getPath(), data);
79     }
80
81     @Deprecated
82     public static WriteModification fromStream(final ObjectInput in, final short version) {
83         WriteModification mod = new WriteModification(version);
84         try {
85             mod.readExternal(in);
86         } catch (IOException e) {
87             throw new IllegalArgumentException("Error deserializing WriteModification", e);
88         }
89         return mod;
90     }
91
92     public static WriteModification fromStream(final NormalizedNodeDataInput in, final short version)
93             throws IOException {
94         final NormalizedNode<?, ?> node = in.readNormalizedNode();
95         final YangInstanceIdentifier path = in.readYangInstanceIdentifier();
96         return new WriteModification(version, path, node);
97     }
98
99     @Override
100     public void writeTo(final NormalizedNodeDataOutput out) throws IOException {
101         // FIXME: this should be inverted, as the path helps receivers in establishment of context
102         out.writeNormalizedNode(data);
103         out.writeYangInstanceIdentifier(getPath());
104     }
105 }