Adjust to mdsal DOM read/exists FluentFuture change
[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.Preconditions;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.common.api.ReadFailedException;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19
20 public class ReadData extends AbstractRead<Optional<NormalizedNode<?, ?>>> {
21     private static final long serialVersionUID = 1L;
22
23     public ReadData() {
24     }
25
26     public ReadData(final YangInstanceIdentifier path, short version) {
27         super(path, version);
28     }
29
30     @Override
31     public FluentFuture<Optional<NormalizedNode<?, ?>>> apply(DOMStoreReadTransaction readDelegate) {
32         return readDelegate.read(getPath());
33     }
34
35     @Override
36     public void processResponse(Object readResponse, SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
37         if (ReadDataReply.isSerializedType(readResponse)) {
38             ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
39             returnFuture.set(Optional.<NormalizedNode<?, ?>>ofNullable(reply.getNormalizedNode()));
40         } else {
41             returnFuture.setException(new ReadFailedException("Invalid response reading data for path " + getPath()));
42         }
43     }
44
45     @Override
46     protected AbstractRead<Optional<NormalizedNode<?, ?>>> newInstance(short withVersion) {
47         return new ReadData(getPath(), withVersion);
48     }
49
50     public static ReadData fromSerializable(final Object serializable) {
51         Preconditions.checkArgument(serializable instanceof ReadData);
52         return (ReadData)serializable;
53     }
54
55     public static boolean isSerializedType(Object message) {
56         return message instanceof ReadData;
57     }
58 }