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.netconf.monitoring;
10 import java.util.Collections;
11 import org.opendaylight.controller.config.util.xml.DocumentedException;
12 import org.opendaylight.controller.config.util.xml.XmlElement;
13 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
14 import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation;
15 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.mapping.api.HandlingPriority;
18 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
19 import org.opendaylight.netconf.monitoring.xml.JaxBSerializer;
20 import org.opendaylight.netconf.monitoring.xml.model.NetconfState;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
26 public class Get extends AbstractNetconfOperation {
28 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
29 private final NetconfMonitoringService netconfMonitor;
31 public Get(final NetconfMonitoringService netconfMonitor) {
32 super(MonitoringConstants.MODULE_NAME);
33 this.netconfMonitor = netconfMonitor;
36 private Element getPlaceholder(final Document innerResult)
37 throws DocumentedException {
38 final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
39 innerResult.getDocumentElement(), XmlMappingConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
40 return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
44 protected String getOperationName() {
45 return XmlNetconfConstants.GET;
49 protected HandlingPriority getHandlingPriority() {
50 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
54 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
55 throws DocumentedException {
56 if (subsequentOperation.isExecutionTermination()){
57 throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
58 DocumentedException.ErrorType.application,
59 DocumentedException.ErrorTag.operation_failed,
60 DocumentedException.ErrorSeverity.error);
64 final Document innerResult = subsequentOperation.execute(requestMessage);
66 final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
67 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
69 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
70 final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
71 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
74 } catch (final RuntimeException e) {
75 final String errorMessage = "Get operation for netconf-state subtree failed";
76 LOG.warn(errorMessage, e);
78 throw new DocumentedException(errorMessage, DocumentedException.ErrorType.application,
79 DocumentedException.ErrorTag.operation_failed,
80 DocumentedException.ErrorSeverity.error,
81 Collections.singletonMap(DocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
86 protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation)
87 throws DocumentedException {
88 throw new UnsupportedOperationException("Never gets called");