Integrate netconf-mapping-api into netconf-server
[netconf.git] / netconf / mdsal-netconf-monitoring / src / main / java / org / opendaylight / controller / config / yang / netconf / mdsal / monitoring / GetSchema.java
1 /*
2  * Copyright (c) 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.controller.config.yang.netconf.mdsal.monitoring;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Map;
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.monitoring.NetconfMonitoringService;
18 import org.opendaylight.netconf.api.xml.XmlElement;
19 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.netconf.api.xml.XmlUtil;
21 import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
22 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29
30 public final class GetSchema extends AbstractSingletonNetconfOperation {
31     private static final String GET_SCHEMA = "get-schema";
32     private static final String IDENTIFIER = "identifier";
33     private static final String VERSION = "version";
34
35     private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
36     private final NetconfMonitoringService monitoring;
37
38     public GetSchema(final String netconfSessionIdForReporting, final NetconfMonitoringService monitoring) {
39         super(netconfSessionIdForReporting);
40         this.monitoring = requireNonNull(monitoring);
41     }
42
43     @Override
44     protected String getOperationName() {
45         return GET_SCHEMA;
46     }
47
48     @Override
49     protected String getOperationNamespace() {
50         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
51     }
52
53     @Override
54     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml)
55             throws DocumentedException {
56         final var entry = new GetSchemaEntry(xml);
57
58         final String schema;
59         try {
60             schema = monitoring.getSchemaForCapability(entry.identifier, Optional.ofNullable(entry.version));
61         } catch (final IllegalStateException e) {
62             LOG.warn("Rpc error: {}", ErrorTag.OPERATION_FAILED, e);
63             throw new DocumentedException(e.getMessage(), e,
64                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
65                 // FIXME: so we have an <operation-failed>e.getMessage()</operation-failed> ??? In which namespace? Why?
66                 Map.of(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage()));
67         }
68
69         final var getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
70                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
71         LOG.trace("{} operation successful", GET_SCHEMA);
72         return getSchemaResult;
73     }
74
75     private static final class GetSchemaEntry {
76         private final @NonNull String identifier;
77         private final @Nullable String version;
78
79         GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
80             getSchemaElement.checkName(GET_SCHEMA);
81             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
82
83             final XmlElement identifierElement;
84             try {
85                 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
86             } catch (final DocumentedException e) {
87                 LOG.trace("Can't get identifier element as only child element with same namespace due to ", e);
88                 throw DocumentedException.wrap(e);
89             }
90             identifier = identifierElement.getTextContent();
91             final var versionElement = getSchemaElement.getOnlyChildElementWithSameNamespaceOptionally(VERSION);
92             if (versionElement.isPresent()) {
93                 version = versionElement.orElseThrow().getTextContent();
94             } else {
95                 version = null;
96             }
97         }
98     }
99 }