Bug 460 - Fix warning throughout netconf subsystem
[controller.git] / opendaylight / netconf / netconf-monitoring / src / main / java / org / opendaylight / controller / 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.controller.netconf.monitoring;
9
10 import com.google.common.collect.Maps;
11 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
12 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
13 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
14 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
15 import org.opendaylight.controller.netconf.monitoring.xml.JaxBSerializer;
16 import org.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
17 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25
26 import java.util.Map;
27
28 public class Get extends AbstractNetconfOperation {
29
30     private static final Logger logger = LoggerFactory.getLogger(Get.class);
31     private final NetconfMonitoringService netconfMonitor;
32
33     public Get(NetconfMonitoringService netconfMonitor) {
34         super(MonitoringConstants.MODULE_NAME);
35         this.netconfMonitor = netconfMonitor;
36     }
37
38     private Element getPlaceholder(Document innerResult) throws NetconfDocumentedException {
39         try {
40             XmlElement rootElement = null;
41             rootElement = XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
42                     XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.RFC4741_TARGET_NAMESPACE);
43             return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
44         } catch (RuntimeException e) {
45             throw new IllegalArgumentException(String.format(
46                     "Input xml in wrong format, Expecting root element %s with child element %s, but was %s",
47                     XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.DATA_KEY,
48                     XmlUtil.toString(innerResult.getDocumentElement())), e);
49         }
50     }
51
52     @Override
53     protected String getOperationName() {
54         return XmlNetconfConstants.GET;
55     }
56
57     @Override
58     protected HandlingPriority getHandlingPriority() {
59         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
60     }
61
62     @Override
63     public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation)
64             throws NetconfDocumentedException {
65         if (subsequentOperation.isExecutionTermination()){
66             throw new NetconfDocumentedException(String.format("Subsequent netconf operation expected by %s", this),
67                     NetconfDocumentedException.ErrorType.application,
68                     NetconfDocumentedException.ErrorTag.operation_failed,
69                     NetconfDocumentedException.ErrorSeverity.error);
70         }
71
72         try {
73             Document innerResult = subsequentOperation.execute(requestMessage);
74
75             NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
76             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
77
78             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
79             Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
80             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
81
82             return innerResult;
83         } catch (RuntimeException e) {
84             String errorMessage = "Get operation for netconf-state subtree failed";
85             logger.warn(errorMessage, e);
86             Map<String, String> info = Maps.newHashMap();
87             info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage());
88             throw new NetconfDocumentedException(errorMessage, NetconfDocumentedException.ErrorType.application,
89                     NetconfDocumentedException.ErrorTag.operation_failed,
90                     NetconfDocumentedException.ErrorSeverity.error, info);
91         }
92     }
93
94     @Override
95     protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
96             throws NetconfDocumentedException {
97         throw new UnsupportedOperationException("Never gets called");
98     }
99 }