BUG 2412 - restconf @GET getModule(identifier,uri) method migration
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / WriteData.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 public class WriteData extends ModifyData implements VersionedSerializableMessage {
20     private static final long serialVersionUID = 1L;
21
22     public static final Class<WriteData> SERIALIZABLE_CLASS = WriteData.class;
23
24     public WriteData() {
25     }
26
27     public WriteData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
28         super(path, data);
29     }
30
31     @Override
32     public Object toSerializable(short toVersion) {
33         if(toVersion >= DataStoreVersions.LITHIUM_VERSION) {
34             setVersion(toVersion);
35             return this;
36         } else {
37             // To base or R1 Helium version
38             Encoded encoded = new NormalizedNodeToNodeCodec(null).encode(getPath(), getData());
39             return ShardTransactionMessages.WriteData.newBuilder()
40                     .setInstanceIdentifierPathArguments(encoded.getEncodedPath())
41                     .setNormalizedNode(encoded.getEncodedNode().getNormalizedNode()).build();
42         }
43     }
44
45     public static WriteData fromSerializable(Object serializable) {
46         if(serializable instanceof WriteData) {
47             return (WriteData) serializable;
48         } else {
49             // From base or R1 Helium version
50             ShardTransactionMessages.WriteData o = (ShardTransactionMessages.WriteData) serializable;
51             Decoded decoded = new NormalizedNodeToNodeCodec(null).decode(
52                     o.getInstanceIdentifierPathArguments(), o.getNormalizedNode());
53             return new WriteData(decoded.getDecodedPath(), decoded.getDecodedNode());
54         }
55     }
56
57     public static boolean isSerializedType(Object message) {
58         return SERIALIZABLE_CLASS.isInstance(message) ||
59                message instanceof ShardTransactionMessages.WriteData;
60     }
61 }