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.api.NetconfSession;
17 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
18 import org.opendaylight.controller.netconf.mapping.api.DefaultNetconfOperation;
19 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
29 import com.google.common.base.Optional;
30 import com.google.common.collect.Maps;
32 public final class DefaultGetSchema extends AbstractNetconfOperation implements DefaultNetconfOperation {
34 private final CapabilityProvider cap;
35 private NetconfSession netconfSession;
37 private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
39 public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
40 super(netconfSessionIdForReporting);
44 public static final String GET_SCHEMA = "get-schema";
45 public static final String IDENTIFIER = "identifier";
46 public static final String VERSION = "version";
49 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
50 if (netconfOperationName.equals("get-schema") == false)
51 return HandlingPriority.CANNOT_HANDLE;
52 if (namespace.equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING) == false)
53 return HandlingPriority.CANNOT_HANDLE;
55 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
59 protected Element handle(Document document, XmlElement xml, NetconfOperationRouter router)
60 throws NetconfDocumentedException {
64 entry = new GetSchemaEntry(xml);
65 } catch (final IllegalArgumentException e) {
66 logger.warn("Error parsing xml", e);
67 final Map<String, String> errorInfo = new HashMap<>();
68 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
69 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
70 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
72 } catch (final IllegalStateException e) {
73 logger.warn("Error parsing xml", e);
74 final Map<String, String> errorInfo = new HashMap<>();
75 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
76 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
77 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
83 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
84 } catch (IllegalStateException e) {
85 Map<String, String> errorInfo = Maps.newHashMap();
86 errorInfo.put(entry.identifier, e.getMessage());
87 logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
88 throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
89 NetconfDocumentedException.ErrorTag.operation_failed,
90 NetconfDocumentedException.ErrorSeverity.error, errorInfo);
93 Element getSchemaResult;
94 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
95 XmlUtil.addNamespaceAttr(getSchemaResult,
96 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
98 logger.info("{} operation successful", GET_SCHEMA);
100 return getSchemaResult;
103 private static final class GetSchemaEntry {
104 private final String identifier;
105 private final Optional<String> version;
107 GetSchemaEntry(XmlElement getSchemaElement) {
108 getSchemaElement.checkName(GET_SCHEMA);
109 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
111 XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
112 identifier = identifierElement.getTextContent();
113 Optional<XmlElement> versionElement = getSchemaElement
114 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
115 if (versionElement.isPresent()) {
116 version = Optional.of(versionElement.get().getTextContent());
118 version = Optional.absent();
124 public void setNetconfSession(NetconfSession s) {
125 this.netconfSession = s;
128 public NetconfSession getNetconfSession() {
129 return netconfSession;