Merge "BUG-832 Add initial configuration for controller self mount"
[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 java.util.Map;
11
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;
26
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Maps;
29
30 public class Get extends AbstractNetconfOperation {
31
32     private static final Logger logger = LoggerFactory.getLogger(Get.class);
33     private final NetconfMonitoringService netconfMonitor;
34
35     public Get(NetconfMonitoringService netconfMonitor) {
36         super(MonitoringConstants.MODULE_NAME);
37         this.netconfMonitor = netconfMonitor;
38     }
39
40     private Element getPlaceholder(Document innerResult) {
41         try {
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);
50         }
51     }
52
53     @Override
54     protected String getOperationName() {
55         return XmlNetconfConstants.GET;
56     }
57
58     @Override
59     protected HandlingPriority getHandlingPriority() {
60         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
61     }
62
63     @Override
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);
68
69         try {
70             Document innerResult = subsequentOperation.execute(requestMessage);
71
72             NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
73             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
74
75             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
76             Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
77             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
78
79             return innerResult;
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);
88         }
89     }
90
91     @Override
92     protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
93             throws NetconfDocumentedException {
94         throw new UnsupportedOperationException("Never gets called");
95     }
96 }