2 * Copyright (c) 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.server.mdsal.monitoring;
10 import static java.util.Objects.requireNonNull;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.xml.XmlElement;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
21 import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
23 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
24 import org.opendaylight.yangtools.yang.common.ErrorTag;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
31 public final class GetSchema extends AbstractSingletonNetconfOperation {
32 private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
33 public static final String URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING =
34 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring";
36 private static final String GET_SCHEMA = "get-schema";
37 private static final String IDENTIFIER = "identifier";
38 private static final String VERSION = "version";
40 private final NetconfMonitoringService monitoring;
42 public GetSchema(final SessionIdType sessionId, final NetconfMonitoringService monitoring) {
44 this.monitoring = requireNonNull(monitoring);
48 protected String getOperationName() {
53 protected String getOperationNamespace() {
54 return URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
58 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml)
59 throws DocumentedException {
60 final var entry = new GetSchemaEntry(xml);
64 schema = monitoring.getSchemaForCapability(entry.identifier, Optional.ofNullable(entry.version));
65 } catch (final IllegalStateException e) {
66 LOG.warn("Rpc error: {}", ErrorTag.OPERATION_FAILED, e);
67 throw new DocumentedException(e.getMessage(), e,
68 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
69 // FIXME: so we have an <operation-failed>e.getMessage()</operation-failed> ??? In which namespace? Why?
70 Map.of(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage()));
73 final var getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
74 Optional.of(URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
75 LOG.trace("{} operation successful", GET_SCHEMA);
76 return getSchemaResult;
79 private static final class GetSchemaEntry {
80 private final @NonNull String identifier;
81 private final @Nullable String version;
83 GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
84 getSchemaElement.checkName(GET_SCHEMA);
85 getSchemaElement.checkNamespace(URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
87 final XmlElement identifierElement;
89 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
90 } catch (final DocumentedException e) {
91 LOG.trace("Can't get identifier element as only child element with same namespace due to ", e);
92 throw DocumentedException.wrap(e);
94 identifier = identifierElement.getTextContent();
95 final var versionElement = getSchemaElement.getOnlyChildElementWithSameNamespaceOptionally(VERSION);
96 if (versionElement.isPresent()) {
97 version = versionElement.orElseThrow().getTextContent();