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.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
16 import org.opendaylight.netconf.server.api.operations.AbstractNetconfOperation;
17 import org.opendaylight.netconf.server.api.operations.HandlingPriority;
18 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
20 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.common.ErrorType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
28 public class Get extends AbstractNetconfOperation {
29 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
31 private final NetconfMonitoringService netconfMonitor;
33 public Get(final SessionIdType sessionId, final NetconfMonitoringService netconfMonitor) {
35 this.netconfMonitor = netconfMonitor;
39 protected String getOperationName() {
40 return XmlNetconfConstants.GET;
44 protected HandlingPriority getHandlingPriority() {
45 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
48 @SuppressWarnings("checkstyle:IllegalCatch")
50 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
51 throws DocumentedException {
52 if (subsequentOperation.isExecutionTermination()) {
53 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
54 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
58 final Document innerResult = subsequentOperation.execute(requestMessage);
60 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
61 Element monitoringXmlElement = new 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(),
87 XmlNetconfConstants.RPC_REPLY_KEY, NamespaceURN.BASE)
88 .getOnlyChildElement(XmlNetconfConstants.DATA_KEY)