Refactor TransactonContext
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadData.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.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 public class ReadData extends AbstractRead<Optional<NormalizedNode<?, ?>>> {
22   public static final Class<ShardTransactionMessages.ReadData> SERIALIZABLE_CLASS =
23           ShardTransactionMessages.ReadData.class;
24
25   public ReadData(final YangInstanceIdentifier path) {
26     super(path);
27   }
28
29   public Object toSerializable(){
30     return ShardTransactionMessages.ReadData.newBuilder()
31         .setInstanceIdentifierPathArguments(InstanceIdentifierUtils.toSerializable(getPath()))
32         .build();
33   }
34
35   public static ReadData fromSerializable(final Object serializable){
36     ShardTransactionMessages.ReadData o = (ShardTransactionMessages.ReadData) serializable;
37     return new ReadData(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()));
38   }
39
40     @Override
41     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> apply(DOMStoreReadTransaction readDelegate) {
42         return readDelegate.read(getPath());
43     }
44
45     @Override
46     public void processResponse(Object readResponse, SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
47         if(readResponse instanceof ReadDataReply) {
48             ReadDataReply reply = (ReadDataReply) readResponse;
49             returnFuture.set(Optional.<NormalizedNode<?, ?>> fromNullable(reply.getNormalizedNode()));
50
51         } else if(ReadDataReply.isSerializedType(readResponse)) {
52             ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
53             returnFuture.set(Optional.<NormalizedNode<?, ?>> fromNullable(reply.getNormalizedNode()));
54
55         } else {
56             returnFuture.setException(new ReadFailedException("Invalid response reading data for path " + getPath()));
57         }
58     }
59 }