Integrate netconf-mapping-api into netconf-server
[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 package org.opendaylight.netconf.test.tool.rpchandler;
9
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.server.api.operations.HandlingPriority;
16 import org.opendaylight.netconf.server.api.operations.NetconfOperation;
17 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.ErrorTag;
20 import org.opendaylight.yangtools.yang.common.ErrorType;
21 import org.w3c.dom.Document;
22
23 /**
24  * {@link NetconfOperation} implementation. It can be configured to intercept rpcs with defined input
25  * and reply with defined output. If input isn't defined, rpc handling is delegated to the subsequent
26  * {@link NetconfOperation} which is able to handle it.
27  */
28 class SettableRpc implements NetconfOperation {
29     private final RpcHandler rpcHandler;
30
31     SettableRpc(final RpcHandler rpcHandler) {
32         this.rpcHandler = rpcHandler;
33     }
34
35     @Override
36     public HandlingPriority canHandle(final Document message) {
37         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1000);
38     }
39
40     @Override
41     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
42             throws DocumentedException {
43         final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
44         final XmlElement rpcElement = requestElement.getOnlyChildElement();
45         final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
46         final Optional<Document> response = rpcHandler.getResponse(rpcElement);
47         if (response.isPresent()) {
48             final Document document = response.orElseThrow();
49             checkForError(document);
50             document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
51             return document;
52         } else if (subsequentOperation.isExecutionTermination()) {
53             throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage),
54                     ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
55         } else {
56             return subsequentOperation.execute(requestMessage);
57         }
58     }
59
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);
64         }
65     }
66 }