Convert mdsal-netconf-monitoring to OSGi DS
[netconf.git] / netconf / mdsal-netconf-monitoring / src / main / java / org / opendaylight / controller / config / yang / netconf / mdsal / monitoring / GetSchema.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.config.yang.netconf.mdsal.monitoring;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Optional;
13 import org.opendaylight.netconf.api.DocumentedException;
14 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
15 import org.opendaylight.netconf.api.xml.XmlElement;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.api.xml.XmlUtil;
18 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
19 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.common.ErrorType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 public final class GetSchema extends AbstractSingletonNetconfOperation {
28     private static final String GET_SCHEMA = "get-schema";
29     private static final String IDENTIFIER = "identifier";
30     private static final String VERSION = "version";
31
32     private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
33     private final NetconfMonitoringService cap;
34
35     public GetSchema(final String netconfSessionIdForReporting, final NetconfMonitoringService cap) {
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(final Document document, final XmlElement xml)
52             throws DocumentedException {
53         final GetSchemaEntry entry;
54
55         entry = new GetSchemaEntry(xml);
56
57         final String schema;
58         try {
59             schema = cap.getSchemaForCapability(entry.identifier, entry.version);
60         } catch (final IllegalStateException e) {
61             final Map<String, String> errorInfo = new HashMap<>();
62             // FIXME: so we have an <operation-failed>e.getMessage()</operation-failed> ??? In which namespace? Why?
63             errorInfo.put(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage());
64             LOG.warn("Rpc error: {}", ErrorTag.OPERATION_FAILED, e);
65             throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION,
66                     ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR, errorInfo);
67         }
68
69         final Element getSchemaResult;
70         getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
71                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
72         LOG.trace("{} operation successful", GET_SCHEMA);
73
74         return getSchemaResult;
75     }
76
77     private static final class GetSchemaEntry {
78         private final String identifier;
79         private final Optional<String> version;
80
81         GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
82             getSchemaElement.checkName(GET_SCHEMA);
83             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
84
85             final XmlElement identifierElement;
86             try {
87                 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
88             } catch (final DocumentedException e) {
89                 LOG.trace("Can't get identifier element as only child element with same namespace due to ", e);
90                 throw DocumentedException.wrap(e);
91             }
92             identifier = identifierElement.getTextContent();
93             final Optional<XmlElement> versionElement = getSchemaElement
94                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
95             if (versionElement.isPresent()) {
96                 version = Optional.of(versionElement.get().getTextContent());
97             } else {
98                 version = Optional.empty();
99             }
100         }
101     }
102 }