Bug 2597: Batch modification operations in TransactionProxy
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / MergeData.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.messages;
10
11 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
12 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
13 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec.Decoded;
14 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec.Encoded;
15 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 /**
20  * @deprecated Replaced by BatchedModifications.
21  */
22 @Deprecated
23 public class MergeData extends ModifyData {
24     private static final long serialVersionUID = 1L;
25
26     public static final Class<MergeData> SERIALIZABLE_CLASS = MergeData.class;
27
28     public MergeData() {
29     }
30
31     public MergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data, short version) {
32         super(path, data, version);
33     }
34
35     @Override
36     public Object toSerializable() {
37         if(getVersion() >= DataStoreVersions.LITHIUM_VERSION) {
38             return this;
39         } else {
40             // To base or R1 Helium version
41             Encoded encoded = new NormalizedNodeToNodeCodec(null).encode(getPath(), getData());
42             return ShardTransactionMessages.MergeData.newBuilder()
43                     .setInstanceIdentifierPathArguments(encoded.getEncodedPath())
44                     .setNormalizedNode(encoded.getEncodedNode().getNormalizedNode()).build();
45         }
46     }
47
48     public static MergeData fromSerializable(Object serializable){
49         if(serializable instanceof MergeData) {
50             return (MergeData) serializable;
51         } else {
52             // From base or R1 Helium version
53             ShardTransactionMessages.MergeData o = (ShardTransactionMessages.MergeData) serializable;
54             Decoded decoded = new NormalizedNodeToNodeCodec(null).decode(
55                     o.getInstanceIdentifierPathArguments(), o.getNormalizedNode());
56             return new MergeData(decoded.getDecodedPath(), decoded.getDecodedNode(),
57                     DataStoreVersions.HELIUM_2_VERSION);
58         }
59     }
60
61     public static boolean isSerializedType(Object message) {
62         return SERIALIZABLE_CLASS.isInstance(message) ||
63                message instanceof ShardTransactionMessages.MergeData;
64     }
65 }