2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.test.tool.rpchandler;
10 import java.util.Optional;
11 import org.opendaylight.netconf.api.DocumentedException;
12 import org.opendaylight.netconf.api.xml.XmlElement;
13 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
14 import org.opendaylight.netconf.api.xml.XmlUtil;
15 import org.opendaylight.netconf.mapping.api.HandlingPriority;
16 import org.opendaylight.netconf.mapping.api.NetconfOperation;
17 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.w3c.dom.Document;
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.
26 class SettableRpc implements NetconfOperation {
28 private final RpcHandler rpcHandler;
30 SettableRpc(final RpcHandler rpcHandler) {
31 this.rpcHandler = rpcHandler;
35 public HandlingPriority canHandle(final Document message) {
36 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1000);
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);
51 } else if (subsequentOperation.isExecutionTermination()) {
52 throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage),
53 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED,
56 return subsequentOperation.execute(requestMessage);
60 private static 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);