Bug 1392: Change ReadTransaction#read to return CheckedFuture
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / NetconfDeviceReadOnlyTx.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 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
9
10 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.CONFIG_SOURCE_RUNNING;
11 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
12 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
13 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
14 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
15
16 import com.google.common.base.Function;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
25 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
27 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
28 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.controller.sal.core.api.RpcImplementation;
30 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.Node;
35 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 public final class NetconfDeviceReadOnlyTx implements DOMDataReadOnlyTransaction {
42
43     private static final Logger LOG  = LoggerFactory.getLogger(NetconfDeviceReadOnlyTx.class);
44
45     private final RpcImplementation rpc;
46     private final DataNormalizer normalizer;
47     private final RemoteDeviceId id;
48
49     public NetconfDeviceReadOnlyTx(final RpcImplementation rpc, final DataNormalizer normalizer, final RemoteDeviceId id) {
50         this.rpc = rpc;
51         this.normalizer = normalizer;
52         this.id = id;
53     }
54
55     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
56             final YangInstanceIdentifier path) {
57         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
58                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, CONFIG_SOURCE_RUNNING, toFilterStructure(path)));
59
60         ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
61             @Override
62             public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
63                 checkReadSuccess(result, path);
64
65                 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
66                 final CompositeNode node = (CompositeNode) findNode(data, path);
67
68                 return data == null ?
69                         Optional.<NormalizedNode<?, ?>>absent() :
70                         transform(path, node);
71             }
72         });
73
74         return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
75     }
76
77     private void checkReadSuccess(final RpcResult<CompositeNode> result, final YangInstanceIdentifier path) {
78         LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
79         Preconditions.checkArgument(result.isSuccessful(), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
80     }
81
82     private Optional<NormalizedNode<?, ?>> transform(final YangInstanceIdentifier path, final CompositeNode node) {
83         if(node == null) {
84             return Optional.absent();
85         }
86         try {
87             return Optional.<NormalizedNode<?, ?>>of(normalizer.toNormalized(path, node).getValue());
88         } catch (final Exception e) {
89             LOG.error("{}: Unable to normalize data for {}, data: {}", id, path, node, e);
90             throw e;
91         }
92     }
93
94     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
95             final YangInstanceIdentifier path) {
96         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(path)));
97
98         ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
99             @Override
100             public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
101                 checkReadSuccess(result, path);
102
103                 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
104                 final CompositeNode node = (CompositeNode) findNode(data, path);
105
106                 return data == null ?
107                         Optional.<NormalizedNode<?, ?>>absent() :
108                         transform(path, node);
109             }
110         });
111
112         return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
113     }
114
115     private static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
116
117         Node<?> current = node;
118         for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
119             if (current instanceof SimpleNode<?>) {
120                 return null;
121             } else if (current instanceof CompositeNode) {
122                 final CompositeNode currentComposite = (CompositeNode) current;
123
124                 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
125                 if (current == null) {
126                     current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
127                 }
128                 if (current == null) {
129                     current = currentComposite.getFirstSimpleByName(arg.getNodeType());
130                 }
131                 if (current == null) {
132                     current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
133                 }
134                 if (current == null) {
135                     return null;
136                 }
137             }
138         }
139         return current;
140     }
141
142     @Override
143     public void close() {
144         // NOOP
145     }
146
147     @Override
148     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
149             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
150         final YangInstanceIdentifier legacyPath = toLegacyPath(normalizer, path, id);
151
152         switch (store) {
153             case CONFIGURATION : {
154                 return readConfigurationData(legacyPath);
155             }
156             case OPERATIONAL : {
157                 return readOperationalData(legacyPath);
158             }
159         }
160
161         throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
162     }
163
164     static YangInstanceIdentifier toLegacyPath(final DataNormalizer normalizer, final YangInstanceIdentifier path, final RemoteDeviceId id) {
165         try {
166             return normalizer.toLegacy(path);
167         } catch (final DataNormalizationException e) {
168             throw new IllegalArgumentException(id + ": Cannot normalize path " + path, e);
169         }
170     }
171
172     @Override
173     public Object getIdentifier() {
174         return this;
175     }
176 }