Enable overwrite test suite
[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 package org.opendaylight.controller.cluster.datastore.modification;
9
10 import java.io.IOException;
11 import java.io.ObjectInput;
12 import java.io.ObjectOutput;
13 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
14 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
15 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
21 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
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     public static WriteModification fromStream(final NormalizedNodeDataInput in, final short version,
82             final ReusableStreamReceiver receiver) throws IOException {
83         final NormalizedNode<?, ?> node = in.readNormalizedNode(receiver);
84         final YangInstanceIdentifier path = in.readYangInstanceIdentifier();
85         return new WriteModification(version, path, node);
86     }
87
88     @Override
89     public void writeTo(final NormalizedNodeDataOutput out) throws IOException {
90         // FIXME: this should be inverted, as the path helps receivers in establishment of context
91         out.writeNormalizedNode(data);
92         out.writeYangInstanceIdentifier(getPath());
93     }
94 }

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