Bug 2003: CDS serialization improvements
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadDataReply.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 com.google.protobuf.ByteString;
12 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
13 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 public class ReadDataReply implements SerializableMessage {
19     public static final Class<ShardTransactionMessages.ReadDataReply> SERIALIZABLE_CLASS =
20             ShardTransactionMessages.ReadDataReply.class;
21
22     private final NormalizedNode<?, ?> normalizedNode;
23     private final SchemaContext schemaContext;
24
25     public ReadDataReply(SchemaContext context,NormalizedNode<?, ?> normalizedNode){
26
27         this.normalizedNode = normalizedNode;
28         this.schemaContext = context;
29     }
30
31     public NormalizedNode<?, ?> getNormalizedNode() {
32         return normalizedNode;
33     }
34
35     @Override
36     public Object toSerializable(){
37         if(normalizedNode != null) {
38             return ShardTransactionMessages.ReadDataReply.newBuilder()
39                     .setNormalizedNode(new NormalizedNodeToNodeCodec(schemaContext)
40                         .encode(normalizedNode).getNormalizedNode()).build();
41         } else {
42             return ShardTransactionMessages.ReadDataReply.newBuilder().build();
43
44         }
45     }
46
47     public static ReadDataReply fromSerializable(SchemaContext schemaContext,
48             YangInstanceIdentifier id, Object serializable) {
49         ShardTransactionMessages.ReadDataReply o = (ShardTransactionMessages.ReadDataReply) serializable;
50         return new ReadDataReply(schemaContext, new NormalizedNodeToNodeCodec(schemaContext).decode(
51                 o.getNormalizedNode()));
52     }
53
54     public static ByteString getNormalizedNodeByteString(Object serializable){
55         ShardTransactionMessages.ReadDataReply o = (ShardTransactionMessages.ReadDataReply) serializable;
56         return ((ShardTransactionMessages.ReadDataReply) serializable).getNormalizedNode().toByteString();
57     }
58 }