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