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.test.tool.monitoring;
11 import org.opendaylight.netconf.api.DocumentedException;
12 import org.opendaylight.netconf.api.xml.XmlElement;
13 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
14 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
15 import org.opendaylight.netconf.server.api.operations.AbstractNetconfOperation;
16 import org.opendaylight.netconf.server.api.operations.HandlingPriority;
17 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.ErrorTag;
20 import org.opendaylight.yangtools.yang.common.ErrorType;
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 {
27 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;
37 protected String getOperationName() {
38 return XmlNetconfConstants.GET;
42 protected HandlingPriority getHandlingPriority() {
43 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
46 @SuppressWarnings("checkstyle:IllegalCatch")
48 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
49 throws DocumentedException {
50 if (subsequentOperation.isExecutionTermination()) {
51 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
52 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
56 final Document innerResult = subsequentOperation.execute(requestMessage);
58 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
59 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
61 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
62 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
63 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
66 } catch (final RuntimeException e) {
67 final String errorMessage = "Get operation for netconf-state subtree failed";
68 LOG.warn(errorMessage, e);
70 throw new DocumentedException(errorMessage, e,
71 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
72 // FIXME: i.e. <error>e.getMessage()</error> ?
73 Map.of(ErrorSeverity.ERROR.elementBody(), e.getMessage()));
78 protected Element handle(final Document document, final XmlElement message,
79 final NetconfOperationChainedExecution subsequentOperation) {
80 throw new UnsupportedOperationException("Never gets called");
83 private static Element getPlaceholder(final Document innerResult) throws DocumentedException {
84 return XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
85 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)
86 .getOnlyChildElement(XmlNetconfConstants.DATA_KEY)