Decouple config and netconf subsystems.
[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.Collections;
11 import org.opendaylight.controller.config.util.xml.DocumentedException;
12 import org.opendaylight.controller.config.util.xml.XmlElement;
13 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
14 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
15 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.controller.netconf.monitoring.xml.JaxBSerializer;
19 import org.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25
26 public class Get extends AbstractNetconfOperation {
27
28     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
29     private final NetconfMonitoringService netconfMonitor;
30
31     public Get(final NetconfMonitoringService netconfMonitor) {
32         super(MonitoringConstants.MODULE_NAME);
33         this.netconfMonitor = netconfMonitor;
34     }
35
36     private Element getPlaceholder(final Document innerResult)
37             throws DocumentedException {
38         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
39                 innerResult.getDocumentElement(), XmlMappingConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
40         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
41     }
42
43     @Override
44     protected String getOperationName() {
45         return XmlNetconfConstants.GET;
46     }
47
48     @Override
49     protected HandlingPriority getHandlingPriority() {
50         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
51     }
52
53     @Override
54     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
55             throws DocumentedException {
56         if (subsequentOperation.isExecutionTermination()){
57             throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
58                     DocumentedException.ErrorType.application,
59                     DocumentedException.ErrorTag.operation_failed,
60                     DocumentedException.ErrorSeverity.error);
61         }
62
63         try {
64             final Document innerResult = subsequentOperation.execute(requestMessage);
65
66             final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
67             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
68
69             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
70             final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
71             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
72
73             return innerResult;
74         } catch (final RuntimeException e) {
75             final String errorMessage = "Get operation for netconf-state subtree failed";
76             LOG.warn(errorMessage, e);
77
78             throw new DocumentedException(errorMessage, DocumentedException.ErrorType.application,
79                     DocumentedException.ErrorTag.operation_failed,
80                     DocumentedException.ErrorSeverity.error,
81                     Collections.singletonMap(DocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
82         }
83     }
84
85     @Override
86     protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation)
87             throws DocumentedException {
88         throw new UnsupportedOperationException("Never gets called");
89     }
90 }