Resolve Bug:448 - Remove yang-store api and impl.
[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.mapping.AbstractLastNetconfOperation;
16 import org.opendaylight.controller.netconf.util.xml.XmlElement;
17 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 import java.util.HashMap;
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         try {
55             entry = new GetSchemaEntry(xml);
56         } catch (final IllegalArgumentException e) {
57             logger.warn("Error parsing xml", e);
58             final Map<String, String> errorInfo = new HashMap<>();
59             errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
60             throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
61                     NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
62                     errorInfo);
63         } catch (final IllegalStateException e) {
64             logger.warn("Error parsing xml", e);
65             final Map<String, String> errorInfo = new HashMap<>();
66             errorInfo.put(NetconfDocumentedException.ErrorTag.bad_attribute.name(), e.getMessage());
67             throw new NetconfDocumentedException(e.getMessage(), e, NetconfDocumentedException.ErrorType.rpc,
68                     NetconfDocumentedException.ErrorTag.bad_attribute, NetconfDocumentedException.ErrorSeverity.error,
69                     errorInfo);
70         }
71
72         String schema;
73         try {
74             schema = cap.getSchemaForCapability(entry.identifier, entry.version);
75         } catch (IllegalStateException e) {
76             Map<String, String> errorInfo = Maps.newHashMap();
77             errorInfo.put(entry.identifier, e.getMessage());
78             logger.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
79             throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
80                     NetconfDocumentedException.ErrorTag.operation_failed,
81                     NetconfDocumentedException.ErrorSeverity.error, errorInfo);
82         }
83
84         Element getSchemaResult;
85         getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema);
86         XmlUtil.addNamespaceAttr(getSchemaResult,
87                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
88
89         logger.trace("{} operation successful", GET_SCHEMA);
90
91         return getSchemaResult;
92     }
93
94     private static final class GetSchemaEntry {
95         private final String identifier;
96         private final Optional<String> version;
97
98         GetSchemaEntry(XmlElement getSchemaElement) {
99             getSchemaElement.checkName(GET_SCHEMA);
100             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
101
102             XmlElement identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
103             identifier = identifierElement.getTextContent();
104             Optional<XmlElement> versionElement = getSchemaElement
105                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
106             if (versionElement.isPresent()) {
107                 version = Optional.of(versionElement.get().getTextContent());
108             } else {
109                 version = Optional.absent();
110             }
111         }
112     }
113 }