Initial implementation of netconf monitoring module according to http://tools.ietf...
[controller.git] / opendaylight / netconf / netconf-monitoring / src / main / java / org / opendaylight / controller / netconf / monitoring / Get.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.monitoring;
9
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;
26
27 import java.util.Map;
28
29 public class Get implements NetconfOperationFilter {
30
31     private static final Logger logger = LoggerFactory.getLogger(Get.class);
32     private final NetconfMonitoringService netconfMonitor;
33
34     public Get(NetconfMonitoringService netconfMonitor) {
35         this.netconfMonitor = netconfMonitor;
36     }
37
38     @Override
39     public Document doFilter(Document message, NetconfOperationRouter operationRouter,
40             NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
41         AbstractNetconfOperation.OperationNameAndNamespace operationNameAndNamespace = new AbstractNetconfOperation.OperationNameAndNamespace(
42                 message);
43         if (canHandle(operationNameAndNamespace)) {
44             return handle(message, operationRouter, filterChain);
45         }
46         return filterChain.execute(message, operationRouter);
47     }
48
49     private Document handle(Document message, NetconfOperationRouter operationRouter,
50             NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
51         try {
52             Document innerResult = filterChain.execute(message, operationRouter);
53
54             NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
55             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
56
57             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
58             Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
59             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
60
61             return innerResult;
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);
70         }
71     }
72
73     private Element getPlaceholder(Document innerResult) {
74         try {
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);
83         }
84     }
85
86     private boolean canHandle(AbstractNetconfOperation.OperationNameAndNamespace operationNameAndNamespace) {
87         if (operationNameAndNamespace.getOperationName().equals(XmlNetconfConstants.GET) == false)
88             return false;
89         return operationNameAndNamespace.getNamespace().equals(
90                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
91     }
92
93     @Override
94     public int getSortingOrder() {
95         // FIXME filters for different operations cannot have same order
96         return 1;
97     }
98
99     @Override
100     public int compareTo(NetconfOperationFilter o) {
101         return Integer.compare(getSortingOrder(), o.getSortingOrder());
102     }
103
104 }