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