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