Remove netconf from commons/opendaylight pom
[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.config.util.xml.DocumentedException;
15 import org.opendaylight.controller.config.util.xml.XmlElement;
16 import org.opendaylight.controller.config.util.xml.XmlUtil;
17 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
18 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.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 class GetSchema extends AbstractSingletonNetconfOperation {
26     public static final String GET_SCHEMA = "get-schema";
27     public static final String IDENTIFIER = "identifier";
28     public 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 NetconfMonitoringService cap) {
34         super(MonitoringConstants.MODULE_NAME);
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) throws DocumentedException {
50         final GetSchemaEntry entry;
51
52         entry = new GetSchemaEntry(xml);
53
54         final String schema;
55         try {
56             schema = cap.getSchemaForCapability(entry.identifier, entry.version);
57         } catch (final IllegalStateException e) {
58             final Map<String, String> errorInfo = Maps.newHashMap();
59             errorInfo.put(entry.identifier, e.getMessage());
60             LOG.warn("Rpc error: {}", DocumentedException.ErrorTag.operation_failed, e);
61             throw new DocumentedException(e.getMessage(), DocumentedException.ErrorType.application,
62                     DocumentedException.ErrorTag.operation_failed,
63                     DocumentedException.ErrorSeverity.error, errorInfo);
64         }
65
66         final Element getSchemaResult;
67         getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
68                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
69         LOG.trace("{} operation successful", GET_SCHEMA);
70
71         return getSchemaResult;
72     }
73
74     private static final class GetSchemaEntry {
75         private final String identifier;
76         private final Optional<String> version;
77
78         GetSchemaEntry(final XmlElement getSchemaElement) throws DocumentedException {
79             getSchemaElement.checkName(GET_SCHEMA);
80             getSchemaElement.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING);
81
82             XmlElement identifierElement = null;
83             try {
84                 identifierElement = getSchemaElement.getOnlyChildElementWithSameNamespace(IDENTIFIER);
85             } catch (final DocumentedException e) {
86                 LOG.trace("Can't get identifier element as only child element with same namespace due to ",e);
87                 throw DocumentedException.wrap(e);
88             }
89             identifier = identifierElement.getTextContent();
90             final Optional<XmlElement> versionElement = getSchemaElement
91                     .getOnlyChildElementWithSameNamespaceOptionally(VERSION);
92             if (versionElement.isPresent()) {
93                 version = Optional.of(versionElement.get().getTextContent());
94             } else {
95                 version = Optional.absent();
96             }
97         }
98     }
99 }