Follow-up to protobuff deprecation
[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.DataStoreVersions;
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
20 public class DataExists extends AbstractRead<Boolean> {
21     private static final long serialVersionUID = 1L;
22
23     public DataExists() {
24     }
25
26     public DataExists(final YangInstanceIdentifier path, final short version) {
27         super(path, version);
28     }
29
30     @Deprecated
31     @Override
32     protected Object newLegacySerializedInstance() {
33         return ShardTransactionMessages.DataExists.newBuilder()
34                 .setInstanceIdentifierPathArguments(InstanceIdentifierUtils.toSerializable(getPath())).build();
35     }
36
37     @Override
38     public CheckedFuture<Boolean, ReadFailedException> apply(DOMStoreReadTransaction readDelegate) {
39         return readDelegate.exists(getPath());
40     }
41
42     @Override
43     public void processResponse(Object response, SettableFuture<Boolean> returnFuture) {
44         if(DataExistsReply.isSerializedType(response)) {
45             returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
46         } else {
47             returnFuture.setException(new ReadFailedException("Invalid response checking exists for path " + getPath()));
48         }
49     }
50
51     @Override
52     protected AbstractRead<Boolean> newInstance(short withVersion) {
53         return new DataExists(getPath(), withVersion);
54     }
55
56     public static DataExists fromSerializable(final Object serializable){
57         if(serializable instanceof DataExists) {
58             return (DataExists)serializable;
59         } else {
60             ShardTransactionMessages.DataExists o = (ShardTransactionMessages.DataExists) serializable;
61             return new DataExists(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()),
62                     DataStoreVersions.LITHIUM_VERSION);
63         }
64     }
65
66     public static boolean isSerializedType(Object message) {
67         return message instanceof DataExists || message instanceof ShardTransactionMessages.DataExists;
68     }
69 }