2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.monitoring;
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;
25 public class Get extends AbstractNetconfOperation {
27 private static final Logger logger = LoggerFactory.getLogger(Get.class);
28 private final NetconfMonitoringService netconfMonitor;
30 public Get(final NetconfMonitoringService netconfMonitor) {
31 super(MonitoringConstants.MODULE_NAME);
32 this.netconfMonitor = netconfMonitor;
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();
43 protected String getOperationName() {
44 return XmlNetconfConstants.GET;
48 protected HandlingPriority getHandlingPriority() {
49 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
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);
63 final Document innerResult = subsequentOperation.execute(requestMessage);
65 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
66 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
68 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
69 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
70 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
73 } catch (final RuntimeException e) {
74 final String errorMessage = "Get operation for netconf-state subtree failed";
75 logger.warn(errorMessage, e);
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()));
85 protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation)
86 throws NetconfDocumentedException {
87 throw new UnsupportedOperationException("Never gets called");