X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fconnect%2Fnetconf%2FNetconfStateSchemas.java;h=3061b8d17052d4a19ec0dbe2d2cd083f583302e9;hb=c3d40c5e4485e95b9307fcdff1833ca1568f9321;hp=d9ca7df3e923572d8acb8aed014450fba92f0163;hpb=1e1e99672bf3450a165b3ee0be5d38e856586da4;p=netconf.git diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfStateSchemas.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfStateSchemas.java index d9ca7df3e9..3061b8d170 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfStateSchemas.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfStateSchemas.java @@ -5,39 +5,49 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netconf.sal.connect.netconf; -import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Verify.verify; +import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.IETF_NETCONF_MONITORING; +import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_NODEID; +import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_NODEID; +import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME; +import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_NODEID; import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME; +import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TYPE_QNAME; import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId; -import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import com.google.common.collect.Collections2; -import com.google.common.collect.Sets; -import java.net.URI; -import java.util.Collections; +import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.HashSet; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutionException; -import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; -import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.dom.DOMSource; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.dom.api.DOMRpcResult; +import org.opendaylight.mdsal.dom.api.DOMRpcService; +import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemas; import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences; -import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema; import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil; import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId; +import org.opendaylight.netconf.util.NetconfUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; @@ -46,27 +56,50 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.traversal.DocumentTraversal; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.TreeWalker; +import org.xml.sax.SAXException; /** - * Holds QNames for all yang modules reported by ietf-netconf-monitoring/state/schemas + * Holds QNames for all yang modules reported by ietf-netconf-monitoring/state/schemas. */ public final class NetconfStateSchemas implements NetconfDeviceSchemas { + public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(ImmutableSet.of()); private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemas.class); + private static final YangInstanceIdentifier STATE_SCHEMAS_IDENTIFIER = YangInstanceIdentifier.builder() + .node(NetconfState.QNAME).node(Schemas.QNAME).build(); + private static final String MONITORING_NAMESPACE = IETF_NETCONF_MONITORING.getNamespace().toString(); + private static final @NonNull ContainerNode GET_SCHEMAS_RPC; - public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(Collections.emptySet()); - - private static final YangInstanceIdentifier STATE_SCHEMAS_IDENTIFIER = - YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).build(); - - private static final ContainerNode GET_SCHEMAS_RPC; static { - final DataContainerChild filter = NetconfMessageTransformUtil.toFilterStructure(STATE_SCHEMAS_IDENTIFIER, - BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()); - GET_SCHEMAS_RPC - = Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_GET_QNAME)).withChild(filter).build(); + final Document document = XmlUtil.newDocument(); + + final Element filterElem = XmlUtil.createElement(document, NETCONF_FILTER_QNAME.getLocalName(), + Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString())); + filterElem.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(), + "subtree"); + + final Element stateElem = XmlUtil.createElement(document, NetconfState.QNAME.getLocalName(), + Optional.of(NetconfState.QNAME.getNamespace().toString())); + stateElem.appendChild(XmlUtil.createElement(document, Schemas.QNAME.getLocalName(), + Optional.of(Schemas.QNAME.getNamespace().toString()))); + filterElem.appendChild(stateElem); + + GET_SCHEMAS_RPC = Builders.containerBuilder() + .withNodeIdentifier(NETCONF_GET_NODEID) + .withChild(Builders.anyXmlBuilder() + .withNodeIdentifier(NETCONF_FILTER_NODEID) + .withValue(new DOMSource(filterElem)) + .build()) + .build(); } private final Set availableYangSchemas; @@ -81,19 +114,17 @@ public final class NetconfStateSchemas implements NetconfDeviceSchemas { @Override public Set getAvailableYangSchemasQNames() { - return Sets.newHashSet(Collections2.transform(getAvailableYangSchemas(), new Function() { - @Override - public QName apply(final RemoteYangSchema input) { - return input.getQName(); - } - })); + return getAvailableYangSchemas().stream().map(RemoteYangSchema::getQName) + .collect(ImmutableSet.toImmutableSet()); } /** - * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas + * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas. */ - static NetconfStateSchemas create(final DOMRpcService deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) { - if(remoteSessionCapabilities.isMonitoringSupported() == false) { + static NetconfStateSchemas create(final DOMRpcService deviceRpc, + final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id, + final EffectiveModelContext schemaContext) { + if (!remoteSessionCapabilities.isMonitoringSupported()) { // TODO - need to search for get-schema support, not just ietf-netconf-monitoring support // issue might be a deviation to ietf-netconf-monitoring where get-schema is unsupported... LOG.warn("{}: Netconf monitoring not supported on device, cannot detect provided schemas", id); @@ -102,72 +133,130 @@ public final class NetconfStateSchemas implements NetconfDeviceSchemas { final DOMRpcResult schemasNodeResult; try { - schemasNodeResult = deviceRpc.invokeRpc(toPath(NETCONF_GET_QNAME), GET_SCHEMAS_RPC).get(); + schemasNodeResult = deviceRpc.invokeRpc(NETCONF_GET_QNAME, GET_SCHEMAS_RPC).get(); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException(id + ": Interrupted while waiting for response to " + STATE_SCHEMAS_IDENTIFIER, e); + throw new IllegalStateException(id + + ": Interrupted while waiting for response to " + STATE_SCHEMAS_IDENTIFIER, e); } catch (final ExecutionException e) { LOG.warn("{}: Unable to detect available schemas, get to {} failed", id, STATE_SCHEMAS_IDENTIFIER, e); return EMPTY; } - if(schemasNodeResult.getErrors().isEmpty() == false) { - LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}", id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.getErrors()); + if (!schemasNodeResult.getErrors().isEmpty()) { + LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}", + id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.getErrors()); return EMPTY; } - final Optional> schemasNode = findSchemasNode(schemasNodeResult.getResult()); - - if(schemasNode.isPresent()) { - Preconditions.checkState(schemasNode.get() instanceof ContainerNode, - "Expecting container containing schemas, but was %s", schemasNode.get()); - return create(id, ((ContainerNode) schemasNode.get())); - } else { + final Optional optSchemasNode = findSchemasNode(schemasNodeResult.getResult(), + schemaContext); + if (optSchemasNode.isEmpty()) { LOG.warn("{}: Unable to detect available schemas, get to {} was empty", id, STATE_SCHEMAS_IDENTIFIER); return EMPTY; } + + final NormalizedNode schemasNode = optSchemasNode.get(); + checkState(schemasNode instanceof ContainerNode, "Expecting container containing schemas, but was %s", + schemasNode); + return create(id, (ContainerNode) schemasNode); + } + + /** + * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas. + */ + @VisibleForTesting + protected static NetconfStateSchemas create(final RemoteDeviceId id, final ContainerNode schemasNode) { + final Set availableYangSchemas = new HashSet<>(); + + final Optional child = + schemasNode.findChildByArg(toId(Schema.QNAME)); + checkState(child.isPresent(), "Unable to find list: %s in response: %s", Schema.QNAME.withoutRevision(), + schemasNode); + checkState(child.get() instanceof MapNode, + "Unexpected structure for container: %s in response: %s. Expecting a list", + Schema.QNAME.withoutRevision(), schemasNode); + + for (final MapEntryNode schemaNode : ((MapNode) child.get()).body()) { + final Optional fromCompositeNode = + RemoteYangSchema.createFromNormalizedNode(id, schemaNode); + fromCompositeNode.ifPresent(availableYangSchemas::add); + } + + return new NetconfStateSchemas(availableYangSchemas); } - private static Optional> findSchemasNode(final NormalizedNode result) { - if(result == null) { - return Optional.absent(); + private static Optional findSchemasNode(final NormalizedNode result, + final EffectiveModelContext schemaContext) { + if (result == null) { + return Optional.empty(); + } + // FIXME: unchecked cast + final var rpcResultOpt = ((ContainerNode) result).findChildByArg(NETCONF_DATA_NODEID); + if (rpcResultOpt.isEmpty()) { + return Optional.empty(); } - final Optional> dataNode = ((DataContainerNode) result).getChild(toId(NETCONF_DATA_QNAME)); - if(dataNode.isPresent() == false) { - return Optional.absent(); + + final var rpcResult = rpcResultOpt.get(); + verify(rpcResult instanceof DOMSourceAnyxmlNode, "Unexpected result %s", rpcResult); + + // Server may include additional data which we do not understand. Make sure we trim the input before we try + // to interpret it. + // FIXME: this is something NetconfUtil.transformDOMSourceToNormalizedNode(), and more generally, NormalizedNode + // codecs should handle. We really want to a NormalizedNode tree which can be directly queried for known + // things while completely ignoring XML content (and hence its semantics) of other elements. + final var filteredBody = ietfMonitoringCopy(((DOMSourceAnyxmlNode) rpcResult).body()); + + final NormalizedNode dataNode; + try { + dataNode = NetconfUtil.transformDOMSourceToNormalizedNode(schemaContext, filteredBody).getResult(); + } catch (XMLStreamException | URISyntaxException | IOException | SAXException e) { + LOG.warn("Failed to transform {}", rpcResult, e); + return Optional.empty(); } - final Optional> nStateNode = - ((DataContainerNode) dataNode.get()).getChild(toId(NetconfState.QNAME)); - if(nStateNode.isPresent() == false) { - return Optional.absent(); + // FIXME: unchecked cast + final var nStateNode = ((DataContainerNode) dataNode).findChildByArg(toId(NetconfState.QNAME)); + if (nStateNode.isEmpty()) { + return Optional.empty(); } - return ((DataContainerNode) nStateNode.get()).getChild(toId(Schemas.QNAME)); + // FIXME: unchecked cast + return ((DataContainerNode) nStateNode.get()).findChildByArg(toId(Schemas.QNAME)); } - /** - * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas - */ @VisibleForTesting - protected static NetconfStateSchemas create(final RemoteDeviceId id, final ContainerNode schemasNode) { - final Set availableYangSchemas = Sets.newHashSet(); - - final Optional> child = schemasNode.getChild(toId(Schema.QNAME)); - Preconditions.checkState(child.isPresent(), "Unable to find list: %s in response: %s", Schema.QNAME.withoutRevision(), schemasNode); - Preconditions.checkState(child.get() instanceof MapNode, "Unexpected structure for container: %s in response: %s. Expecting a list", Schema.QNAME.withoutRevision(), schemasNode); + static DOMSource ietfMonitoringCopy(final DOMSource domSource) { + final var sourceDoc = XmlUtil.newDocument(); + sourceDoc.appendChild(sourceDoc.importNode(domSource.getNode(), true)); + + final var treeWalker = ((DocumentTraversal) sourceDoc).createTreeWalker(sourceDoc.getDocumentElement(), + NodeFilter.SHOW_ALL, node -> { + final var namespace = node.getNamespaceURI(); + return namespace == null || MONITORING_NAMESPACE.equals(namespace) + ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; + }, false); + + final var filteredDoc = XmlUtil.newDocument(); + filteredDoc.appendChild(filteredDoc.importNode(treeWalker.getRoot(), false)); + final var filteredElement = filteredDoc.getDocumentElement(); + copyChildren(treeWalker, filteredDoc, filteredElement); + + return new DOMSource(filteredElement); + } - for (final MapEntryNode schemaNode : ((MapNode) child.get()).getValue()) { - final Optional fromCompositeNode = RemoteYangSchema.createFromNormalizedNode(id, schemaNode); - if(fromCompositeNode.isPresent()) { - availableYangSchemas.add(fromCompositeNode.get()); + private static void copyChildren(final TreeWalker walker, final Document targetDoc, final Node targetNode) { + if (walker.firstChild() != null) { + for (var node = walker.getCurrentNode(); node != null; node = walker.nextSibling()) { + final var importedNode = targetDoc.importNode(node, false); + targetNode.appendChild(importedNode); + copyChildren(walker, targetDoc, importedNode); + walker.setCurrentNode(node); } } - - return new NetconfStateSchemas(availableYangSchemas); } - public final static class RemoteYangSchema { + public static final class RemoteYangSchema { private final QName qname; RemoteYangSchema(final QName qname) { @@ -178,27 +267,34 @@ public final class NetconfStateSchemas implements NetconfDeviceSchemas { return qname; } - static Optional createFromNormalizedNode(final RemoteDeviceId id, final MapEntryNode schemaNode) { - Preconditions.checkArgument(schemaNode.getNodeType().equals(Schema.QNAME), "Wrong QName %s", schemaNode.getNodeType()); + static Optional createFromNormalizedNode(final RemoteDeviceId id, + final MapEntryNode schemaNode) { + final QName schemaNodeId = schemaNode.getIdentifier().getNodeType(); + checkArgument(schemaNodeId.equals(Schema.QNAME), "Wrong QName %s", schemaNodeId); QName childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_FORMAT; final String formatAsString = getSingleChildNodeValue(schemaNode, childNode).get(); - if(formatAsString.equals(Yang.QNAME.toString()) == false) { + if (!formatAsString.equals(Yang.QNAME.toString())) { LOG.debug("{}: Ignoring schema due to unsupported format: {}", id, formatAsString); - return Optional.absent(); + return Optional.empty(); } childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_LOCATION; final Set locationsAsString = getAllChildNodeValues(schemaNode, childNode); - if(locationsAsString.contains(Schema.Location.Enumeration.NETCONF.toString()) == false) { + if (!locationsAsString.contains(Schema.Location.Enumeration.NETCONF.toString())) { LOG.debug("{}: Ignoring schema due to unsupported location: {}", id, locationsAsString); - return Optional.absent(); + return Optional.empty(); } childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE; - final String namespaceAsString = getSingleChildNodeValue(schemaNode, childNode).get(); + final Optional namespaceValue = getSingleChildNodeValue(schemaNode, childNode); + if (namespaceValue.isEmpty()) { + LOG.warn("{}: Ignoring schema due to missing namespace", id); + return Optional.empty(); + } + final String namespaceAsString = namespaceValue.get(); childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_VERSION; // Revision does not have to be filled @@ -209,52 +305,53 @@ public final class NetconfStateSchemas implements NetconfDeviceSchemas { final QName moduleQName = revisionAsString.isPresent() ? QName.create(namespaceAsString, revisionAsString.get(), moduleNameAsString) - : QName.create(URI.create(namespaceAsString), null, moduleNameAsString); + : QName.create(XMLNamespace.of(namespaceAsString), moduleNameAsString); return Optional.of(new RemoteYangSchema(moduleQName)); } /** - * Extracts all values of a leaf-list node as a set of strings + * Extracts all values of a leaf-list node as a set of strings. */ - private static Set getAllChildNodeValues(final DataContainerNode schemaNode, final QName childNodeQName) { - final Set extractedValues = Sets.newHashSet(); - final Optional> child = schemaNode.getChild(toId(childNodeQName)); - Preconditions.checkArgument(child.isPresent(), "Child nodes %s not present", childNodeQName); - Preconditions.checkArgument(child.get() instanceof LeafSetNode, "Child nodes %s not present", childNodeQName); - for (final LeafSetEntryNode childNode : ((LeafSetNode) child.get()).getValue()) { + private static Set getAllChildNodeValues(final DataContainerNode schemaNode, + final QName childNodeQName) { + final Set extractedValues = new HashSet<>(); + final Optional child = schemaNode.findChildByArg(toId(childNodeQName)); + checkArgument(child.isPresent(), "Child nodes %s not present", childNodeQName); + checkArgument(child.get() instanceof LeafSetNode, "Child nodes %s not present", childNodeQName); + for (final LeafSetEntryNode childNode : ((LeafSetNode) child.get()).body()) { extractedValues.add(getValueOfSimpleNode(childNode).get()); } return extractedValues; } - private static Optional getSingleChildNodeValue(final DataContainerNode schemaNode, final QName childNode) { - final Optional> node = schemaNode.getChild(toId(childNode)); - Preconditions.checkArgument(node.isPresent(), "Child node %s not present", childNode); - return getValueOfSimpleNode(node.get()); + private static Optional getSingleChildNodeValue(final DataContainerNode schemaNode, + final QName childNode) { + final Optional node = schemaNode.findChildByArg(toId(childNode)); + if (node.isPresent()) { + return getValueOfSimpleNode(node.get()); + } + LOG.debug("Child node {} not present", childNode); + return Optional.empty(); } - private static Optional getValueOfSimpleNode(final NormalizedNode node) { - final Object value = node.getValue(); - return value == null || Strings.isNullOrEmpty(value.toString()) ? Optional.absent() : Optional.of(value.toString().trim()); + private static Optional getValueOfSimpleNode(final NormalizedNode node) { + final String valueStr = node.body().toString(); + return Strings.isNullOrEmpty(valueStr) ? Optional.empty() : Optional.of(valueStr.trim()); } @Override - public boolean equals(final Object o) { - if (this == o) { + public boolean equals(final Object obj) { + if (this == obj) { return true; } - if (o == null || getClass() != o.getClass()) { + if (obj == null || getClass() != obj.getClass()) { return false; } - final RemoteYangSchema that = (RemoteYangSchema) o; - - if (!qname.equals(that.qname)) { - return false; - } + final RemoteYangSchema that = (RemoteYangSchema) obj; - return true; + return qname.equals(that.qname); } @Override