Merge "BUG 2667 : Handle null value type"
[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
25 /**
26  * WriteModification stores all the parameters required to write data to the specified path
27  */
28 public class WriteModification extends AbstractModification {
29     private static final long serialVersionUID = 1L;
30
31     private NormalizedNode<?, ?> data;
32
33     public WriteModification() {
34     }
35
36     public WriteModification(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
37         super(path);
38         this.data = data;
39     }
40
41     @Override
42     public void apply(final DOMStoreWriteTransaction transaction) {
43         transaction.write(getPath(), data);
44     }
45
46     public NormalizedNode<?, ?> getData() {
47         return data;
48     }
49
50     @Override
51     public byte getType() {
52         return WRITE;
53     }
54
55     @Override
56     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
57         in.readShort(); // version
58
59         SerializationUtils.deserializePathAndNode(in, this, APPLIER);
60     }
61
62     @Override
63     public void writeExternal(ObjectOutput out) throws IOException {
64         out.writeShort(DataStoreVersions.CURRENT_VERSION);
65         SerializationUtils.serializePathAndNode(getPath(), data, out);
66     }
67
68     @Override
69     @Deprecated
70     public Object toSerializable() {
71         Encoded encoded = new NormalizedNodeToNodeCodec(null).encode(getPath(), getData());
72         return PersistentMessages.Modification.newBuilder().setType(this.getClass().toString())
73                 .setPath(encoded.getEncodedPath()).setData(encoded.getEncodedNode()
74                         .getNormalizedNode()).build();
75     }
76
77     @Deprecated
78     public static WriteModification fromSerializable(final Object serializable) {
79         PersistentMessages.Modification o = (PersistentMessages.Modification) serializable;
80         Decoded decoded = new NormalizedNodeToNodeCodec(null).decode(o.getPath(), o.getData());
81         return new WriteModification(decoded.getDecodedPath(), decoded.getDecodedNode());
82     }
83
84     public static WriteModification fromStream(ObjectInput in) throws ClassNotFoundException, IOException {
85         WriteModification mod = new WriteModification();
86         mod.readExternal(in);
87         return mod;
88     }
89
90     private static final Applier<WriteModification> APPLIER = new Applier<WriteModification>() {
91         @Override
92         public void apply(WriteModification instance, YangInstanceIdentifier path,
93                 NormalizedNode<?, ?> node) {
94             instance.setPath(path);
95             instance.data = node;
96         }
97     };
98 }