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 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;
28 public class Get extends AbstractNetconfOperation {
30 private static final Logger logger = LoggerFactory.getLogger(Get.class);
31 private final NetconfMonitoringService netconfMonitor;
33 public Get(NetconfMonitoringService netconfMonitor) {
34 super(MonitoringConstants.MODULE_NAME);
35 this.netconfMonitor = netconfMonitor;
38 private Element getPlaceholder(Document innerResult) throws NetconfDocumentedException {
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);
53 protected String getOperationName() {
54 return XmlNetconfConstants.GET;
58 protected HandlingPriority getHandlingPriority() {
59 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
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);
73 Document innerResult = subsequentOperation.execute(requestMessage);
75 NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
76 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
78 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
79 Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
80 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
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);
95 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
96 throws NetconfDocumentedException {
97 throw new UnsupportedOperationException("Never gets called");