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;
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.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
62 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
63 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode;
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 String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
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 FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
94 return immediateFluentFuture(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 FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
106 return immediateFailedFluentFuture(new DOMRpcException("rpc invocation not implemented yet") {
107 private static final long serialVersionUID = 1L;
112 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
113 return NoOpListenerRegistration.of(listener);
117 private final DOMRpcService rpcServiceSuccessfulInvocation = new DOMRpcService() {
119 public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
120 final Collection<DataContainerChild> children = ((ContainerNode) input).body();
121 final Module module = SCHEMA_CONTEXT.findModules(type.getNamespace()).stream()
122 .findFirst().orElse(null);
123 final RpcDefinition rpcDefinition = getRpcDefinitionFromModule(module, module.getNamespace(),
124 type.getLocalName());
125 final OutputSchemaNode outputSchemaNode = rpcDefinition.getOutput();
126 final ContainerNode node = ImmutableContainerNodeBuilder.create()
127 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(outputSchemaNode.getQName()))
128 .withValue(children).build();
130 return immediateFluentFuture(new DefaultDOMRpcResult(node));
134 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
135 return NoOpListenerRegistration.of(lsnr);
139 private static EffectiveModelContext SCHEMA_CONTEXT = null;
140 private CurrentSchemaContext currentSchemaContext = null;
143 private DOMSchemaService schemaService;
145 private EffectiveModelContextListener listener;
147 private ListenerRegistration<?> registration;
149 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
152 public static void beforeClass() {
153 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yang/mdsal-netconf-rpc-test.yang");
157 public static void afterClass() {
158 SCHEMA_CONTEXT = null;
162 public void setUp() throws Exception {
163 doNothing().when(registration).close();
164 doAnswer(invocationOnMock -> {
165 ((EffectiveModelContextListener) invocationOnMock.getArguments()[0]).onModelContextUpdated(SCHEMA_CONTEXT);
167 }).when(schemaService).registerSchemaContextListener(any(EffectiveModelContextListener.class));
169 XMLUnit.setIgnoreWhitespace(true);
170 XMLUnit.setIgnoreAttributeOrder(true);
172 doAnswer(invocationOnMock -> {
173 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
174 final YangTextSchemaSource yangTextSchemaSource =
175 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
176 return immediateFluentFuture(yangTextSchemaSource);
177 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
179 this.currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceProvider);
183 public void tearDown() {
184 currentSchemaContext.close();
188 public void testVoidOutputRpc() throws Exception {
189 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
191 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-output.xml");
192 final HandlingPriority priority = rpc.canHandle(rpcDocument);
193 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
195 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
197 verifyResponse(response, RPC_REPLY_OK);
201 public void testSuccesfullNonVoidInvocation() throws Exception {
202 final RuntimeRpc rpc = new RuntimeRpc(
203 SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
205 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
206 final HandlingPriority priority = rpc.canHandle(rpcDocument);
207 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
209 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
210 verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid-control.xml"));
214 public void testSuccesfullContainerInvocation() throws Exception {
215 final RuntimeRpc rpc = new RuntimeRpc(
216 SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
218 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container.xml");
219 final HandlingPriority priority = rpc.canHandle(rpcDocument);
220 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
222 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
223 verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container-control.xml"));
227 public void testFailedInvocation() throws Exception {
228 final RuntimeRpc rpc = new RuntimeRpc(
229 SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_FAILED_INVOCATION);
231 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
232 final HandlingPriority priority = rpc.canHandle(rpcDocument);
233 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
235 final DocumentedException e = assertThrows(DocumentedException.class,
236 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
238 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
239 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_FAILED);
240 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
244 public void testVoidInputOutputRpc() throws Exception {
245 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
247 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-input-output.xml");
248 final HandlingPriority priority = rpc.canHandle(rpcDocument);
249 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
251 final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
253 verifyResponse(response, RPC_REPLY_OK);
257 public void testBadNamespaceInRpc() throws Exception {
258 final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
259 final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-bad-namespace.xml");
261 final DocumentedException e = assertThrows(DocumentedException.class,
262 () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
264 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
265 assertEquals(e.getErrorTag(), ErrorTag.BAD_ELEMENT);
266 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
269 private static void verifyResponse(final Document response, final Document template) {
270 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
271 dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
272 //we care about order so response has to be identical
273 assertTrue(dd.identical());
276 private static RpcDefinition getRpcDefinitionFromModule(final Module module, final XMLNamespace namespaceURI,
278 for (final RpcDefinition rpcDef : module.getRpcs()) {
279 if (rpcDef.getQName().getNamespace().equals(namespaceURI)
280 && rpcDef.getQName().getLocalName().equals(name)) {