97703a79c1a4e0bc855f3e838ca5e958355cc888
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / 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.client.mdsal;
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.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_DATA_NODEID;
14 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.toId;
15
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;
23 import java.util.Set;
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;
68
69 /**
70  * Holds QNames for all YANG modules reported by ietf-netconf-monitoring/state/schemas.
71  */
72 public final class NetconfStateSchemas implements NetconfDeviceSchemas {
73     public static final NetconfStateSchemas EMPTY = new NetconfStateSchemas(ImmutableSet.of());
74
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;
80
81     static {
82         final Document document = XmlUtil.newDocument();
83
84         final Element filterElem = document.createElementNS(NamespaceURN.BASE, "filter");
85         filterElem.setAttribute("type", "subtree");
86
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);
92
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))
98                 .build())
99             .build();
100     }
101
102     private final Set<RemoteYangSchema> availableYangSchemas;
103
104     public NetconfStateSchemas(final Set<RemoteYangSchema> availableYangSchemas) {
105         this.availableYangSchemas = availableYangSchemas;
106     }
107
108     public Set<RemoteYangSchema> getAvailableYangSchemas() {
109         return availableYangSchemas;
110     }
111
112     @Override
113     public Set<QName> getAvailableYangSchemasQNames() {
114         return availableYangSchemas.stream()
115             .map(RemoteYangSchema::getQName)
116             .collect(ImmutableSet.toImmutableSet());
117     }
118
119     /**
120      * Issue get request to remote device and parse response to find all schemas under netconf-state/schemas.
121      */
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);
129             return EMPTY;
130         }
131
132         final DOMRpcResult schemasNodeResult;
133         try {
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);
141             return EMPTY;
142         }
143
144         if (!schemasNodeResult.errors().isEmpty()) {
145             LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}",
146                     id, STATE_SCHEMAS_IDENTIFIER, schemasNodeResult.errors());
147             return EMPTY;
148         }
149
150         final Optional<? extends NormalizedNode> optSchemasNode = findSchemasNode(schemasNodeResult.value(),
151                 schemaContext);
152         if (optSchemasNode.isEmpty()) {
153             LOG.warn("{}: Unable to detect available schemas, get to {} was empty", id, STATE_SCHEMAS_IDENTIFIER);
154             return EMPTY;
155         }
156
157         final NormalizedNode schemasNode = optSchemasNode.orElseThrow();
158         checkState(schemasNode instanceof ContainerNode, "Expecting container containing schemas, but was %s",
159             schemasNode);
160         return create(id, (ContainerNode) schemasNode);
161     }
162
163     /**
164      * Parse response of get(netconf-state/schemas) to find all schemas under netconf-state/schemas.
165      */
166     @VisibleForTesting
167     protected static NetconfStateSchemas create(final RemoteDeviceId id, final ContainerNode schemasNode) {
168         final Set<RemoteYangSchema> availableYangSchemas = new HashSet<>();
169
170         final DataContainerChild child = schemasNode.childByArg(toId(Schema.QNAME));
171         checkState(child != null, "Unable to find list: %s in response: %s", Schema.QNAME.withoutRevision(),
172             schemasNode);
173         checkState(child instanceof MapNode,
174                 "Unexpected structure for container: %s in response: %s. Expecting a list",
175                 Schema.QNAME.withoutRevision(), schemasNode);
176
177         for (final MapEntryNode schemaNode : ((MapNode) child).body()) {
178             final Optional<RemoteYangSchema> fromCompositeNode =
179                     RemoteYangSchema.createFromNormalizedNode(id, schemaNode);
180             fromCompositeNode.ifPresent(availableYangSchemas::add);
181         }
182
183         return new NetconfStateSchemas(availableYangSchemas);
184     }
185
186     private static Optional<? extends NormalizedNode> findSchemasNode(final NormalizedNode result,
187             final EffectiveModelContext schemaContext) {
188         if (result == null) {
189             return Optional.empty();
190         }
191         // FIXME: unchecked cast
192         final var rpcResult = ((ContainerNode) result).childByArg(NETCONF_DATA_NODEID);
193         if (rpcResult == null) {
194             return Optional.empty();
195         }
196
197         verify(rpcResult instanceof DOMSourceAnyxmlNode, "Unexpected result %s", rpcResult);
198
199         // Server may include additional data which we do not understand. Make sure we trim the input before we try
200         // to interpret it.
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());
205
206         final NormalizedNode dataNode;
207         try {
208             dataNode = NormalizedDataUtil.transformDOMSourceToNormalizedNode(schemaContext, filteredBody).getResult()
209                 .data();
210         } catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
211             LOG.warn("Failed to transform {}", rpcResult, e);
212             return Optional.empty();
213         }
214
215         // FIXME: unchecked cast
216         final var nStateNode = ((DataContainerNode) dataNode).childByArg(toId(NetconfState.QNAME));
217         if (nStateNode == null) {
218             return Optional.empty();
219         }
220
221         // FIXME: unchecked cast
222         return ((DataContainerNode) nStateNode).findChildByArg(toId(Schemas.QNAME));
223     }
224
225     @VisibleForTesting
226     static DOMSource ietfMonitoringCopy(final DOMSource domSource) {
227         final var sourceDoc = XmlUtil.newDocument();
228         sourceDoc.appendChild(sourceDoc.importNode(domSource.getNode(), true));
229
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;
235             }, false);
236
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);
241
242         return new DOMSource(filteredElement);
243     }
244
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);
252             }
253         }
254     }
255
256     public static final class RemoteYangSchema {
257         private final QName qname;
258
259         RemoteYangSchema(final QName qname) {
260             this.qname = qname;
261         }
262
263         public QName getQName() {
264             return qname;
265         }
266
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);
271
272             QName childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_FORMAT;
273
274             final String formatAsString = getSingleChildNodeValue(schemaNode, childNode).orElseThrow();
275
276             if (!formatAsString.equals(Yang.QNAME.toString())) {
277                 LOG.debug("{}: Ignoring schema due to unsupported format: {}", id, formatAsString);
278                 return Optional.empty();
279             }
280
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();
286             }
287
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();
293             }
294             final String namespaceAsString = namespaceValue.orElseThrow();
295
296             childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_VERSION;
297             // Revision does not have to be filled
298             final Optional<String> revisionAsString = getSingleChildNodeValue(schemaNode, childNode);
299
300             childNode = NetconfMessageTransformUtil.IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER;
301             final String moduleNameAsString = getSingleChildNodeValue(schemaNode, childNode).orElseThrow();
302
303             final QName moduleQName = revisionAsString.isPresent()
304                     ? QName.create(namespaceAsString, revisionAsString.orElseThrow(), moduleNameAsString)
305                     : QName.create(XMLNamespace.of(namespaceAsString), moduleNameAsString);
306
307             return Optional.of(new RemoteYangSchema(moduleQName));
308         }
309
310         /**
311          * Extracts all values of a leaf-list node as a set of strings.
312          */
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());
321             }
322             return extractedValues;
323         }
324
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());
330             }
331             LOG.debug("Child node {} not present", childNode);
332             return Optional.empty();
333         }
334
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());
338         }
339
340         @Override
341         public boolean equals(final Object obj) {
342             if (this == obj) {
343                 return true;
344             }
345             if (obj == null || getClass() != obj.getClass()) {
346                 return false;
347             }
348
349             final RemoteYangSchema that = (RemoteYangSchema) obj;
350
351             return qname.equals(that.qname);
352         }
353
354         @Override
355         public int hashCode() {
356             return qname.hashCode();
357         }
358     }
359 }