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