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