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.impl.mapping.CapabilityProvider;
16 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.Maps;
28 public final class DefaultGetSchema extends AbstractLastNetconfOperation {
29 public static final String GET_SCHEMA = "get-schema";
30 public static final String IDENTIFIER = "identifier";
31 public static final String VERSION = "version";
33 private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
34 private final CapabilityProvider cap;
36 public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
37 super(netconfSessionIdForReporting);
42 protected String getOperationName() {
47 protected String getOperationNamespace() {
48 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
52 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
56 entry = new GetSchemaEntry(xml);
57 } catch (final IllegalArgumentException e) {
58 logger.warn("Error parsing xml", e);
59 final Map<String, String> errorInfo = new HashMap<>();
60 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
61 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
62 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
64 } catch (final IllegalStateException e) {
65 logger.warn("Error parsing xml", e);
66 final Map<String, String> errorInfo = new HashMap<>();
67 errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
68 throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
69 NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
75 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
76 } catch (IllegalStateException e) {
77 Map<String, String> errorInfo = Maps.newHashMap();
78 errorInfo.put(entry.identifier, e.getMessage());
79 logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
80 throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
81 NetconfDocumentedException.ErrorTag.operation_failed,
82 NetconfDocumentedException.ErrorSeverity.error, errorInfo);
85 Element getSchemaResult;
86 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
87 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
88 logger.trace("{} operation successful", GET_SCHEMA);
90 return getSchemaResult;
93 private static final class GetSchemaEntry {
94 private final String identifier;
95 private final Optional<String> version;
97 GetSchemaEntry(XmlElement getSchemaElement) {
98 getSchemaElement.checkName(GET_SCHEMA);
99 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
101 XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
102 identifier = identifierElement.getTextContent();
103 Optional<XmlElement> versionElement = getSchemaElement
104 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
105 if (versionElement.isPresent()) {
106 version = Optional.of(versionElement.get().getTextContent());
108 version = Optional.absent();