Add support for reusable streaming
[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 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
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) throws IOException {
71         SerializationUtils.readNodeAndPath(in, this, (instance, path, node) -> {
72             instance.setPath(path);
73             instance.data = node;
74         });
75     }
76
77     @Override
78     public void writeExternal(final ObjectOutput out) throws IOException {
79         SerializationUtils.writeNodeAndPath(out, getPath(), data);
80     }
81
82     public static WriteModification fromStream(final NormalizedNodeDataInput in, final short version,
83             final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
84         final NormalizedNode<?, ?> node = in.readNormalizedNode(writer);
85         final YangInstanceIdentifier path = in.readYangInstanceIdentifier();
86         return new WriteModification(version, path, node);
87     }
88
89     @Override
90     public void writeTo(final NormalizedNodeDataOutput out) throws IOException {
91         // FIXME: this should be inverted, as the path helps receivers in establishment of context
92         out.writeNormalizedNode(data);
93         out.writeYangInstanceIdentifier(getPath());
94     }
95 }

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