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.NetconfOperationRouter;
13 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
14 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
15 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain;
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;
29 public class Get implements NetconfOperationFilter {
31 private static final Logger logger = LoggerFactory.getLogger(Get.class);
32 private final NetconfMonitoringService netconfMonitor;
34 public Get(NetconfMonitoringService netconfMonitor) {
35 this.netconfMonitor = netconfMonitor;
39 public Document doFilter(Document message, NetconfOperationRouter operationRouter,
40 NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
41 AbstractNetconfOperation.OperationNameAndNamespace operationNameAndNamespace = new AbstractNetconfOperation.OperationNameAndNamespace(
43 if (canHandle(operationNameAndNamespace)) {
44 return handle(message, operationRouter, filterChain);
46 return filterChain.execute(message, operationRouter);
49 private Document handle(Document message, NetconfOperationRouter operationRouter,
50 NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
52 Document innerResult = filterChain.execute(message, operationRouter);
54 NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
55 Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
57 monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
58 Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
59 monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
62 } catch (RuntimeException e) {
63 String errorMessage = "Get operation for netconf-state subtree failed";
64 logger.warn(errorMessage, e);
65 Map<String, String> info = Maps.newHashMap();
66 info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage());
67 throw new NetconfDocumentedException(errorMessage, NetconfDocumentedException.ErrorType.application,
68 NetconfDocumentedException.ErrorTag.operation_failed,
69 NetconfDocumentedException.ErrorSeverity.error, info);
73 private Element getPlaceholder(Document innerResult) {
75 XmlElement rootElement = XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
76 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.RFC4741_TARGET_NAMESPACE);
77 return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
78 } catch (RuntimeException e) {
79 throw new IllegalArgumentException(String.format(
80 "Input xml in wrong format, Expecting root element %s with child element %s, but was %s",
81 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.DATA_KEY,
82 XmlUtil.toString(innerResult.getDocumentElement())), e);
86 private boolean canHandle(AbstractNetconfOperation.OperationNameAndNamespace operationNameAndNamespace) {
87 if (operationNameAndNamespace.getOperationName().equals(XmlNetconfConstants.GET) == false)
89 return operationNameAndNamespace.getNamespace().equals(
90 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
94 public int getSortingOrder() {
95 // FIXME filters for different operations cannot have same order
100 public int compareTo(NetconfOperationFilter o) {
101 return Integer.compare(getSortingOrder(), o.getSortingOrder());