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;
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
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.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
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;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Maps;
30 public class Get extends AbstractNetconfOperation {
32 private static final Logger logger = LoggerFactory.getLogger(Get.class);
33 private final NetconfMonitoringService netconfMonitor;
35 public Get(NetconfMonitoringService netconfMonitor) {
36 super(MonitoringConstants.MODULE_NAME);
37 this.netconfMonitor = netconfMonitor;
40 private Element getPlaceholder(Document innerResult) {
42 XmlElement rootElement = XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
43 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
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);
54 protected String getOperationName() {
55 return XmlNetconfConstants.GET;
59 protected HandlingPriority getHandlingPriority() {
60 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
64 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation)
65 throws NetconfDocumentedException {
66 Preconditions.checkArgument(subsequentOperation.isExecutionTermination() == false,
67 "Subsequent netconf operation expected by %s", this);
70 Document innerResult = subsequentOperation.execute(requestMessage);
72 NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
73 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
75 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
76 Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
77 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
80 } catch (RuntimeException e) {
81 String errorMessage = "Get operation for netconf-state subtree failed";
82 logger.warn(errorMessage, e);
83 Map<String, String> info = Maps.newHashMap();
84 info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage());
85 throw new NetconfDocumentedException(errorMessage, NetconfDocumentedException.ErrorType.application,
86 NetconfDocumentedException.ErrorTag.operation_failed,
87 NetconfDocumentedException.ErrorSeverity.error, info);
92 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
93 throws NetconfDocumentedException {
94 throw new UnsupportedOperationException("Never gets called");