Add netconf.api.NamespaceURN
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / 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.netconf.test.tool.monitoring;
9
10 import java.util.Map;
11 import org.opendaylight.netconf.api.DocumentedException;
12 import org.opendaylight.netconf.api.NamespaceURN;
13 import org.opendaylight.netconf.api.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
16 import org.opendaylight.netconf.server.api.operations.AbstractNetconfOperation;
17 import org.opendaylight.netconf.server.api.operations.HandlingPriority;
18 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
20 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.common.ErrorType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 public class Get extends AbstractNetconfOperation {
29     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
30
31     private final NetconfMonitoringService netconfMonitor;
32
33     public Get(final SessionIdType sessionId, final NetconfMonitoringService netconfMonitor) {
34         super(sessionId);
35         this.netconfMonitor = netconfMonitor;
36     }
37
38     @Override
39     protected String getOperationName() {
40         return XmlNetconfConstants.GET;
41     }
42
43     @Override
44     protected HandlingPriority getHandlingPriority() {
45         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
46     }
47
48     @SuppressWarnings("checkstyle:IllegalCatch")
49     @Override
50     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
51             throws DocumentedException {
52         if (subsequentOperation.isExecutionTermination()) {
53             throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this),
54                     ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
55         }
56
57         try {
58             final Document innerResult = subsequentOperation.execute(requestMessage);
59
60             final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
61             Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
62
63             monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
64             final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
65             monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
66
67             return innerResult;
68         } catch (final RuntimeException e) {
69             final String errorMessage = "Get operation for netconf-state subtree failed";
70             LOG.warn(errorMessage, e);
71
72             throw new DocumentedException(errorMessage, e,
73                     ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
74                     // FIXME: i.e. <error>e.getMessage()</error> ?
75                     Map.of(ErrorSeverity.ERROR.elementBody(), e.getMessage()));
76         }
77     }
78
79     @Override
80     protected Element handle(final Document document, final XmlElement message,
81                              final NetconfOperationChainedExecution subsequentOperation) {
82         throw new UnsupportedOperationException("Never gets called");
83     }
84
85     private static Element getPlaceholder(final Document innerResult) throws DocumentedException {
86         return XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
87             XmlNetconfConstants.RPC_REPLY_KEY, NamespaceURN.BASE)
88             .getOnlyChildElement(XmlNetconfConstants.DATA_KEY)
89             .getDomElement();
90     }
91 }