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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.