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