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