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.NamespaceURN;
13 import org.opendaylight.netconf.api.messages.RpcReplyMessage;
14 import org.opendaylight.netconf.api.xml.XmlElement;
15 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
17 import org.opendaylight.netconf.server.api.operations.AbstractNetconfOperation;
18 import org.opendaylight.netconf.server.api.operations.HandlingPriority;
19 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
21 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.common.ErrorType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
29 public class Get extends AbstractNetconfOperation {
30 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
32 private final NetconfMonitoringService netconfMonitor;
34 public Get(final SessionIdType sessionId, final NetconfMonitoringService netconfMonitor) {
36 this.netconfMonitor = netconfMonitor;
40 protected String getOperationName() {
41 return XmlNetconfConstants.GET;
45 protected HandlingPriority getHandlingPriority() {
46 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
49 @SuppressWarnings("checkstyle:IllegalCatch")
51 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
52 throws DocumentedException {
53 if (subsequentOperation.isExecutionTermination()) {
54 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
55 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
59 final var innerResult = subsequentOperation.execute(requestMessage);
60 final var netconfMonitoring = new NetconfState(netconfMonitor);
61 var monitoringXmlElement = JaxBSerializer.toXml(netconfMonitoring);
63 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
64 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
65 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
68 } catch (final RuntimeException e) {
69 final String errorMessage = "Get operation for netconf-state subtree failed";
70 LOG.warn(errorMessage, e);
72 throw new DocumentedException(errorMessage, e,
73 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
74 // FIXME: i.e. <error>e.getMessage()</error> ?
75 Map.of(ErrorSeverity.ERROR.elementBody(), e.getMessage()));
80 protected Element handle(final Document document, final XmlElement message,
81 final NetconfOperationChainedExecution subsequentOperation) {
82 throw new UnsupportedOperationException("Never gets called");
85 private static Element getPlaceholder(final Document innerResult) throws DocumentedException {
86 return XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(), RpcReplyMessage.ELEMENT_NAME,
88 .getOnlyChildElement(XmlNetconfConstants.DATA_KEY)