2 * Copyright (c) 2014, 2015 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.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_DATA_NODEID;
14 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.toId;
16 import com.google.common.annotations.VisibleForTesting;
17 import com.google.common.base.Strings;
18 import com.google.common.collect.ImmutableSet;
19 import java.io.IOException;
20 import java.net.URISyntaxException;
21 import java.util.HashSet;
22 import java.util.Optional;
24 import java.util.concurrent.ExecutionException;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.transform.dom.DOMSource;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.netconf.api.NamespaceURN;
31 import org.opendaylight.netconf.api.xml.XmlUtil;
32 import org.opendaylight.netconf.client.mdsal.api.NetconfDeviceSchemas;
33 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
34 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
35 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
36 import org.opendaylight.netconf.common.mdsal.NormalizedDataUtil;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Get;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.GetInput;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.input.Filter;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.common.XMLNamespace;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
57 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61 import org.w3c.dom.Document;
62 import org.w3c.dom.Element;
63 import org.w3c.dom.Node;
64 import org.w3c.dom.traversal.DocumentTraversal;
65 import org.w3c.dom.traversal.NodeFilter;
66 import org.w3c.dom.traversal.TreeWalker;
67 import org.xml.sax.SAXException;
70 * Holds QNames for all YANG modules reported by ietf-netconf-monitoring/state/schemas.
72 public final class NetconfStateSchemas implements NetconfDeviceSchemas {
73 public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(ImmutableSet.of());
75 private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemas.class);
76 private static final YangInstanceIdentifier STATE_SCHEMAS_IDENTIFIER = YangInstanceIdentifier.builder()
77 .node(NetconfState.QNAME).node(Schemas.QNAME).build();
78 private static final String MONITORING_NAMESPACE = NetconfState.QNAME.getNamespace().toString();
79 private static final @NonNull ContainerNode GET_SCHEMAS_RPC;
82 final Document document = XmlUtil.newDocument();
84 final Element filterElem = document.createElementNS(NamespaceURN.BASE, "filter");
85 filterElem.setAttribute("type", "subtree");
87 final Element stateElem = document.createElementNS(NetconfState.QNAME.getNamespace().toString(),
88 NetconfState.QNAME.getLocalName());
89 stateElem.appendChild(document.createElementNS(Schemas.QNAME.getNamespace().toString(),
90 Schemas.QNAME.getLocalName()));
91 filterElem.appendChild(stateElem);
93 GET_SCHEMAS_RPC = ImmutableNodes.newContainerBuilder()
94 .withNodeIdentifier(new NodeIdentifier(GetInput.QNAME))
95 .withChild(ImmutableNodes.newAnyxmlBuilder(DOMSource.class)
96 .withNodeIdentifier(new NodeIdentifier(Filter.QNAME))
97 .withValue(new DOMSource(filterElem))
102 private final Set<RemoteYangSchema> availableYangSchemas;
104 public NetconfStateSchemas(final Set<RemoteYangSchema> availableYangSchemas) {
105 this.availableYangSchemas = availableYangSchemas;
108 public Set<RemoteYangSchema> getAvailableYangSchemas() {
109 return availableYangSchemas;
113 public Set<QName> getAvailableYangSchemasQNames() {
114 return availableYangSchemas.stream()
115 .map(RemoteYangSchema::getQName)
116 .collect(ImmutableSet.toImmutableSet());
120 * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas.
122 static NetconfStateSchemas create(final DOMRpcService deviceRpc,
123 final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id,
124 final EffectiveModelContext schemaContext) {
125 if (!remoteSessionCapabilities.isMonitoringSupported()) {
126 // TODO - need to search for get-schema support, not just ietf-netconf-monitoring support
127 // issue might be a deviation to ietf-netconf-monitoring where get-schema is unsupported...
128 LOG.warn("{}: Netconf monitoring not supported on device, cannot detect provided schemas", id);
132 final DOMRpcResult schemasNodeResult;
134 schemasNodeResult = deviceRpc.invokeRpc(Get.QNAME, GET_SCHEMAS_RPC).get();
135 } catch (final InterruptedException e) {
136 Thread.currentThread().interrupt();
137 throw new IllegalStateException(id
138 + ": Interrupted while waiting for response to " + STATE_SCHEMAS_IDENTIFIER, e);
139 } catch (final ExecutionException e) {
140 LOG.warn("{}: Unable to detect available schemas, get to {} failed", id, STATE_SCHEMAS_IDENTIFIER, e);
144 if (!schemasNodeResult.errors().isEmpty()) {
145 LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}",
146 id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.errors());
150 final Optional<? extends NormalizedNode> optSchemasNode = findSchemasNode(schemasNodeResult.value(),
152 if (optSchemasNode.isEmpty()) {
153 LOG.warn("{}: Unable to detect available schemas, get to {} was empty", id, STATE_SCHEMAS_IDENTIFIER);
157 final NormalizedNode schemasNode = optSchemasNode.orElseThrow();
158 checkState(schemasNode instanceof ContainerNode, "Expecting container containing schemas, but was %s",
160 return create(id, (ContainerNode) schemasNode);
164 * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas.
167 protected static NetconfStateSchemas create(final RemoteDeviceId id, final ContainerNode schemasNode) {
168 final Set<RemoteYangSchema> availableYangSchemas = new HashSet<>();
170 final DataContainerChild child = schemasNode.childByArg(toId(Schema.QNAME));
171 checkState(child != null, "Unable to find list: %s in response: %s", Schema.QNAME.withoutRevision(),
173 checkState(child instanceof MapNode,
174 "Unexpected structure for container: %s in response: %s. Expecting a list",
175 Schema.QNAME.withoutRevision(), schemasNode);
177 for (final MapEntryNode schemaNode : ((MapNode) child).body()) {
178 final Optional<RemoteYangSchema> fromCompositeNode =
179 RemoteYangSchema.createFromNormalizedNode(id, schemaNode);
180 fromCompositeNode.ifPresent(availableYangSchemas::add);
183 return new NetconfStateSchemas(availableYangSchemas);
186 private static Optional<? extends NormalizedNode> findSchemasNode(final NormalizedNode result,
187 final EffectiveModelContext schemaContext) {
188 if (result == null) {
189 return Optional.empty();
191 // FIXME: unchecked cast
192 final var rpcResult = ((ContainerNode) result).childByArg(NETCONF_DATA_NODEID);
193 if (rpcResult == null) {
194 return Optional.empty();
197 verify(rpcResult instanceof DOMSourceAnyxmlNode, "Unexpected result %s", rpcResult);
199 // Server may include additional data which we do not understand. Make sure we trim the input before we try
201 // FIXME: this is something NetconfUtil.transformDOMSourceToNormalizedNode(), and more generally, NormalizedNode
202 // codecs should handle. We really want to a NormalizedNode tree which can be directly queried for known
203 // things while completely ignoring XML content (and hence its semantics) of other elements.
204 final var filteredBody = ietfMonitoringCopy(((DOMSourceAnyxmlNode) rpcResult).body());
206 final NormalizedNode dataNode;
208 dataNode = NormalizedDataUtil.transformDOMSourceToNormalizedNode(schemaContext, filteredBody).getResult()
210 } catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
211 LOG.warn("Failed to transform {}", rpcResult, e);
212 return Optional.empty();
215 // FIXME: unchecked cast
216 final var nStateNode = ((DataContainerNode) dataNode).childByArg(toId(NetconfState.QNAME));
217 if (nStateNode == null) {
218 return Optional.empty();
221 // FIXME: unchecked cast
222 return ((DataContainerNode) nStateNode).findChildByArg(toId(Schemas.QNAME));
226 static DOMSource ietfMonitoringCopy(final DOMSource domSource) {
227 final var sourceDoc = XmlUtil.newDocument();
228 sourceDoc.appendChild(sourceDoc.importNode(domSource.getNode(), true));
230 final var treeWalker = ((DocumentTraversal) sourceDoc).createTreeWalker(sourceDoc.getDocumentElement(),
231 NodeFilter.SHOW_ALL, node -> {
232 final var namespace = node.getNamespaceURI();
233 return namespace == null || MONITORING_NAMESPACE.equals(namespace)
234 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
237 final var filteredDoc = XmlUtil.newDocument();
238 filteredDoc.appendChild(filteredDoc.importNode(treeWalker.getRoot(), false));
239 final var filteredElement = filteredDoc.getDocumentElement();
240 copyChildren(treeWalker, filteredDoc, filteredElement);
242 return new DOMSource(filteredElement);
245 private static void copyChildren(final TreeWalker walker, final Document targetDoc, final Node targetNode) {
246 if (walker.firstChild() != null) {
247 for (var node = walker.getCurrentNode(); node != null; node = walker.nextSibling()) {
248 final var importedNode = targetDoc.importNode(node, false);
249 targetNode.appendChild(importedNode);
250 copyChildren(walker, targetDoc, importedNode);
251 walker.setCurrentNode(node);
256 public static final class RemoteYangSchema {
257 private final QName qname;
259 RemoteYangSchema(final QName qname) {
263 public QName getQName() {
267 static Optional<RemoteYangSchema> createFromNormalizedNode(final RemoteDeviceId id,
268 final MapEntryNode schemaNode) {
269 final QName schemaNodeId = schemaNode.name().getNodeType();
270 checkArgument(schemaNodeId.equals(Schema.QNAME), "Wrong QName %s", schemaNodeId);
272 QName childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_FORMAT;
274 final String formatAsString = getSingleChildNodeValue(schemaNode, childNode).orElseThrow();
276 if (!formatAsString.equals(Yang.QNAME.toString())) {
277 LOG.debug("{}: Ignoring schema due to unsupported format: {}", id, formatAsString);
278 return Optional.empty();
281 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_LOCATION;
282 final Set<String> locationsAsString = getAllChildNodeValues(schemaNode, childNode);
283 if (!locationsAsString.contains(Schema.Location.Enumeration.NETCONF.toString())) {
284 LOG.debug("{}: Ignoring schema due to unsupported location: {}", id, locationsAsString);
285 return Optional.empty();
288 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE;
289 final Optional<String> namespaceValue = getSingleChildNodeValue(schemaNode, childNode);
290 if (namespaceValue.isEmpty()) {
291 LOG.warn("{}: Ignoring schema due to missing namespace", id);
292 return Optional.empty();
294 final String namespaceAsString = namespaceValue.orElseThrow();
296 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_VERSION;
297 // Revision does not have to be filled
298 final Optional<String> revisionAsString = getSingleChildNodeValue(schemaNode, childNode);
300 childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER;
301 final String moduleNameAsString = getSingleChildNodeValue(schemaNode, childNode).orElseThrow();
303 final QName moduleQName = revisionAsString.isPresent()
304 ? QName.create(namespaceAsString, revisionAsString.orElseThrow(), moduleNameAsString)
305 : QName.create(XMLNamespace.of(namespaceAsString), moduleNameAsString);
307 return Optional.of(new RemoteYangSchema(moduleQName));
311 * Extracts all values of a leaf-list node as a set of strings.
313 private static Set<String> getAllChildNodeValues(final DataContainerNode schemaNode,
314 final QName childNodeQName) {
315 final Set<String> extractedValues = new HashSet<>();
316 final DataContainerChild child = schemaNode.childByArg(toId(childNodeQName));
317 checkArgument(child != null, "Child nodes %s not present", childNodeQName);
318 checkArgument(child instanceof LeafSetNode, "Child nodes %s not present", childNodeQName);
319 for (final LeafSetEntryNode<?> childNode : ((LeafSetNode<?>) child).body()) {
320 extractedValues.add(getValueOfSimpleNode(childNode).orElseThrow());
322 return extractedValues;
325 private static Optional<String> getSingleChildNodeValue(final DataContainerNode schemaNode,
326 final QName childNode) {
327 final Optional<DataContainerChild> node = schemaNode.findChildByArg(toId(childNode));
328 if (node.isPresent()) {
329 return getValueOfSimpleNode(node.orElseThrow());
331 LOG.debug("Child node {} not present", childNode);
332 return Optional.empty();
335 private static Optional<String> getValueOfSimpleNode(final NormalizedNode node) {
336 final String valueStr = node.body().toString();
337 return Strings.isNullOrEmpty(valueStr) ? Optional.empty() : Optional.of(valueStr.trim());
341 public boolean equals(final Object obj) {
345 if (obj == null || getClass() != obj.getClass()) {
349 final RemoteYangSchema that = (RemoteYangSchema) obj;
351 return qname.equals(that.qname);
355 public int hashCode() {
356 return qname.hashCode();