Merge "Bug 615: Removed xtend from Topology Manager"
[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 java.util.HashMap;
12 import java.util.Map;
13
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;
24
25 import com.google.common.base.Optional;
26 import com.google.common.collect.Maps;
27
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";
32
33     private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
34     private final CapabilityProvider cap;
35
36     public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
37         super(netconfSessionIdForReporting);
38         this.cap = cap;
39     }
40
41     @Override
42     protected String getOperationName() {
43         return GET_SCHEMA;
44     }
45
46     @Override
47     protected String getOperationNamespace() {
48         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
49     }
50
51     @Override
52     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
53         GetSchemaEntry entry;
54
55         try {
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,
63                     errorInfo);
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,
70                     errorInfo);
71         }
72
73         String schema;
74         try {
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);
83         }
84
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);
89
90         return getSchemaResult;
91     }
92
93     private static final class GetSchemaEntry {
94         private final String identifier;
95         private final Optional<String> version;
96
97         GetSchemaEntry(XmlElement getSchemaElement) {
98             getSchemaElement.checkName(GET_SCHEMA);
99             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
100
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());
107             } else {
108                 version = Optional.absent();
109             }
110         }
111     }
112 }