Integrate netconf-mapping-api into netconf-server
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / monitoring / Get.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.test.tool.monitoring;
9
10 import java.util.Map;
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.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;
25
26 public class Get extends AbstractNetconfOperation {
27     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
28
29     private final NetconfMonitoringService netconfMonitor;
30
31     public Get(final NetconfMonitoringService netconfMonitor) {
32         super(MonitoringConstants.MODULE_NAME);
33         this.netconfMonitor = netconfMonitor;
34     }
35
36     @Override
37     protected String getOperationName() {
38         return XmlNetconfConstants.GET;
39     }
40
41     @Override
42     protected HandlingPriority getHandlingPriority() {
43         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
44     }
45
46     @SuppressWarnings("checkstyle:IllegalCatch")
47     @Override
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);
53         }
54
55         try {
56             final Document innerResult = subsequentOperation.execute(requestMessage);
57
58             final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
59             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
60
61             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
62             final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
63             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
64
65             return innerResult;
66         } catch (final RuntimeException e) {
67             final String errorMessage = "Get operation for netconf-state subtree failed";
68             LOG.warn(errorMessage, e);
69
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()));
74         }
75     }
76
77     @Override
78     protected Element handle(final Document document, final XmlElement message,
79                              final NetconfOperationChainedExecution subsequentOperation) {
80         throw new UnsupportedOperationException("Never gets called");
81     }
82
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)
87             .getDomElement();
88     }
89 }