Merge "BUG 2412 - restconf @GET getModule(uri) method migration"
[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         this(DataStoreVersions.CURRENT_VERSION);
35     }
36
37     public WriteModification(short version) {
38         super(version);
39     }
40
41     public WriteModification(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
42         super(path);
43         this.data = data;
44     }
45
46     @Override
47     public void apply(final DOMStoreWriteTransaction transaction) {
48         transaction.write(getPath(), data);
49     }
50
51     public NormalizedNode<?, ?> getData() {
52         return data;
53     }
54
55     @Override
56     public byte getType() {
57         return WRITE;
58     }
59
60     @Override
61     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
62         SerializationUtils.deserializePathAndNode(in, this, APPLIER);
63     }
64
65     @Override
66     public void writeExternal(ObjectOutput out) throws IOException {
67         SerializationUtils.serializePathAndNode(getPath(), data, out);
68     }
69
70     @Override
71     @Deprecated
72     public Object toSerializable() {
73         Encoded encoded = new NormalizedNodeToNodeCodec(null).encode(getPath(), getData());
74         return PersistentMessages.Modification.newBuilder().setType(this.getClass().toString())
75                 .setPath(encoded.getEncodedPath()).setData(encoded.getEncodedNode()
76                         .getNormalizedNode()).build();
77     }
78
79     @Deprecated
80     public static WriteModification fromSerializable(final Object serializable) {
81         PersistentMessages.Modification o = (PersistentMessages.Modification) serializable;
82         Decoded decoded = new NormalizedNodeToNodeCodec(null).decode(o.getPath(), o.getData());
83         return new WriteModification(decoded.getDecodedPath(), decoded.getDecodedNode());
84     }
85
86     public static WriteModification fromStream(ObjectInput in, short version)
87             throws ClassNotFoundException, IOException {
88         WriteModification mod = new WriteModification(version);
89         mod.readExternal(in);
90         return mod;
91     }
92
93     private static final Applier<WriteModification> APPLIER = new Applier<WriteModification>() {
94         @Override
95         public void apply(WriteModification instance, YangInstanceIdentifier path,
96                 NormalizedNode<?, ?> node) {
97             instance.setPath(path);
98             instance.data = node;
99         }
100     };
101 }