Merge "Added feature for sal-mdsal-xsql"
[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 java.util.Collections;
11 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
12 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
13 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
15 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
16 import org.opendaylight.controller.netconf.monitoring.xml.JaxBSerializer;
17 import org.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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 logger = 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 NetconfDocumentedException {
37         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
38                 innerResult.getDocumentElement(), XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.RFC4741_TARGET_NAMESPACE);
39         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
40     }
41
42     @Override
43     protected String getOperationName() {
44         return XmlNetconfConstants.GET;
45     }
46
47     @Override
48     protected HandlingPriority getHandlingPriority() {
49         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
50     }
51
52     @Override
53     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
54             throws NetconfDocumentedException {
55         if (subsequentOperation.isExecutionTermination()){
56             throw new NetconfDocumentedException(String.format("Subsequent netconf operation expected by %s", this),
57                     NetconfDocumentedException.ErrorType.application,
58                     NetconfDocumentedException.ErrorTag.operation_failed,
59                     NetconfDocumentedException.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             logger.warn(errorMessage, e);
76
77             throw new NetconfDocumentedException(errorMessage, NetconfDocumentedException.ErrorType.application,
78                     NetconfDocumentedException.ErrorTag.operation_failed,
79                     NetconfDocumentedException.ErrorSeverity.error,
80                     Collections.singletonMap(NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
81         }
82     }
83
84     @Override
85     protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation)
86             throws NetconfDocumentedException {
87         throw new UnsupportedOperationException("Never gets called");
88     }
89 }