904f3f613e43549134eafcf7fc733591f32f4a2f
[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.api.NetconfOperationRouter;
16 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
17 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Maps;
29
30 public final class DefaultGetSchema extends AbstractNetconfOperation {
31     public static final String GET_SCHEMA = "get-schema";
32     public static final String IDENTIFIER = "identifier";
33     public static final String VERSION = "version";
34
35     private static final Logger logger = LoggerFactory.getLogger(DefaultGetSchema.class);
36     private final CapabilityProvider cap;
37
38     public DefaultGetSchema(CapabilityProvider cap, String netconfSessionIdForReporting) {
39         super(netconfSessionIdForReporting);
40         this.cap = cap;
41     }
42
43     @Override
44     protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
45         if (netconfOperationName.equals("get-schema") == false)
46             return HandlingPriority.CANNOT_HANDLE;
47         if (namespace.equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING) == false)
48             return HandlingPriority.CANNOT_HANDLE;
49
50         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
51     }
52
53     @Override
54     protected Element handle(Document document, XmlElement xml, NetconfOperationRouter router)
55             throws NetconfDocumentedException {
56         GetSchemaEntry entry;
57
58         try {
59             entry = new GetSchemaEntry(xml);
60         } catch (final IllegalArgumentException e) {
61             logger.warn("Error parsing xml", e);
62             final Map<String, String> errorInfo = new HashMap<>();
63             errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
64             throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
65                     NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
66                     errorInfo);
67         } catch (final IllegalStateException e) {
68             logger.warn("Error parsing xml", e);
69             final Map<String, String> errorInfo = new HashMap<>();
70             errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
71             throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
72                     NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
73                     errorInfo);
74         }
75
76         String schema;
77         try {
78             schema = cap.getSchemaForCapability(entry.identifier, entry.version);
79         } catch (IllegalStateException e) {
80             Map<String, String> errorInfo = Maps.newHashMap();
81             errorInfo.put(entry.identifier, e.getMessage());
82             logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
83             throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
84                     NetconfDocumentedException.ErrorTag.operation_failed,
85                     NetconfDocumentedException.ErrorSeverity.error, errorInfo);
86         }
87
88         Element getSchemaResult;
89         getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
90         XmlUtil.addNamespaceAttr(getSchemaResult,
91                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
92
93         logger.trace("{} operation successful", GET_SCHEMA);
94
95         return getSchemaResult;
96     }
97
98     private static final class GetSchemaEntry {
99         private final String identifier;
100         private final Optional<String> version;
101
102         GetSchemaEntry(XmlElement getSchemaElement) {
103             getSchemaElement.checkName(GET_SCHEMA);
104             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
105
106             XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
107             identifier = identifierElement.getTextContent();
108             Optional<XmlElement> versionElement = getSchemaElement
109                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
110             if (versionElement.isPresent()) {
111                 version = Optional.of(versionElement.get().getTextContent());
112             } else {
113                 version = Optional.absent();
114             }
115         }
116     }
117 }