Merge "Migrate NetconfHelloMessage's use of Optional"
[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
9 package org.opendaylight.controller.config.yang.netconf.mdsal.monitoring;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Optional;
14 import org.opendaylight.netconf.api.DocumentedException;
15 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
16 import org.opendaylight.netconf.api.xml.XmlElement;
17 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 public final class GetSchema extends AbstractSingletonNetconfOperation {
26     private static final String GET_SCHEMA = "get-schema";
27     private static final String IDENTIFIER = "identifier";
28     private static final String VERSION = "version";
29
30     private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
31     private final NetconfMonitoringService cap;
32
33     public GetSchema(final String netconfSessionIdForReporting, final NetconfMonitoringService cap) {
34         super(netconfSessionIdForReporting);
35         this.cap = cap;
36     }
37
38     @Override
39     protected String getOperationName() {
40         return GET_SCHEMA;
41     }
42
43     @Override
44     protected String getOperationNamespace() {
45         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
46     }
47
48     @Override
49     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml)
50             throws DocumentedException {
51         final GetSchemaEntry entry;
52
53         entry = new GetSchemaEntry(xml);
54
55         final String schema;
56         try {
57             schema = cap.getSchemaForCapability(entry.identifier, entry.version);
58         } catch (final IllegalStateException e) {
59             final Map<String, String> errorInfo = new HashMap<>();
60             errorInfo.put(DocumentedException.ErrorTag.OPERATION_FAILED.toString(), e.getMessage());
61             LOG.warn("Rpc error: {}", DocumentedException.ErrorTag.OPERATION_FAILED, e);
62             throw new DocumentedException(e.getMessage(), e, DocumentedException.ErrorType.APPLICATION,
63                     DocumentedException.ErrorTag.OPERATION_FAILED,
64                     DocumentedException.ErrorSeverity.ERROR, errorInfo);
65         }
66
67         final Element getSchemaResult;
68         getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
69                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
70         LOG.trace("{} operation successful", GET_SCHEMA);
71
72         return getSchemaResult;
73     }
74
75     private static final class GetSchemaEntry {
76         private final String identifier;
77         private final Optional<String> version;
78
79         GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
80             getSchemaElement.checkName(GET_SCHEMA);
81             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
82
83             final XmlElement identifierElement;
84             try {
85                 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
86             } catch (final DocumentedException e) {
87                 LOG.trace("Can't get identifier element as only child element with same namespace due to ", e);
88                 throw DocumentedException.wrap(e);
89             }
90             identifier = identifierElement.getTextContent();
91             final Optional<XmlElement> versionElement = getSchemaElement
92                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
93             if (versionElement.isPresent()) {
94                 version = Optional.of(versionElement.get().getTextContent());
95             } else {
96                 version = Optional.empty();
97             }
98         }
99     }
100 }