Remove netconf-monitoring
[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.Collections;
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.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22
23 public class Get extends AbstractNetconfOperation {
24
25     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
26     private final NetconfMonitoringService netconfMonitor;
27
28     public Get(final NetconfMonitoringService netconfMonitor) {
29         super(MonitoringConstants.MODULE_NAME);
30         this.netconfMonitor = netconfMonitor;
31     }
32
33     private Element getPlaceholder(final Document innerResult)
34             throws DocumentedException {
35         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
36                 innerResult.getDocumentElement(), XmlNetconfConstants.RPC_REPLY_KEY,
37                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
38         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
39     }
40
41     @Override
42     protected String getOperationName() {
43         return XmlNetconfConstants.GET;
44     }
45
46     @Override
47     protected HandlingPriority getHandlingPriority() {
48         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
49     }
50
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     @Override
53     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
54             throws DocumentedException {
55         if (subsequentOperation.isExecutionTermination()) {
56             throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
57                     DocumentedException.ErrorType.APPLICATION,
58                     DocumentedException.ErrorTag.OPERATION_FAILED,
59                     DocumentedException.ErrorSeverity.ERROR);
60         }
61
62         try {
63             final Document innerResult = subsequentOperation.execute(requestMessage);
64
65             final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
66             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
67
68             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
69             final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
70             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
71
72             return innerResult;
73         } catch (final RuntimeException e) {
74             final String errorMessage = "Get operation for netconf-state subtree failed";
75             LOG.warn(errorMessage, e);
76
77             throw new DocumentedException(errorMessage, e, DocumentedException.ErrorType.APPLICATION,
78                     DocumentedException.ErrorTag.OPERATION_FAILED,
79                     DocumentedException.ErrorSeverity.ERROR,
80                     Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), e.getMessage()));
81         }
82     }
83
84     @Override
85     protected Element handle(final Document document, final XmlElement message,
86                              final NetconfOperationChainedExecution subsequentOperation) {
87         throw new UnsupportedOperationException("Never gets called");
88     }
89 }