2 * Copyright (c) 2015 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.mdsal.connector.ops;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
17 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
19 import com.google.common.base.Preconditions;
20 import com.google.common.io.ByteSource;
21 import com.google.common.util.concurrent.FluentFuture;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.List;
25 import javax.xml.parsers.ParserConfigurationException;
26 import org.custommonkey.xmlunit.DetailedDiff;
27 import org.custommonkey.xmlunit.Diff;
28 import org.custommonkey.xmlunit.XMLUnit;
29 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
39 import org.opendaylight.mdsal.dom.api.DOMRpcException;
40 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
41 import org.opendaylight.mdsal.dom.api.DOMRpcService;
42 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
43 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
44 import org.opendaylight.netconf.api.DocumentedException;
45 import org.opendaylight.netconf.api.xml.XmlUtil;
46 import org.opendaylight.netconf.mapping.api.HandlingPriority;
47 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
48 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
49 import org.opendaylight.netconf.util.test.XmlFileLoader;
50 import org.opendaylight.yangtools.concepts.ListenerRegistration;
51 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
52 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
53 import org.opendaylight.yangtools.yang.common.ErrorTag;
54 import org.opendaylight.yangtools.yang.common.ErrorType;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.common.XMLNamespace;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
60 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
61 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
62 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
63 import org.opendaylight.yangtools.yang.model.api.Module;
64 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
65 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
66 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
67 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
68 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71 import org.w3c.dom.Document;
72 import org.xml.sax.SAXException;
74 @RunWith(MockitoJUnitRunner.StrictStubs.class)
75 public class RuntimeRpcTest {
76 private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpcTest.class);
77 private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
78 private static final Document RPC_REPLY_OK = getReplyOk();
80 private static Document getReplyOk() {
82 return XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/runtimerpc-ok-reply.xml");
83 } catch (final IOException | SAXException | ParserConfigurationException e) {
84 LOG.debug("unable to load rpc reply ok.", e);
85 return XmlUtil.newDocument();
89 private static final DOMRpcService RPC_SERVICE_VOID_INVOKER = new DOMRpcService() {
91 public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
92 return immediateFluentFuture(new DefaultDOMRpcResult(null, List.of()));
96 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
97 return NoOpListenerRegistration.of(listener);
101 private static final DOMRpcService RPC_SERVICE_FAILED_INVOCATION = new DOMRpcService() {
103 public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
104 return immediateFailedFluentFuture(new DOMRpcException("rpc invocation not implemented yet") {
105 private static final long serialVersionUID = 1L;
110 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
111 return NoOpListenerRegistration.of(listener);
115 private final DOMRpcService rpcServiceSuccessfulInvocation = new DOMRpcService() {
117 public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
118 final Collection<DataContainerChild> children = input.body();
119 final Module module = SCHEMA_CONTEXT.findModules(type.getNamespace()).stream()
120 .findFirst().orElse(null);
121 final RpcDefinition rpcDefinition = getRpcDefinitionFromModule(module, module.getNamespace(),
122 type.getLocalName());
123 final ContainerNode node = Builders.containerBuilder()
124 .withNodeIdentifier(new NodeIdentifier(rpcDefinition.getOutput().getQName()))
128 return immediateFluentFuture(new DefaultDOMRpcResult(node));
132 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
133 return NoOpListenerRegistration.of(lsnr);
137 private static EffectiveModelContext SCHEMA_CONTEXT = null;
138 private CurrentSchemaContext currentSchemaContext = null;
141 private DOMSchemaService schemaService;
143 private EffectiveModelContextListener listener;
145 private ListenerRegistration<?> registration;
147 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
150 public static void beforeClass() {
151 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yang/mdsal-netconf-rpc-test.yang");
155 public static void afterClass() {
156 SCHEMA_CONTEXT = null;
160 public void setUp() throws Exception {
161 doNothing().when(registration).close();
162 doAnswer(invocationOnMock -> {
163 ((EffectiveModelContextListener) invocationOnMock.getArguments()[0]).onModelContextUpdated(SCHEMA_CONTEXT);
165 }).when(schemaService).registerSchemaContextListener(any(EffectiveModelContextListener.class));
167 XMLUnit.setIgnoreWhitespace(true);
168 XMLUnit.setIgnoreAttributeOrder(true);
170 doAnswer(invocationOnMock -> {
171 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
172 final YangTextSchemaSource yangTextSchemaSource =
173 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
174 return immediateFluentFuture(yangTextSchemaSource);
175 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
177 currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceProvider);
181 public void tearDown() {
182 currentSchemaContext.close();
186 public void testVoidOutputRpc() throws Exception {
187 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
189 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-output.xml");
190 final HandlingPriority priority = rpc.canHandle(rpcDocument);
191 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
193 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
195 verifyResponse(response, RPC_REPLY_OK);
199 public void testSuccesfullNonVoidInvocation() throws Exception {
200 final RuntimeRpc rpc = new RuntimeRpc(
201 SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
203 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
204 final HandlingPriority priority = rpc.canHandle(rpcDocument);
205 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
207 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
208 verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid-control.xml"));
212 public void testSuccesfullContainerInvocation() throws Exception {
213 final RuntimeRpc rpc = new RuntimeRpc(
214 SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
216 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container.xml");
217 final HandlingPriority priority = rpc.canHandle(rpcDocument);
218 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
220 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
221 verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container-control.xml"));
225 public void testFailedInvocation() throws Exception {
226 final RuntimeRpc rpc = new RuntimeRpc(
227 SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_FAILED_INVOCATION);
229 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
230 final HandlingPriority priority = rpc.canHandle(rpcDocument);
231 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
233 final DocumentedException e = assertThrows(DocumentedException.class,
234 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
236 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
237 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_FAILED);
238 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
242 public void testVoidInputOutputRpc() throws Exception {
243 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
245 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-input-output.xml");
246 final HandlingPriority priority = rpc.canHandle(rpcDocument);
247 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
249 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
251 verifyResponse(response, RPC_REPLY_OK);
255 public void testBadNamespaceInRpc() throws Exception {
256 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
257 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-bad-namespace.xml");
259 final DocumentedException e = assertThrows(DocumentedException.class,
260 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
262 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
263 assertEquals(e.getErrorTag(), ErrorTag.BAD_ELEMENT);
264 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
267 private static void verifyResponse(final Document response, final Document template) {
268 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
269 dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
270 //we care about order so response has to be identical
271 assertTrue(dd.identical());
274 private static RpcDefinition getRpcDefinitionFromModule(final Module module, final XMLNamespace namespaceURI,
276 for (final RpcDefinition rpcDef : module.getRpcs()) {
277 if (rpcDef.getQName().getNamespace().equals(namespaceURI)
278 && rpcDef.getQName().getLocalName().equals(name)) {