Filter non-standard nodes from NETCONF monitoring schemas
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / NetconfStateSchemas.java
1 /*
2  * Copyright (c) 2014, 2015 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.sal.connect.netconf;
9
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.sal.connect.netconf.util.NetconfMessageTransformUtil.IETF_NETCONF_MONITORING;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_NODEID;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_NODEID;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_NODEID;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TYPE_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.base.Strings;
24 import com.google.common.collect.ImmutableSet;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
27 import java.util.HashSet;
28 import java.util.Optional;
29 import java.util.Set;
30 import java.util.concurrent.ExecutionException;
31 import javax.xml.stream.XMLStreamException;
32 import javax.xml.transform.dom.DOMSource;
33 import org.eclipse.jdt.annotation.NonNull;
34 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
35 import org.opendaylight.mdsal.dom.api.DOMRpcService;
36 import org.opendaylight.netconf.api.xml.XmlUtil;
37 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemas;
38 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
39 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
40 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
41 import org.opendaylight.netconf.util.NetconfUtil;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.common.XMLNamespace;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
59 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.w3c.dom.Document;
63 import org.w3c.dom.Element;
64 import org.w3c.dom.Node;
65 import org.w3c.dom.traversal.DocumentTraversal;
66 import org.w3c.dom.traversal.NodeFilter;
67 import org.w3c.dom.traversal.TreeWalker;
68 import org.xml.sax.SAXException;
69
70 /**
71  * Holds QNames for all yang modules reported by ietf-netconf-monitoring/state/schemas.
72  */
73 public final class NetconfStateSchemas implements NetconfDeviceSchemas {
74     public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(ImmutableSet.of());
75
76     private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemas.class);
77     private static final YangInstanceIdentifier STATE_SCHEMAS_IDENTIFIER = YangInstanceIdentifier.builder()
78         .node(NetconfState.QNAME).node(Schemas.QNAME).build();
79     private static final String MONITORING_NAMESPACE = IETF_NETCONF_MONITORING.getNamespace().toString();
80     private static final @NonNull ContainerNode GET_SCHEMAS_RPC;
81
82     static {
83         final Document document = XmlUtil.newDocument();
84
85         final Element filterElem = XmlUtil.createElement(document, NETCONF_FILTER_QNAME.getLocalName(),
86             Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString()));
87         filterElem.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(),
88             "subtree");
89
90         final Element stateElem = XmlUtil.createElement(document, NetconfState.QNAME.getLocalName(),
91             Optional.of(NetconfState.QNAME.getNamespace().toString()));
92         stateElem.appendChild(XmlUtil.createElement(document, Schemas.QNAME.getLocalName(),
93             Optional.of(Schemas.QNAME.getNamespace().toString())));
94         filterElem.appendChild(stateElem);
95
96         GET_SCHEMAS_RPC = Builders.containerBuilder()
97                 .withNodeIdentifier(NETCONF_GET_NODEID)
98                 .withChild(Builders.anyXmlBuilder()
99                     .withNodeIdentifier(NETCONF_FILTER_NODEID)
100                     .withValue(new DOMSource(filterElem))
101                     .build())
102                 .build();
103     }
104
105     private final Set<RemoteYangSchema> availableYangSchemas;
106
107     public NetconfStateSchemas(final Set<RemoteYangSchema> availableYangSchemas) {
108         this.availableYangSchemas = availableYangSchemas;
109     }
110
111     public Set<RemoteYangSchema> getAvailableYangSchemas() {
112         return availableYangSchemas;
113     }
114
115     @Override
116     public Set<QName> getAvailableYangSchemasQNames() {
117         return getAvailableYangSchemas().stream().map(RemoteYangSchema::getQName)
118                 .collect(ImmutableSet.toImmutableSet());
119     }
120
121     /**
122      * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas.
123      */
124     static NetconfStateSchemas create(final DOMRpcService deviceRpc,
125             final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id,
126             final EffectiveModelContext schemaContext) {
127         if (!remoteSessionCapabilities.isMonitoringSupported()) {
128             // TODO - need to search for get-schema support, not just ietf-netconf-monitoring support
129             // issue might be a deviation to ietf-netconf-monitoring where get-schema is unsupported...
130             LOG.warn("{}: Netconf monitoring not supported on device, cannot detect provided schemas", id);
131             return EMPTY;
132         }
133
134         final DOMRpcResult schemasNodeResult;
135         try {
136             schemasNodeResult = deviceRpc.invokeRpc(NETCONF_GET_QNAME, GET_SCHEMAS_RPC).get();
137         } catch (final InterruptedException e) {
138             Thread.currentThread().interrupt();
139             throw new IllegalStateException(id
140                     + ": Interrupted while waiting for response to " + STATE_SCHEMAS_IDENTIFIER, e);
141         } catch (final ExecutionException e) {
142             LOG.warn("{}: Unable to detect available schemas, get to {} failed", id, STATE_SCHEMAS_IDENTIFIER, e);
143             return EMPTY;
144         }
145
146         if (!schemasNodeResult.getErrors().isEmpty()) {
147             LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}",
148                     id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.getErrors());
149             return EMPTY;
150         }
151
152         final Optional<? extends NormalizedNode> optSchemasNode = findSchemasNode(schemasNodeResult.getResult(),
153                 schemaContext);
154         if (optSchemasNode.isEmpty()) {
155             LOG.warn("{}: Unable to detect available schemas, get to {} was empty", id, STATE_SCHEMAS_IDENTIFIER);
156             return EMPTY;
157         }
158
159         final NormalizedNode schemasNode = optSchemasNode.get();
160         checkState(schemasNode instanceof ContainerNode, "Expecting container containing schemas, but was %s",
161             schemasNode);
162         return create(id, (ContainerNode) schemasNode);
163     }
164
165     /**
166      * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas.
167      */
168     @VisibleForTesting
169     protected static NetconfStateSchemas create(final RemoteDeviceId id, final ContainerNode schemasNode) {
170         final Set<RemoteYangSchema> availableYangSchemas = new HashSet<>();
171
172         final Optional<DataContainerChild> child =
173                 schemasNode.findChildByArg(toId(Schema.QNAME));
174         checkState(child.isPresent(), "Unable to find list: %s in response: %s", Schema.QNAME.withoutRevision(),
175             schemasNode);
176         checkState(child.get() instanceof MapNode,
177                 "Unexpected structure for container: %s in response: %s. Expecting a list",
178                 Schema.QNAME.withoutRevision(), schemasNode);
179
180         for (final MapEntryNode schemaNode : ((MapNode) child.get()).body()) {
181             final Optional<RemoteYangSchema> fromCompositeNode =
182                     RemoteYangSchema.createFromNormalizedNode(id, schemaNode);
183             fromCompositeNode.ifPresent(availableYangSchemas::add);
184         }
185
186         return new NetconfStateSchemas(availableYangSchemas);
187     }
188
189     private static Optional<? extends NormalizedNode> findSchemasNode(final NormalizedNode result,
190             final EffectiveModelContext schemaContext) {
191         if (result == null) {
192             return Optional.empty();
193         }
194         // FIXME: unchecked cast
195         final var rpcResultOpt = ((ContainerNode) result).findChildByArg(NETCONF_DATA_NODEID);
196         if (rpcResultOpt.isEmpty()) {
197             return Optional.empty();
198         }
199
200         final var rpcResult = rpcResultOpt.get();
201         verify(rpcResult instanceof DOMSourceAnyxmlNode, "Unexpected result %s", rpcResult);
202
203         // Server may include additional data which we do not understand. Make sure we trim the input before we try
204         // to interpret it.
205         // FIXME: this is something NetconfUtil.transformDOMSourceToNormalizedNode(), and more generally, NormalizedNode
206         //        codecs should handle. We really want to a NormalizedNode tree which can be directly queried for known
207         //        things while completely ignoring XML content (and hence its semantics) of other elements.
208         final var filteredBody = ietfMonitoringCopy(((DOMSourceAnyxmlNode) rpcResult).body());
209
210         final NormalizedNode dataNode;
211         try {
212             dataNode = NetconfUtil.transformDOMSourceToNormalizedNode(schemaContext, filteredBody).getResult();
213         } catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
214             LOG.warn("Failed to transform {}", rpcResult, e);
215             return Optional.empty();
216         }
217
218         // FIXME: unchecked cast
219         final var nStateNode = ((DataContainerNode) dataNode).findChildByArg(toId(NetconfState.QNAME));
220         if (nStateNode.isEmpty()) {
221             return Optional.empty();
222         }
223
224         // FIXME: unchecked cast
225         return ((DataContainerNode) nStateNode.get()).findChildByArg(toId(Schemas.QNAME));
226     }
227
228     @VisibleForTesting
229     static DOMSource ietfMonitoringCopy(final DOMSource domSource) {
230         final var sourceDoc = XmlUtil.newDocument();
231         sourceDoc.appendChild(sourceDoc.importNode(domSource.getNode(), true));
232
233         final var treeWalker = ((DocumentTraversal) sourceDoc).createTreeWalker(sourceDoc.getDocumentElement(),
234             NodeFilter.SHOW_ALL, node -> {
235                 final var namespace = node.getNamespaceURI();
236                 return namespace == null || MONITORING_NAMESPACE.equals(namespace)
237                     ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
238             }, false);
239
240         final var filteredDoc = XmlUtil.newDocument();
241         filteredDoc.appendChild(filteredDoc.importNode(treeWalker.getRoot(), false));
242         final var filteredElement = filteredDoc.getDocumentElement();
243         copyChildren(treeWalker, filteredDoc, filteredElement);
244
245         return new DOMSource(filteredElement);
246     }
247
248     private static void copyChildren(final TreeWalker walker, final Document targetDoc, final Node targetNode) {
249         if (walker.firstChild() != null) {
250             for (var node = walker.getCurrentNode(); node != null; node = walker.nextSibling()) {
251                 final var importedNode = targetDoc.importNode(node, false);
252                 targetNode.appendChild(importedNode);
253                 copyChildren(walker, targetDoc, importedNode);
254                 walker.setCurrentNode(node);
255             }
256         }
257     }
258
259     public static final class RemoteYangSchema {
260         private final QName qname;
261
262         RemoteYangSchema(final QName qname) {
263             this.qname = qname;
264         }
265
266         public QName getQName() {
267             return qname;
268         }
269
270         static Optional<RemoteYangSchema> createFromNormalizedNode(final RemoteDeviceId id,
271                                                                    final MapEntryNode schemaNode) {
272             final QName schemaNodeId = schemaNode.getIdentifier().getNodeType();
273             checkArgument(schemaNodeId.equals(Schema.QNAME), "Wrong QName %s", schemaNodeId);
274
275             QName childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_FORMAT;
276
277             final String formatAsString = getSingleChildNodeValue(schemaNode, childNode).get();
278
279             if (!formatAsString.equals(Yang.QNAME.toString())) {
280                 LOG.debug("{}: Ignoring schema due to unsupported format: {}", id, formatAsString);
281                 return Optional.empty();
282             }
283
284             childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_LOCATION;
285             final Set<String> locationsAsString = getAllChildNodeValues(schemaNode, childNode);
286             if (!locationsAsString.contains(Schema.Location.Enumeration.NETCONF.toString())) {
287                 LOG.debug("{}: Ignoring schema due to unsupported location: {}", id, locationsAsString);
288                 return Optional.empty();
289             }
290
291             childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE;
292             final Optional<String> namespaceValue = getSingleChildNodeValue(schemaNode, childNode);
293             if (namespaceValue.isEmpty()) {
294                 LOG.warn("{}: Ignoring schema due to missing namespace", id);
295                 return Optional.empty();
296             }
297             final String namespaceAsString = namespaceValue.get();
298
299             childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_VERSION;
300             // Revision does not have to be filled
301             final Optional<String> revisionAsString = getSingleChildNodeValue(schemaNode, childNode);
302
303             childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER;
304             final String moduleNameAsString = getSingleChildNodeValue(schemaNode, childNode).get();
305
306             final QName moduleQName = revisionAsString.isPresent()
307                     ? QName.create(namespaceAsString, revisionAsString.get(), moduleNameAsString)
308                     : QName.create(XMLNamespace.of(namespaceAsString), moduleNameAsString);
309
310             return Optional.of(new RemoteYangSchema(moduleQName));
311         }
312
313         /**
314          * Extracts all values of a leaf-list node as a set of strings.
315          */
316         private static Set<String> getAllChildNodeValues(final DataContainerNode schemaNode,
317                                                          final QName childNodeQName) {
318             final Set<String> extractedValues = new HashSet<>();
319             final Optional<DataContainerChild> child = schemaNode.findChildByArg(toId(childNodeQName));
320             checkArgument(child.isPresent(), "Child nodes %s not present", childNodeQName);
321             checkArgument(child.get() instanceof LeafSetNode<?>, "Child nodes %s not present", childNodeQName);
322             for (final LeafSetEntryNode<?> childNode : ((LeafSetNode<?>) child.get()).body()) {
323                 extractedValues.add(getValueOfSimpleNode(childNode).get());
324             }
325             return extractedValues;
326         }
327
328         private static Optional<String> getSingleChildNodeValue(final DataContainerNode schemaNode,
329                                                                 final QName childNode) {
330             final Optional<DataContainerChild> node = schemaNode.findChildByArg(toId(childNode));
331             if (node.isPresent()) {
332                 return getValueOfSimpleNode(node.get());
333             }
334             LOG.debug("Child node {} not present", childNode);
335             return Optional.empty();
336         }
337
338         private static Optional<String> getValueOfSimpleNode(final NormalizedNode node) {
339             final String valueStr = node.body().toString();
340             return Strings.isNullOrEmpty(valueStr) ? Optional.empty() : Optional.of(valueStr.trim());
341         }
342
343         @Override
344         public boolean equals(final Object obj) {
345             if (this == obj) {
346                 return true;
347             }
348             if (obj == null || getClass() != obj.getClass()) {
349                 return false;
350             }
351
352             final RemoteYangSchema that = (RemoteYangSchema) obj;
353
354             return qname.equals(that.qname);
355         }
356
357         @Override
358         public int hashCode() {
359             return qname.hashCode();
360         }
361     }
362 }