2 * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.impl.mapping.operations;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
15 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
16 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
17 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
26 import java.util.HashMap;
29 public final class DefaultGetSchema extends AbstractNetconfOperation {
31 private final CapabilityProvider cap;
33 private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
35 public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
36 super(netconfSessionIdForReporting);
40 public static final String GET_SCHEMA = "get-schema";
41 public static final String IDENTIFIER = "identifier";
42 public static final String VERSION = "version";
45 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
46 if (netconfOperationName.equals("get-schema") == false)
47 return HandlingPriority.CANNOT_HANDLE;
48 if (namespace.equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING) == false)
49 return HandlingPriority.CANNOT_HANDLE;
51 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
55 protected Element handle(Document document, XmlElement xml, NetconfOperationRouter router)
56 throws NetconfDocumentedException {
60 entry = new GetSchemaEntry(xml);
61 } catch (final IllegalArgumentException e) {
62 logger.warn("Error parsing xml", e);
63 final Map<String, String> errorInfo = new HashMap<>();
64 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
65 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
66 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
68 } catch (final IllegalStateException e) {
69 logger.warn("Error parsing xml", e);
70 final Map<String, String> errorInfo = new HashMap<>();
71 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
72 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
73 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
79 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
80 } catch (IllegalStateException e) {
81 Map<String, String> errorInfo = Maps.newHashMap();
82 errorInfo.put(entry.identifier, e.getMessage());
83 logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
84 throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
85 NetconfDocumentedException.ErrorTag.operation_failed,
86 NetconfDocumentedException.ErrorSeverity.error, errorInfo);
89 Element getSchemaResult;
90 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
91 XmlUtil.addNamespaceAttr(getSchemaResult,
92 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
94 logger.info("{} operation successful", GET_SCHEMA);
96 return getSchemaResult;
99 private static final class GetSchemaEntry {
100 private final String identifier;
101 private final Optional<String> version;
103 GetSchemaEntry(XmlElement getSchemaElement) {
104 getSchemaElement.checkName(GET_SCHEMA);
105 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
107 XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
108 identifier = identifierElement.getTextContent();
109 Optional<XmlElement> versionElement = getSchemaElement
110 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
111 if (versionElement.isPresent()) {
112 version = Optional.of(versionElement.get().getTextContent());
114 version = Optional.absent();