2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.messages;
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;
19 public class DataExists extends AbstractRead<Boolean> {
21 public static final Class<ShardTransactionMessages.DataExists> SERIALIZABLE_CLASS =
22 ShardTransactionMessages.DataExists.class;
24 public DataExists(final YangInstanceIdentifier path) {
28 @Override public Object toSerializable() {
29 return ShardTransactionMessages.DataExists.newBuilder()
30 .setInstanceIdentifierPathArguments(
31 InstanceIdentifierUtils.toSerializable(getPath())).build();
34 public static DataExists fromSerializable(final Object serializable){
35 ShardTransactionMessages.DataExists o = (ShardTransactionMessages.DataExists) serializable;
36 return new DataExists(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()));
40 public CheckedFuture<Boolean, ReadFailedException> apply(DOMStoreReadTransaction readDelegate) {
41 return readDelegate.exists(getPath());
45 public void processResponse(Object response, SettableFuture<Boolean> returnFuture) {
46 if(response instanceof DataExistsReply) {
47 returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
49 } else if(response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
50 returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
53 returnFuture.setException(new ReadFailedException("Invalid response checking exists for path " + getPath()));