Merge "BUG 9112: NPE in karaf cli when device is still connecting"
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / rpchandler / SettableRpc.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.netconf.test.tool.rpchandler;
10
11 import java.util.Optional;
12 import org.opendaylight.controller.config.util.xml.DocumentedException;
13 import org.opendaylight.controller.config.util.xml.XmlElement;
14 import org.opendaylight.controller.config.util.xml.XmlUtil;
15 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.netconf.mapping.api.HandlingPriority;
17 import org.opendaylight.netconf.mapping.api.NetconfOperation;
18 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
19 import org.w3c.dom.Document;
20
21 /**
22  * {@link NetconfOperation} implementation. It can be configured to intercept rpcs with defined input
23  * and reply with defined output. If input isn't defined, rpc handling is delegated to the subsequent
24  * {@link NetconfOperation} which is able to handle it.
25  */
26 class SettableRpc implements NetconfOperation {
27
28     private final RpcHandler rpcHandler;
29
30     SettableRpc(RpcHandler rpcHandler) {
31         this.rpcHandler = rpcHandler;
32     }
33
34     @Override
35     public HandlingPriority canHandle(final Document message) throws DocumentedException {
36         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1000);
37     }
38
39     @Override
40     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
41             throws DocumentedException {
42         final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
43         final XmlElement rpcElement = requestElement.getOnlyChildElement();
44         final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
45         final Optional<Document> response = rpcHandler.getResponse(rpcElement);
46         if (response.isPresent()) {
47             final Document document = response.get();
48             checkForError(document);
49             document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
50             return document;
51         } else if (subsequentOperation.isExecutionTermination()) {
52             throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage),
53                     DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED,
54                     DocumentedException.ErrorSeverity.ERROR);
55         } else {
56             return subsequentOperation.execute(requestMessage);
57         }
58     }
59
60     private void checkForError(final Document document) throws DocumentedException {
61         final XmlElement rpcReply = XmlElement.fromDomDocument(document);
62         if (rpcReply.getOnlyChildElementOptionally("rpc-error").isPresent()) {
63             throw DocumentedException.fromXMLDocument(document);
64         }
65     }
66
67 }