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.monitoring;
10 import java.util.HashMap;
12 import java.util.Optional;
13 import org.opendaylight.netconf.api.DocumentedException;
14 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
15 import org.opendaylight.netconf.api.xml.XmlElement;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.api.xml.XmlUtil;
18 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 public class GetSchema extends AbstractSingletonNetconfOperation {
25 public static final String GET_SCHEMA = "get-schema";
26 public static final String IDENTIFIER = "identifier";
27 public static final String VERSION = "version";
29 private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
30 private final NetconfMonitoringService cap;
32 public GetSchema(final NetconfMonitoringService cap) {
33 super(MonitoringConstants.MODULE_NAME);
38 protected String getOperationName() {
43 protected String getOperationNamespace() {
44 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
48 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml)
49 throws DocumentedException {
50 final GetSchemaEntry entry;
52 entry = new GetSchemaEntry(xml);
56 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
57 } catch (final IllegalStateException e) {
58 final Map<String, String> errorInfo = new HashMap<>();
59 errorInfo.put(DocumentedException.ErrorTag.OPERATION_FAILED.toString(), e.getMessage());
60 LOG.warn("Rpc error: {}", DocumentedException.ErrorTag.OPERATION_FAILED, e);
61 throw new DocumentedException(e.getMessage(), e, DocumentedException.ErrorType.APPLICATION,
62 DocumentedException.ErrorTag.OPERATION_FAILED,
63 DocumentedException.ErrorSeverity.ERROR, errorInfo);
66 final Element getSchemaResult;
67 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
68 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
69 LOG.trace("{} operation successful", GET_SCHEMA);
71 return getSchemaResult;
74 private static final class GetSchemaEntry {
75 private final String identifier;
76 private final Optional<String> version;
78 GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
79 getSchemaElement.checkName(GET_SCHEMA);
80 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
82 XmlElement identifierElement = null;
84 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
85 } catch (final DocumentedException e) {
86 LOG.trace("Can't get identifier element as only child element with same namespace due to ", e);
87 throw DocumentedException.wrap(e);
89 identifier = identifierElement.getTextContent();
90 final Optional<XmlElement> versionElement = getSchemaElement
91 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
92 if (versionElement.isPresent()) {
93 version = Optional.of(versionElement.get().getTextContent());
95 version = Optional.empty();