BUG 2743 - Added support for runtime RPC's to netconf mdsal northbound.
[controller.git] / opendaylight / netconf / netconf-monitoring / src / main / java / org / opendaylight / controller / netconf / 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.netconf.monitoring;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Maps;
13 import java.util.Map;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
16 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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 public class GetSchema extends AbstractSingletonNetconfOperation {
27     public static final String GET_SCHEMA = "get-schema";
28     public static final String IDENTIFIER = "identifier";
29     public static final String VERSION = "version";
30
31     private static final Logger LOG = LoggerFactory.getLogger(GetSchema.class);
32     private final NetconfMonitoringService cap;
33
34     public GetSchema(final NetconfMonitoringService cap) {
35         super(MonitoringConstants.MODULE_NAME);
36         this.cap = cap;
37     }
38
39     @Override
40     protected String getOperationName() {
41         return GET_SCHEMA;
42     }
43
44     @Override
45     protected String getOperationNamespace() {
46         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING;
47     }
48
49     @Override
50     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml) throws NetconfDocumentedException {
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 = Maps.newHashMap();
60             errorInfo.put(entry.identifier, e.getMessage());
61             LOG.warn("Rpc error: {}", NetconfDocumentedException.ErrorTag.operation_failed, e);
62             throw new NetconfDocumentedException(e.getMessage(), NetconfDocumentedException.ErrorType.application,
63                     NetconfDocumentedException.ErrorTag.operation_failed,
64                     NetconfDocumentedException.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 NetconfDocumentedException {
80             getSchemaElement.checkName(GET_SCHEMA);
81             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
82
83             XmlElement identifierElement = null;
84             try {
85                 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
86             } catch (final MissingNameSpaceException e) {
87                 LOG.trace("Can't get identifier element as only child element with same namespace due to ",e);
88                 throw NetconfDocumentedException.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.absent();
97             }
98         }
99     }
100 }