Merge "BUG-1690: catch wildcard InstanceIdentifiers"
[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
20   private final NormalizedNode<?, ?> normalizedNode;
21   private final SchemaContext schemaContext;
22   public static final Class SERIALIZABLE_CLASS = ShardTransactionMessages.ReadDataReply.class;
23   public ReadDataReply(SchemaContext context,NormalizedNode<?, ?> normalizedNode){
24
25     this.normalizedNode = normalizedNode;
26     this.schemaContext = context;
27   }
28
29   public NormalizedNode<?, ?> getNormalizedNode() {
30     return normalizedNode;
31   }
32
33   public Object toSerializable(){
34     if(normalizedNode != null) {
35       return ShardTransactionMessages.ReadDataReply.newBuilder()
36           .setNormalizedNode(new NormalizedNodeToNodeCodec(schemaContext)
37                   .encode(YangInstanceIdentifier.builder().build(), normalizedNode).getNormalizedNode()
38           ).build();
39     }else{
40       return ShardTransactionMessages.ReadDataReply.newBuilder().build();
41
42     }
43
44   }
45
46   public static ReadDataReply fromSerializable(SchemaContext schemaContext,YangInstanceIdentifier id,Object serializable){
47     ShardTransactionMessages.ReadDataReply o = (ShardTransactionMessages.ReadDataReply) serializable;
48     return new ReadDataReply(schemaContext,new NormalizedNodeToNodeCodec(schemaContext).decode(id, o.getNormalizedNode()));
49   }
50
51   public static ByteString getNormalizedNodeByteString(Object serializable){
52       ShardTransactionMessages.ReadDataReply o = (ShardTransactionMessages.ReadDataReply) serializable;
53       return ((ShardTransactionMessages.ReadDataReply) serializable).getNormalizedNode().toByteString();
54   }
55 }