6c46bed7626f27fd86d1f4b11c9462bbda395611
[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 com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
19 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
21 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
22 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.controller.sal.core.api.RpcImplementation;
24 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.util.concurrent.ExecutionException;
33
34 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.CONFIG_SOURCE_RUNNING;
35 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
36 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
37 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
38 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
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         final 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) NetconfMessageTransformUtil.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         try {
79             Preconditions.checkArgument(result.isSuccessful(), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
80         } catch (IllegalArgumentException e) {
81             LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
82             throw e;
83         }
84     }
85
86     private Optional<NormalizedNode<?, ?>> transform(final YangInstanceIdentifier path, final CompositeNode node) {
87         if(node == null) {
88             return Optional.absent();
89         }
90         try {
91             return Optional.<NormalizedNode<?, ?>>of(normalizer.toNormalized(path, node).getValue());
92         } catch (final Exception e) {
93             LOG.error("{}: Unable to normalize data for {}, data: {}", id, path, node, e);
94             throw e;
95         }
96     }
97
98     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
99             final YangInstanceIdentifier path) {
100         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(path)));
101
102         final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
103             @Override
104             public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
105                 checkReadSuccess(result, path);
106
107                 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
108                 final CompositeNode node = (CompositeNode) NetconfMessageTransformUtil.findNode(data, path);
109
110                 return data == null ?
111                         Optional.<NormalizedNode<?, ?>>absent() :
112                         transform(path, node);
113             }
114         });
115
116         return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
117     }
118
119     @Override
120     public void close() {
121         // NOOP
122     }
123
124     @Override
125     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
126             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
127         final YangInstanceIdentifier legacyPath = toLegacyPath(normalizer, path, id);
128
129         switch (store) {
130             case CONFIGURATION : {
131                 return readConfigurationData(legacyPath);
132             }
133             case OPERATIONAL : {
134                 return readOperationalData(legacyPath);
135             }
136         }
137
138         throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
139     }
140
141     @Override public CheckedFuture<Boolean, ReadFailedException> exists(
142         LogicalDatastoreType store,
143         YangInstanceIdentifier path) {
144         CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>
145             data = read(store, path);
146
147         try {
148             return Futures.immediateCheckedFuture(data.get().isPresent());
149         } catch (InterruptedException | ExecutionException e) {
150             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Exists failed",e));
151         }
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 }