2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.client.mdsal;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
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.mdsal.dom.api.DOMRpcService;
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;
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.
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);
56 private final DOMRpcService rpc;
57 private final RemoteDeviceId id;
59 public MonitoringSchemaSourceProvider(final RemoteDeviceId id, final DOMRpcService rpc) {
60 this.id = requireNonNull(id);
61 this.rpc = requireNonNull(rpc);
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();
74 private static Optional<String> getSchemaFromRpc(final RemoteDeviceId id, final ContainerNode result) {
76 return Optional.empty();
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,
84 final DOMSource wrappedNode = ((DOMSourceAnyxmlNode) child).body();
85 final Element dataNode = (Element) requireNonNull(wrappedNode.getNode());
87 return Optional.of(dataNode.getTextContent().trim());
91 public ListenableFuture<YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
92 final String moduleName = sourceIdentifier.name().getLocalName();
94 final Optional<String> revision = Optional.ofNullable(sourceIdentifier.revision()).map(Revision::toString);
95 final ContainerNode getSchemaRequest = createGetSchemaRequest(moduleName, revision);
96 LOG.trace("{}: Loading YANG schema source for {}:{}", id, moduleName, revision);
97 return Futures.transform(
98 rpc.invokeRpc(GetSchema.QNAME, getSchemaRequest), result -> {
99 // Transform composite node to string schema representation and then to ASTSchemaSource.
100 if (result.errors().isEmpty()) {
101 final String schemaString = getSchemaFromRpc(id, result.value())
102 .orElseThrow(() -> new IllegalStateException(
103 id + ": Unexpected response to get-schema, schema not present in message for: "
104 + sourceIdentifier));
105 LOG.debug("{}: YANG Schema successfully retrieved for {}:{}", id, moduleName, revision);
106 return new CachedYangTextSchemaSource(id, sourceIdentifier, moduleName, schemaString);
109 LOG.warn("{}: YANG schema was not successfully retrieved for {}. Errors: {}", id, sourceIdentifier,
111 throw new IllegalStateException(String.format(
112 "%s: YANG schema was not successfully retrieved for %s. Errors: %s", id, sourceIdentifier,
114 }, MoreExecutors.directExecutor());