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.impl.mapping.CapabilityProvider;
15 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
16 import org.opendaylight.controller.netconf.util.xml.XmlElement;
17 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 import java.util.HashMap;
27 public final class DefaultGetSchema extends AbstractLastNetconfOperation {
28 public static final String GET_SCHEMA = "get-schema";
29 public static final String IDENTIFIER = "identifier";
30 public static final String VERSION = "version";
32 private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
33 private final CapabilityProvider cap;
35 public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
36 super(netconfSessionIdForReporting);
41 protected String getOperationName() {
46 protected String getOperationNamespace() {
47 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
51 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
55 entry = new GetSchemaEntry(xml);
56 } catch (final IllegalArgumentException e) {
57 logger.warn("Error parsing xml", e);
58 final Map<String, String> errorInfo = new HashMap<>();
59 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
60 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
61 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
63 } catch (final IllegalStateException e) {
64 logger.warn("Error parsing xml", e);
65 final Map<String, String> errorInfo = new HashMap<>();
66 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
67 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
68 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
74 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
75 } catch (IllegalStateException e) {
76 Map<String, String> errorInfo = Maps.newHashMap();
77 errorInfo.put(entry.identifier, e.getMessage());
78 logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
79 throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
80 NetconfDocumentedException.ErrorTag.operation_failed,
81 NetconfDocumentedException.ErrorSeverity.error, errorInfo);
84 Element getSchemaResult;
85 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
86 XmlUtil.addNamespaceAttr(getSchemaResult,
87 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
89 logger.trace("{} operation successful", GET_SCHEMA);
91 return getSchemaResult;
94 private static final class GetSchemaEntry {
95 private final String identifier;
96 private final Optional<String> version;
98 GetSchemaEntry(XmlElement getSchemaElement) {
99 getSchemaElement.checkName(GET_SCHEMA);
100 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
102 XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
103 identifier = identifierElement.getTextContent();
104 Optional<XmlElement> versionElement = getSchemaElement
105 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
106 if (versionElement.isPresent()) {
107 version = Optional.of(versionElement.get().getTextContent());
109 version = Optional.absent();