Remove DocumentedException.ErrorType
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / customrpc / 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.customrpc;
9
10 import java.io.File;
11 import java.util.Optional;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.api.xml.XmlUtil;
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.opendaylight.yangtools.yang.common.ErrorSeverity;
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
30     private final RpcMapping mapping;
31
32     SettableRpc(final File rpcConfig) {
33         mapping = new RpcMapping(rpcConfig);
34     }
35
36     @Override
37     public HandlingPriority canHandle(final Document message) {
38         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1000);
39     }
40
41     @Override
42     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
43             throws DocumentedException {
44         final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
45         final XmlElement rpcElement = requestElement.getOnlyChildElement();
46         final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
47         final Optional<Document> response = mapping.getResponse(rpcElement);
48         if (response.isPresent()) {
49             final Document document = response.get();
50             checkForError(document);
51             document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
52             return document;
53         } else if (subsequentOperation.isExecutionTermination()) {
54             throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage),
55                     ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
56         } else {
57             return subsequentOperation.execute(requestMessage);
58         }
59     }
60
61     private static void checkForError(final Document document) throws DocumentedException {
62         final XmlElement rpcReply = XmlElement.fromDomDocument(document);
63         if (rpcReply.getOnlyChildElementOptionally("rpc-error").isPresent()) {
64             throw DocumentedException.fromXMLDocument(document);
65         }
66     }
67
68 }