Merge "BUG-2288: deprecate old binding Notification API elements"
[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.NormalizedNodeToNodeCodec;
16 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec.Decoded;
17 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec.Encoded;
18 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
19 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils.Applier;
20 import org.opendaylight.controller.protobuff.messages.persistent.PersistentMessages;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25
26 /**
27  * WriteModification stores all the parameters required to write data to the specified path
28  */
29 public class WriteModification extends AbstractModification {
30     private static final long serialVersionUID = 1L;
31
32     private NormalizedNode<?, ?> data;
33
34     public WriteModification() {
35         this(DataStoreVersions.CURRENT_VERSION);
36     }
37
38     public WriteModification(short version) {
39         super(version);
40     }
41
42     public WriteModification(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
43         super(path);
44         this.data = data;
45     }
46
47     @Override
48     public void apply(final DOMStoreWriteTransaction transaction) {
49         transaction.write(getPath(), data);
50     }
51
52     @Override
53     public void apply(final DataTreeModification transaction) {
54         transaction.write(getPath(), data);
55     }
56
57     public NormalizedNode<?, ?> getData() {
58         return data;
59     }
60
61     @Override
62     public byte getType() {
63         return WRITE;
64     }
65
66     @Override
67     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
68         SerializationUtils.deserializePathAndNode(in, this, APPLIER);
69     }
70
71     @Override
72     public void writeExternal(ObjectOutput out) throws IOException {
73         SerializationUtils.serializePathAndNode(getPath(), data, out);
74     }
75
76     @Override
77     @Deprecated
78     public Object toSerializable() {
79         Encoded encoded = new NormalizedNodeToNodeCodec(null).encode(getPath(), getData());
80         return PersistentMessages.Modification.newBuilder().setType(this.getClass().toString())
81                 .setPath(encoded.getEncodedPath()).setData(encoded.getEncodedNode()
82                         .getNormalizedNode()).build();
83     }
84
85     @Deprecated
86     public static WriteModification fromSerializable(final Object serializable) {
87         PersistentMessages.Modification o = (PersistentMessages.Modification) serializable;
88         Decoded decoded = new NormalizedNodeToNodeCodec(null).decode(o.getPath(), o.getData());
89         return new WriteModification(decoded.getDecodedPath(), decoded.getDecodedNode());
90     }
91
92     public static WriteModification fromStream(ObjectInput in, short version)
93             throws ClassNotFoundException, IOException {
94         WriteModification mod = new WriteModification(version);
95         mod.readExternal(in);
96         return mod;
97     }
98
99     private static final Applier<WriteModification> APPLIER = new Applier<WriteModification>() {
100         @Override
101         public void apply(WriteModification instance, YangInstanceIdentifier path,
102                 NormalizedNode<?, ?> node) {
103             instance.setPath(path);
104             instance.data = node;
105         }
106     };
107 }