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 java.util.HashMap;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
16 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
17 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Maps;
30 public final class DefaultGetSchema extends AbstractNetconfOperation {
31 public static final String GET_SCHEMA = "get-schema";
32 public static final String IDENTIFIER = "identifier";
33 public static final String VERSION = "version";
35 private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
36 private final CapabilityProvider cap;
38 public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
39 super(netconfSessionIdForReporting);
44 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
45 if (netconfOperationName.equals("get-schema") == false)
46 return HandlingPriority.CANNOT_HANDLE;
47 if (namespace.equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING) == false)
48 return HandlingPriority.CANNOT_HANDLE;
50 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
54 protected Element handle(Document document, XmlElement xml, NetconfOperationRouter router)
55 throws NetconfDocumentedException {
59 entry = new GetSchemaEntry(xml);
60 } catch (final IllegalArgumentException e) {
61 logger.warn("Error parsing xml", e);
62 final Map<String, String> errorInfo = new HashMap<>();
63 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
64 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
65 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
67 } catch (final IllegalStateException e) {
68 logger.warn("Error parsing xml", e);
69 final Map<String, String> errorInfo = new HashMap<>();
70 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
71 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
72 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
78 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
79 } catch (IllegalStateException e) {
80 Map<String, String> errorInfo = Maps.newHashMap();
81 errorInfo.put(entry.identifier, e.getMessage());
82 logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
83 throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
84 NetconfDocumentedException.ErrorTag.operation_failed,
85 NetconfDocumentedException.ErrorSeverity.error, errorInfo);
88 Element getSchemaResult;
89 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
90 XmlUtil.addNamespaceAttr(getSchemaResult,
91 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
93 logger.trace("{} operation successful", GET_SCHEMA);
95 return getSchemaResult;
98 private static final class GetSchemaEntry {
99 private final String identifier;
100 private final Optional<String> version;
102 GetSchemaEntry(XmlElement getSchemaElement) {
103 getSchemaElement.checkName(GET_SCHEMA);
104 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
106 XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
107 identifier = identifierElement.getTextContent();
108 Optional<XmlElement> versionElement = getSchemaElement
109 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
110 if (versionElement.isPresent()) {
111 version = Optional.of(versionElement.get().getTextContent());
113 version = Optional.absent();