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.netconf.api.DocumentedException;
12 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
13 import org.opendaylight.netconf.api.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.mapping.api.HandlingPriority;
16 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
17 import org.opendaylight.netconf.monitoring.xml.JaxBSerializer;
18 import org.opendaylight.netconf.monitoring.xml.model.NetconfState;
19 import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
25 public class Get extends AbstractNetconfOperation {
27 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
28 private final NetconfMonitoringService netconfMonitor;
30 public Get(final NetconfMonitoringService netconfMonitor) {
31 super(MonitoringConstants.MODULE_NAME);
32 this.netconfMonitor = netconfMonitor;
35 private Element getPlaceholder(final Document innerResult)
36 throws DocumentedException {
37 final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
38 innerResult.getDocumentElement(), XmlNetconfConstants.RPC_REPLY_KEY,
39 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
40 return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
44 protected String getOperationName() {
45 return XmlNetconfConstants.GET;
49 protected HandlingPriority getHandlingPriority() {
50 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
53 @SuppressWarnings("checkstyle:IllegalCatch")
55 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
56 throws DocumentedException {
57 if (subsequentOperation.isExecutionTermination()) {
58 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
59 DocumentedException.ErrorType.APPLICATION,
60 DocumentedException.ErrorTag.OPERATION_FAILED,
61 DocumentedException.ErrorSeverity.ERROR);
65 final Document innerResult = subsequentOperation.execute(requestMessage);
67 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
68 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
70 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
71 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
72 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
75 } catch (final RuntimeException e) {
76 final String errorMessage = "Get operation for netconf-state subtree failed";
77 LOG.warn(errorMessage, e);
79 throw new DocumentedException(errorMessage, e, DocumentedException.ErrorType.APPLICATION,
80 DocumentedException.ErrorTag.OPERATION_FAILED,
81 DocumentedException.ErrorSeverity.ERROR,
82 Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), e.getMessage()));
87 protected Element handle(final Document document, final XmlElement message,
88 final NetconfOperationChainedExecution subsequentOperation) {
89 throw new UnsupportedOperationException("Never gets called");