Merge "Bug 8153: Enforce check-style rules for netconf - netconf-topology"
[netconf.git] / netconf / netconf-monitoring / src / main / java / org / opendaylight / netconf / 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.monitoring;
9
10 import java.util.Collections;
11 import org.opendaylight.controller.config.util.xml.DocumentedException;
12 import org.opendaylight.controller.config.util.xml.XmlElement;
13 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
14 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
15 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.netconf.mapping.api.HandlingPriority;
17 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.netconf.monitoring.xml.JaxBSerializer;
19 import org.opendaylight.netconf.monitoring.xml.model.NetconfState;
20 import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation;
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
28     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
29     private final NetconfMonitoringService netconfMonitor;
30
31     public Get(final NetconfMonitoringService netconfMonitor) {
32         super(MonitoringConstants.MODULE_NAME);
33         this.netconfMonitor = netconfMonitor;
34     }
35
36     private Element getPlaceholder(final Document innerResult)
37             throws DocumentedException {
38         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
39                 innerResult.getDocumentElement(), XmlMappingConstants.RPC_REPLY_KEY,
40                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
41         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
42     }
43
44     @Override
45     protected String getOperationName() {
46         return XmlNetconfConstants.GET;
47     }
48
49     @Override
50     protected HandlingPriority getHandlingPriority() {
51         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
52     }
53
54     @SuppressWarnings("checkstyle:IllegalCatch")
55     @Override
56     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
57             throws DocumentedException {
58         if (subsequentOperation.isExecutionTermination()) {
59             throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
60                     DocumentedException.ErrorType.APPLICATION,
61                     DocumentedException.ErrorTag.OPERATION_FAILED,
62                     DocumentedException.ErrorSeverity.ERROR);
63         }
64
65         try {
66             final Document innerResult = subsequentOperation.execute(requestMessage);
67
68             final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
69             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
70
71             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
72             final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
73             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
74
75             return innerResult;
76         } catch (final RuntimeException e) {
77             final String errorMessage = "Get operation for netconf-state subtree failed";
78             LOG.warn(errorMessage, e);
79
80             throw new DocumentedException(errorMessage, DocumentedException.ErrorType.APPLICATION,
81                     DocumentedException.ErrorTag.OPERATION_FAILED,
82                     DocumentedException.ErrorSeverity.ERROR,
83                     Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), e.getMessage()));
84         }
85     }
86
87     @Override
88     protected Element handle(final Document document, final XmlElement message,
89                              final NetconfOperationChainedExecution subsequentOperation)
90             throws DocumentedException {
91         throw new UnsupportedOperationException("Never gets called");
92     }
93 }