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