Move byte-based serialization method
[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.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.controller.cluster.datastore.node.utils.stream.SerializationUtils.Applier;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20
21 /**
22  * WriteModification stores all the parameters required to write data to the specified path.
23  */
24 public class WriteModification extends AbstractModification {
25     private static final long serialVersionUID = 1L;
26
27     private NormalizedNode<?, ?> data;
28
29     public WriteModification() {
30         this(DataStoreVersions.CURRENT_VERSION);
31     }
32
33     public WriteModification(short version) {
34         super(version);
35     }
36
37     public WriteModification(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
38         super(path);
39         this.data = data;
40     }
41
42     @Override
43     public void apply(final DOMStoreWriteTransaction transaction) {
44         transaction.write(getPath(), data);
45     }
46
47     @Override
48     public void apply(final DataTreeModification transaction) {
49         transaction.write(getPath(), data);
50     }
51
52     public NormalizedNode<?, ?> getData() {
53         return data;
54     }
55
56     @Override
57     public byte getType() {
58         return WRITE;
59     }
60
61     @Override
62     public void readExternal(ObjectInput in) {
63         SerializationUtils.deserializePathAndNode(in, this, APPLIER);
64     }
65
66     @Override
67     public void writeExternal(ObjectOutput out) {
68         SerializationUtils.serializePathAndNode(getPath(), data, out);
69     }
70
71     public static WriteModification fromStream(ObjectInput in, short version) {
72         WriteModification mod = new WriteModification(version);
73         mod.readExternal(in);
74         return mod;
75     }
76
77     private static final Applier<WriteModification> APPLIER = (instance, path, node) -> {
78         instance.setPath(path);
79         instance.data = node;
80     };
81 }

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