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.controller.config.yang.netconf.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 private static final String GET_SCHEMA = "get-schema";
34 private static final String IDENTIFIER = "identifier";
35 private static final String VERSION = "version";
37 private final NetconfMonitoringService monitoring;
39 public GetSchema(final SessionIdType sessionId, final NetconfMonitoringService monitoring) {
41 this.monitoring = requireNonNull(monitoring);
45 protected String getOperationName() {
50 protected String getOperationNamespace() {
51 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
55 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml)
56 throws DocumentedException {
57 final var entry = new GetSchemaEntry(xml);
61 schema = monitoring.getSchemaForCapability(entry.identifier, Optional.ofNullable(entry.version));
62 } catch (final IllegalStateException e) {
63 LOG.warn("Rpc error: {}", ErrorTag.OPERATION_FAILED, e);
64 throw new DocumentedException(e.getMessage(), e,
65 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
66 // FIXME: so we have an <operation-failed>e.getMessage()</operation-failed> ??? In which namespace? Why?
67 Map.of(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage()));
70 final var getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
71 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
72 LOG.trace("{} operation successful", GET_SCHEMA);
73 return getSchemaResult;
76 private static final class GetSchemaEntry {
77 private final @NonNull String identifier;
78 private final @Nullable String version;
80 GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
81 getSchemaElement.checkName(GET_SCHEMA);
82 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
84 final XmlElement identifierElement;
86 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
87 } catch (final DocumentedException e) {
88 LOG.trace("Can't get identifier element as only child element with same namespace due to ", e);
89 throw DocumentedException.wrap(e);
91 identifier = identifierElement.getTextContent();
92 final var versionElement = getSchemaElement.getOnlyChildElementWithSameNamespaceOptionally(VERSION);
93 if (versionElement.isPresent()) {
94 version = versionElement.orElseThrow().getTextContent();