beceb8d008cb6e6b61662cf16f6ea32bd31981ed
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultGetSchema.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.impl.mapping.operations;
10
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.exception.MissingNameSpaceException;
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;
24
25 import java.util.Map;
26
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";
31
32     private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
33     private final CapabilityProvider cap;
34
35     public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
36         super(netconfSessionIdForReporting);
37         this.cap = cap;
38     }
39
40     @Override
41     protected String getOperationName() {
42         return GET_SCHEMA;
43     }
44
45     @Override
46     protected String getOperationNamespace() {
47         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
48     }
49
50     @Override
51     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
52         GetSchemaEntry entry;
53
54         entry = new GetSchemaEntry(xml);
55
56         String schema;
57         try {
58             schema = cap.getSchemaForCapability(entry.identifier, entry.version);
59         } catch (IllegalStateException e) {
60             Map<String, String> errorInfo = Maps.newHashMap();
61             errorInfo.put(entry.identifier, e.getMessage());
62             logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
63             throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
64                     NetconfDocumentedException.ErrorTag.operation_failed,
65                     NetconfDocumentedException.ErrorSeverity.error, errorInfo);
66         }
67
68         Element getSchemaResult;
69         getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
70                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
71         logger.trace("{} operation successful", GET_SCHEMA);
72
73         return getSchemaResult;
74     }
75
76     private static final class GetSchemaEntry {
77         private final String identifier;
78         private final Optional<String> version;
79
80         GetSchemaEntry(XmlElement getSchemaElement) throws NetconfDocumentedException {
81             getSchemaElement.checkName(GET_SCHEMA);
82             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
83
84             XmlElement identifierElement = null;
85             try {
86                 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
87             } catch (MissingNameSpaceException e) {
88                 logger.trace("Can't get identifier element as only child element with same namespace due to {}",e);
89                 throw NetconfDocumentedException.wrap(e);
90             }
91             identifier = identifierElement.getTextContent();
92             Optional<XmlElement> versionElement = getSchemaElement
93                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
94             if (versionElement.isPresent()) {
95                 version = Optional.of(versionElement.get().getTextContent());
96             } else {
97                 version = Optional.absent();
98             }
99         }
100     }
101 }