2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.monitoring;
10 import java.util.Collections;
11 import org.opendaylight.controller.config.util.xml.DocumentedException;
12 import org.opendaylight.controller.config.util.xml.XmlElement;
13 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
14 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
15 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.netconf.mapping.api.HandlingPriority;
17 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.netconf.monitoring.xml.JaxBSerializer;
19 import org.opendaylight.netconf.monitoring.xml.model.NetconfState;
20 import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
26 public class Get extends AbstractNetconfOperation {
28 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
29 private final NetconfMonitoringService netconfMonitor;
31 public Get(final NetconfMonitoringService netconfMonitor) {
32 super(MonitoringConstants.MODULE_NAME);
33 this.netconfMonitor = netconfMonitor;
36 private Element getPlaceholder(final Document innerResult)
37 throws DocumentedException {
38 final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
39 innerResult.getDocumentElement(), XmlMappingConstants.RPC_REPLY_KEY,
40 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
41 return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
45 protected String getOperationName() {
46 return XmlNetconfConstants.GET;
50 protected HandlingPriority getHandlingPriority() {
51 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
54 @SuppressWarnings("checkstyle:IllegalCatch")
56 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
57 throws DocumentedException {
58 if (subsequentOperation.isExecutionTermination()) {
59 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
60 DocumentedException.ErrorType.APPLICATION,
61 DocumentedException.ErrorTag.OPERATION_FAILED,
62 DocumentedException.ErrorSeverity.ERROR);
66 final Document innerResult = subsequentOperation.execute(requestMessage);
68 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
69 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
71 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
72 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
73 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
76 } catch (final RuntimeException e) {
77 final String errorMessage = "Get operation for netconf-state subtree failed";
78 LOG.warn(errorMessage, e);
80 throw new DocumentedException(errorMessage, e, DocumentedException.ErrorType.APPLICATION,
81 DocumentedException.ErrorTag.OPERATION_FAILED,
82 DocumentedException.ErrorSeverity.ERROR,
83 Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), e.getMessage()));
88 protected Element handle(final Document document, final XmlElement message,
89 final NetconfOperationChainedExecution subsequentOperation)
90 throws DocumentedException {
91 throw new UnsupportedOperationException("Never gets called");