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