CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / ReadOnlyTx.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.sal.connect.netconf.sal.tx;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
23 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
24 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
25 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
37
38     private static final Logger LOG  = LoggerFactory.getLogger(ReadOnlyTx.class);
39
40     private final NetconfBaseOps netconfOps;
41     private final RemoteDeviceId id;
42     private final FutureCallback<DOMRpcResult> loggingCallback;
43
44     public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
45         this.netconfOps = netconfOps;
46         this.id = id;
47
48         // Simple logging callback to log result of read operation
49         loggingCallback = new FutureCallback<DOMRpcResult>() {
50             @Override
51             public void onSuccess(final DOMRpcResult result) {
52                 if(AbstractWriteTx.isSuccess(result)) {
53                     LOG.trace("{}: Reading data successful", id);
54                 } else {
55                     LOG.warn("{}: Reading data unsuccessful: {}", id, result.getErrors());
56                 }
57
58             }
59
60             @Override
61             public void onFailure(final Throwable t) {
62                 LOG.warn("{}: Reading data failed", id, t);
63             }
64         };
65     }
66
67     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
68             final YangInstanceIdentifier path) {
69         final ListenableFuture<DOMRpcResult> configRunning = netconfOps.getConfigRunning(loggingCallback, Optional.fromNullable(path));
70
71         final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configRunning, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
72             @Override
73             public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
74                 checkReadSuccess(result, path);
75
76                 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
77                 return NormalizedNodes.findNode(dataNode, path.getPathArguments());
78             }
79         });
80
81         return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
82     }
83
84     private DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> findDataNode(final DOMRpcResult result) {
85         return ((ContainerNode) result.getResult()).getChild(NetconfMessageTransformUtil.toId(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)).get();
86     }
87
88     private void checkReadSuccess(final DOMRpcResult result, final YangInstanceIdentifier path) {
89         try {
90             Preconditions.checkArgument(AbstractWriteTx.isSuccess(result), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
91         } catch (final IllegalArgumentException e) {
92             LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
93             throw e;
94         }
95     }
96
97     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
98             final YangInstanceIdentifier path) {
99         final ListenableFuture<DOMRpcResult> configCandidate = netconfOps.get(loggingCallback, Optional.fromNullable(path));
100
101         // Find data node and normalize its content
102         final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configCandidate, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
103             @Override
104             public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
105                 checkReadSuccess(result, path);
106
107                 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
108                 return NormalizedNodes.findNode(dataNode, path.getPathArguments());
109             }
110         });
111
112         return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
113     }
114
115     @Override
116     public void close() {
117         // NOOP
118     }
119
120     @Override
121     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
122             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
123         switch (store) {
124             case CONFIGURATION : {
125                 return readConfigurationData(path);
126             }
127             case OPERATIONAL : {
128                 return readOperationalData(path);
129             }
130         }
131
132         throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
133     }
134
135     @Override
136     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
137         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
138
139         try {
140             return Futures.immediateCheckedFuture(data.get().isPresent());
141         } catch (InterruptedException | ExecutionException e) {
142             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Exists failed",e));
143         }
144     }
145
146     @Override
147     public Object getIdentifier() {
148         return this;
149     }
150 }