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.server.mdsal.operations;
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;
17 import com.google.common.base.Preconditions;
18 import com.google.common.io.ByteSource;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import java.io.IOException;
22 import java.io.Serial;
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.server.api.operations.HandlingPriority;
47 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
48 import org.opendaylight.netconf.server.mdsal.CurrentSchemaContext;
49 import org.opendaylight.netconf.test.util.XmlFileLoader;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
53 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
54 import org.opendaylight.yangtools.yang.common.ErrorTag;
55 import org.opendaylight.yangtools.yang.common.ErrorType;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.common.Uint32;
58 import org.opendaylight.yangtools.yang.common.XMLNamespace;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
62 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
63 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
64 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
67 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
68 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
70 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.w3c.dom.Document;
74 import org.xml.sax.SAXException;
76 @RunWith(MockitoJUnitRunner.StrictStubs.class)
77 public class RuntimeRpcTest {
78 private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpcTest.class);
79 private static final SessionIdType SESSION_ID_FOR_REPORTING = new SessionIdType(Uint32.valueOf(123));
80 private static final Document RPC_REPLY_OK = getReplyOk();
82 private static Document getReplyOk() {
84 return XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/runtimerpc-ok-reply.xml");
85 } catch (final IOException | SAXException | ParserConfigurationException e) {
86 LOG.debug("unable to load rpc reply ok.", e);
87 return XmlUtil.newDocument();
91 private static final DOMRpcService RPC_SERVICE_VOID_INVOKER = new DOMRpcService() {
93 public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
94 return Futures.immediateFuture(new DefaultDOMRpcResult(null, List.of()));
98 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
99 return NoOpListenerRegistration.of(listener);
103 private static final DOMRpcService RPC_SERVICE_FAILED_INVOCATION = new DOMRpcService() {
105 public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
106 return Futures.immediateFailedFuture(new DOMRpcException("rpc invocation not implemented yet") {
108 private static final long serialVersionUID = 1L;
113 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
114 return NoOpListenerRegistration.of(listener);
118 private final DOMRpcService rpcServiceSuccessfulInvocation = new DOMRpcService() {
120 public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
121 final Collection<DataContainerChild> children = input.body();
122 final Module module = SCHEMA_CONTEXT.findModules(type.getNamespace()).stream()
123 .findFirst().orElse(null);
124 final RpcDefinition rpcDefinition = getRpcDefinitionFromModule(module, module.getNamespace(),
125 type.getLocalName());
126 final ContainerNode node = Builders.containerBuilder()
127 .withNodeIdentifier(new NodeIdentifier(rpcDefinition.getOutput().getQName()))
131 return Futures.immediateFuture(new DefaultDOMRpcResult(node));
135 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
136 return NoOpListenerRegistration.of(lsnr);
140 private static EffectiveModelContext SCHEMA_CONTEXT = null;
141 private CurrentSchemaContext currentSchemaContext = null;
144 private DOMSchemaService schemaService;
146 private EffectiveModelContextListener listener;
148 private ListenerRegistration<?> registration;
150 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
153 public static void beforeClass() {
154 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yang/mdsal-netconf-rpc-test.yang");
158 public static void afterClass() {
159 SCHEMA_CONTEXT = null;
163 public void setUp() throws Exception {
164 doNothing().when(registration).close();
165 doAnswer(invocationOnMock -> {
166 ((EffectiveModelContextListener) invocationOnMock.getArguments()[0]).onModelContextUpdated(SCHEMA_CONTEXT);
168 }).when(schemaService).registerSchemaContextListener(any(EffectiveModelContextListener.class));
170 XMLUnit.setIgnoreWhitespace(true);
171 XMLUnit.setIgnoreAttributeOrder(true);
173 doAnswer(invocationOnMock -> {
174 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
175 final YangTextSchemaSource yangTextSchemaSource =
176 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
177 return Futures.immediateFuture(yangTextSchemaSource);
178 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
180 currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceProvider);
184 public void tearDown() {
185 currentSchemaContext.close();
189 public void testVoidOutputRpc() throws Exception {
190 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
192 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-output.xml");
193 final HandlingPriority priority = rpc.canHandle(rpcDocument);
194 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
196 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
198 verifyResponse(response, RPC_REPLY_OK);
202 public void testSuccesfullNonVoidInvocation() throws Exception {
203 final RuntimeRpc rpc = new RuntimeRpc(
204 SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
206 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
207 final HandlingPriority priority = rpc.canHandle(rpcDocument);
208 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
210 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
211 verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid-control.xml"));
215 public void testSuccesfullContainerInvocation() throws Exception {
216 final RuntimeRpc rpc = new RuntimeRpc(
217 SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
219 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container.xml");
220 final HandlingPriority priority = rpc.canHandle(rpcDocument);
221 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
223 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
224 verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container-control.xml"));
228 public void testFailedInvocation() throws Exception {
229 final RuntimeRpc rpc = new RuntimeRpc(
230 SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_FAILED_INVOCATION);
232 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
233 final HandlingPriority priority = rpc.canHandle(rpcDocument);
234 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
236 final DocumentedException e = assertThrows(DocumentedException.class,
237 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
239 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
240 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_FAILED);
241 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
245 public void testVoidInputOutputRpc() throws Exception {
246 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
248 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-input-output.xml");
249 final HandlingPriority priority = rpc.canHandle(rpcDocument);
250 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
252 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
254 verifyResponse(response, RPC_REPLY_OK);
258 public void testBadNamespaceInRpc() throws Exception {
259 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
260 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-bad-namespace.xml");
262 final DocumentedException e = assertThrows(DocumentedException.class,
263 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
265 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
266 assertEquals(e.getErrorTag(), ErrorTag.BAD_ELEMENT);
267 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
270 private static void verifyResponse(final Document response, final Document template) {
271 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
272 dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
273 //we care about order so response has to be identical
274 assertTrue(dd.identical());
277 private static RpcDefinition getRpcDefinitionFromModule(final Module module, final XMLNamespace namespaceURI,
279 for (final RpcDefinition rpcDef : module.getRpcs()) {
280 if (rpcDef.getQName().getNamespace().equals(namespaceURI)
281 && rpcDef.getQName().getLocalName().equals(name)) {