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;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
17 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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;
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 {
55 entry = new GetSchemaEntry(xml);
59 schema = cap.getSchemaForCapability(entry.identifier, entry.version);
60 } catch (IllegalStateException e) {
61 Map<String, String> errorInfo = Maps.newHashMap();
62 errorInfo.put(entry.identifier, e.getMessage());
63 logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
64 throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
65 NetconfDocumentedException.ErrorTag.operation_failed,
66 NetconfDocumentedException.ErrorSeverity.error, errorInfo);
69 Element getSchemaResult;
70 getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
71 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
72 logger.trace("{} operation successful", GET_SCHEMA);
74 return getSchemaResult;
77 private static final class GetSchemaEntry {
78 private final String identifier;
79 private final Optional<String> version;
81 GetSchemaEntry(XmlElement getSchemaElement) throws NetconfDocumentedException {
82 getSchemaElement.checkName(GET_SCHEMA);
83 getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
85 XmlElement identifierElement = null;
87 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
88 } catch (MissingNameSpaceException e) {
89 logger.trace("Can't get identifier element as only child element with same namespace due to {}",e);
90 throw NetconfDocumentedException.wrap(e);
92 identifier = identifierElement.getTextContent();
93 Optional<XmlElement> versionElement = getSchemaElement
94 .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
95 if (versionElement.isPresent()) {
96 version = Optional.of(versionElement.get().getTextContent());
98 version = Optional.absent();