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.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.util.mapping.AbstractNetconfOperation;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 public class Get extends AbstractNetconfOperation {
26 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
27 private final NetconfMonitoringService netconfMonitor;
29 public Get(final NetconfMonitoringService netconfMonitor) {
30 super(MonitoringConstants.MODULE_NAME);
31 this.netconfMonitor = netconfMonitor;
34 private Element getPlaceholder(final Document innerResult)
35 throws DocumentedException {
36 final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
37 innerResult.getDocumentElement(), XmlNetconfConstants.RPC_REPLY_KEY,
38 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
39 return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
43 protected String getOperationName() {
44 return XmlNetconfConstants.GET;
48 protected HandlingPriority getHandlingPriority() {
49 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
52 @SuppressWarnings("checkstyle:IllegalCatch")
54 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
55 throws DocumentedException {
56 if (subsequentOperation.isExecutionTermination()) {
57 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
58 DocumentedException.ErrorType.APPLICATION,
59 DocumentedException.ErrorTag.OPERATION_FAILED,
64 final Document innerResult = subsequentOperation.execute(requestMessage);
66 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
67 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
69 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
70 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
71 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
74 } catch (final RuntimeException e) {
75 final String errorMessage = "Get operation for netconf-state subtree failed";
76 LOG.warn(errorMessage, e);
78 throw new DocumentedException(errorMessage, e, DocumentedException.ErrorType.APPLICATION,
79 DocumentedException.ErrorTag.OPERATION_FAILED,
81 Map.of(ErrorSeverity.ERROR.toString(), e.getMessage()));
86 protected Element handle(final Document document, final XmlElement message,
87 final NetconfOperationChainedExecution subsequentOperation) {
88 throw new UnsupportedOperationException("Never gets called");