Eliminate SchemaResourcesDTO
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / MonitoringSchemaSourceProvider.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.netconf.client.mdsal;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.Optional;
17 import javax.xml.transform.dom.DOMSource;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.netconf.client.mdsal.api.NetconfRpcService;
20 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.GetSchema;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.get.schema.output.Data;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
34 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.w3c.dom.Element;
38
39 /**
40  * A {@link SchemaSourceProvider} producing {@link YangTextSource}s based on a device's
41  * {@code ietf-netconf-monitoring} interface. The set of available sources is not pre-determined and each request is
42  * dispatched to the device, i.e. this provider reflects real-time updates to available schemas.
43  */
44 public final class MonitoringSchemaSourceProvider implements SchemaSourceProvider<YangTextSource> {
45     private static final Logger LOG = LoggerFactory.getLogger(MonitoringSchemaSourceProvider.class);
46     private static final NodeIdentifier FORMAT_PATHARG =
47             NodeIdentifier.create(QName.create(GetSchema.QNAME, "format").intern());
48     private static final NodeIdentifier GET_SCHEMA_PATHARG = NodeIdentifier.create(GetSchema.QNAME);
49     private static final NodeIdentifier IDENTIFIER_PATHARG =
50             NodeIdentifier.create(QName.create(GetSchema.QNAME, "identifier").intern());
51     private static final NodeIdentifier VERSION_PATHARG =
52             NodeIdentifier.create(QName.create(GetSchema.QNAME, "version").intern());
53     private static final LeafNode<?> FORMAT_LEAF = ImmutableNodes.leafNode(FORMAT_PATHARG, Yang.QNAME);
54     private static final NodeIdentifier NETCONF_DATA_PATHARG = NodeIdentifier.create(Data.QNAME);
55
56     private final NetconfRpcService rpc;
57     private final RemoteDeviceId id;
58
59     public MonitoringSchemaSourceProvider(final RemoteDeviceId id, final NetconfRpcService rpc) {
60         this.id = requireNonNull(id);
61         this.rpc = requireNonNull(rpc);
62     }
63
64     public static @NonNull ContainerNode createGetSchemaRequest(final String moduleName,
65             final Optional<String> revision) {
66         final var builder = ImmutableNodes.newContainerBuilder()
67             .withNodeIdentifier(GET_SCHEMA_PATHARG)
68             .withChild(ImmutableNodes.leafNode(IDENTIFIER_PATHARG, moduleName))
69             .withChild(FORMAT_LEAF);
70         revision.ifPresent(rev -> builder.withChild(ImmutableNodes.leafNode(VERSION_PATHARG, rev)));
71         return builder.build();
72     }
73
74     private static Optional<String> getSchemaFromRpc(final RemoteDeviceId id, final ContainerNode result) {
75         if (result == null) {
76             return Optional.empty();
77         }
78
79         final DataContainerChild child = result.childByArg(NETCONF_DATA_PATHARG);
80         checkState(child instanceof DOMSourceAnyxmlNode,
81                 "%s Unexpected response to get-schema, expected response with one child %s, but was %s", id,
82                 Data.QNAME, result);
83
84         final DOMSource wrappedNode = ((DOMSourceAnyxmlNode) child).body();
85         final Element dataNode = (Element) requireNonNull(wrappedNode.getNode());
86
87         return Optional.of(dataNode.getTextContent().trim());
88     }
89
90     @Override
91     public ListenableFuture<YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
92         final String moduleName = sourceIdentifier.name().getLocalName();
93         final Revision revision = sourceIdentifier.revision();
94         final ContainerNode getSchemaRequest = createGetSchemaRequest(moduleName,
95             revision == null ? Optional.empty() : Optional.of(revision.toString()));
96
97         LOG.trace("{}: Loading YANG schema source for {}:{}", id, moduleName, revision);
98         return Futures.transform(rpc.invokeNetconf(GetSchema.QNAME, getSchemaRequest),
99             result -> {
100                 // Transform composite node to string schema representation and then to ASTSchemaSource.
101                 if (result.errors().isEmpty()) {
102                     final String schemaString = getSchemaFromRpc(id, result.value())
103                         .orElseThrow(() -> new IllegalStateException(
104                             id + ": Unexpected response to get-schema, schema not present in message for: "
105                                 + sourceIdentifier));
106                     LOG.debug("{}: YANG Schema successfully retrieved for {}:{}", id, moduleName, revision);
107                     return new CachedYangTextSource(id, sourceIdentifier, schemaString, null);
108                 }
109
110                 LOG.warn("{}: YANG schema was not successfully retrieved for {}. Errors: {}", id, sourceIdentifier,
111                     result.errors());
112                 throw new IllegalStateException(String.format(
113                     "%s: YANG schema was not successfully retrieved for %s. Errors: %s", id, sourceIdentifier,
114                     result.errors()));
115             }, MoreExecutors.directExecutor());
116     }
117 }