summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java
blob: 19386a12c4ad2c6db46d89c242d934f1707e7969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (c) 2013, 2015 Brocade Communication Systems, Inc., Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.sal.restconf.impl.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;

import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
import org.opendaylight.mdsal.dom.api.DOMRpcResult;
import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
import org.opendaylight.restconf.common.ErrorTags;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.common.errors.RestconfError;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.common.ErrorType;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
import org.opendaylight.yangtools.yang.model.api.ContainerLike;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;

public class InvokeRpcMethodTest {

    private static UriInfo uriInfo;
    private static EffectiveModelContext schemaContext;

    private final RestconfImpl restconfImpl;
    private final ControllerContext controllerContext;
    private final BrokerFacade brokerFacade = mock(BrokerFacade.class);

    public InvokeRpcMethodTest() {
        controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
        restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
    }

    @BeforeClass
    public static void init() throws FileNotFoundException, ReactorException {
        schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/invoke-rpc");
        final Collection<? extends Module> allModules = schemaContext.getModules();
        assertNotNull(allModules);
        final Module module = TestUtils.resolveModule("invoke-rpc-module", allModules);
        assertNotNull(module);

        uriInfo = mock(UriInfo.class);
        final MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
        map.put("prettyPrint", List.of("true"));
        doReturn(map).when(uriInfo).getQueryParameters(any(Boolean.class));
    }

    /**
     * Test method invokeRpc in RestconfImpl class tests if composite node as input parameter of method invokeRpc
     * (second argument) is wrapped to parent composite node which has QName equals to QName of rpc (resolved from
     * string - first argument).
     */
    @Test
    public void invokeRpcMethodTest() {
        controllerContext.findModuleNameByNamespace(XMLNamespace.of("invoke:rpc:module"));

        final NormalizedNodeContext payload = prepareDomPayload();

        final NormalizedNodeContext rpcResponse =
                restconfImpl.invokeRpc("invoke-rpc-module:rpc-test", payload, uriInfo);
        assertNotNull(rpcResponse);
        assertNull(rpcResponse.getData());

    }

    private NormalizedNodeContext prepareDomPayload() {
        final EffectiveModelContext schema = controllerContext.getGlobalSchema();
        final Module rpcModule = schema.findModules("invoke-rpc-module").iterator().next();
        assertNotNull(rpcModule);
        final QName rpcQName = QName.create(rpcModule.getQNameModule(), "rpc-test");
        RpcDefinition rpcSchemaNode = null;
        for (final RpcDefinition rpc : rpcModule.getRpcs()) {
            if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
                rpcSchemaNode = rpc;
                break;
            }
        }
        assertNotNull(rpcSchemaNode);
        final InputSchemaNode rpcInputSchemaNode = rpcSchemaNode.getInput();
        final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container =
                SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);

        final QName contQName = QName.create(rpcModule.getQNameModule(), "cont");
        final DataSchemaNode contSchemaNode = rpcInputSchemaNode.getDataChildByName(contQName);
        assertTrue(contSchemaNode instanceof ContainerSchemaNode);
        final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contNode =
                SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) contSchemaNode);

        final QName lfQName = QName.create(rpcModule.getQNameModule(), "lf");
        final DataSchemaNode lfSchemaNode = ((ContainerSchemaNode) contSchemaNode).getDataChildByName(lfQName);
        assertTrue(lfSchemaNode instanceof LeafSchemaNode);
        final LeafNode<Object> lfNode =
                SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode).withValue("any value").build();
        contNode.withChild(lfNode);
        container.withChild(contNode.build());

        return new NormalizedNodeContext(InstanceIdentifierContext.ofRpcInput(schema, rpcSchemaNode, null),
            container.build());
    }

    @Test
    public void testInvokeRpcWithNoPayloadRpc_FailNoErrors() {
        final QName qname = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast");

        doReturn(immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException("testExeption")))
            .when(brokerFacade).invokeRpc(eq(qname), any());

        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
            () -> restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
        verifyRestconfDocumentedException(ex, 0, ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED,
            Optional.empty(), Optional.empty());
    }

    void verifyRestconfDocumentedException(final RestconfDocumentedException restDocumentedException, final int index,
            final ErrorType expErrorType, final ErrorTag expErrorTag, final Optional<String> expErrorMsg,
            final Optional<String> expAppTag) {

        final List<RestconfError> errors = restDocumentedException.getErrors();
        assertTrue("RestconfError not found at index " + index, errors.size() > index);

        RestconfError actual = errors.get(index);

        assertEquals("getErrorType", expErrorType, actual.getErrorType());
        assertEquals("getErrorTag", expErrorTag, actual.getErrorTag());
        assertNotNull("getErrorMessage is null", actual.getErrorMessage());

        if (expErrorMsg.isPresent()) {
            assertEquals("getErrorMessage", expErrorMsg.get(), actual.getErrorMessage());
        }

        if (expAppTag.isPresent()) {
            assertEquals("getErrorAppTag", expAppTag.get(), actual.getErrorAppTag());
        }
    }

    @Test
    public void testInvokeRpcWithNoPayloadRpc_FailWithRpcError() {
        final List<RpcError> rpcErrors = List.of(
                RpcResultBuilder.newError(ErrorType.TRANSPORT, new ErrorTag("bogusTag"), "foo"),
                RpcResultBuilder.newWarning(ErrorType.RPC, ErrorTag.IN_USE, "bar", "app-tag", null, null));

        final DOMRpcResult result = new DefaultDOMRpcResult(rpcErrors);
        final QName path = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast");
        doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(path), any());

        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
            () -> restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));

        // We are performing pass-through here of error-tag, hence the tag remains as specified, but we want to make
        // sure the HTTP status remains the same as
        final ErrorTag bogus = new ErrorTag("bogusTag");
        verifyRestconfDocumentedException(ex, 0, ErrorType.TRANSPORT, bogus, Optional.of("foo"), Optional.empty());
        assertEquals(ErrorTags.statusOf(ErrorTag.OPERATION_FAILED), ErrorTags.statusOf(bogus));

        verifyRestconfDocumentedException(ex, 1, ErrorType.RPC, ErrorTag.IN_USE, Optional.of("bar"),
            Optional.of("app-tag"));
    }

    @Test
    public void testInvokeRpcWithNoPayload_Success() {
        final NormalizedNode resultObj = null;
        final DOMRpcResult expResult = new DefaultDOMRpcResult(resultObj);

        final QName qname = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast");

        doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(qname), any());

        final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo);
        assertNotNull(output);
        assertEquals(null, output.getData());
        // additional validation in the fact that the restconfImpl does not
        // throw an exception.
    }

    @Test
    public void testInvokeRpcWithEmptyOutput() {
        final ContainerNode resultObj = mock(ContainerNode.class);
        doReturn(Set.of()).when(resultObj).body();
        doCallRealMethod().when(resultObj).isEmpty();
        final DOMRpcResult expResult = new DefaultDOMRpcResult(resultObj);

        final QName qname = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast");
        doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(qname), any());

        WebApplicationException exceptionToBeThrown = assertThrows(WebApplicationException.class,
            () -> restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
        assertEquals(Response.Status.NO_CONTENT.getStatusCode(), exceptionToBeThrown.getResponse().getStatus());
    }

    @Test
    public void testInvokeRpcMethodWithBadMethodName() {
        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
            () -> restconfImpl.invokeRpc("toaster:bad-method", null, uriInfo));
        verifyRestconfDocumentedException(ex, 0, ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT,
            Optional.empty(), Optional.empty());
    }

    @Test
    @Ignore
    public void testInvokeRpcMethodWithInput() {
        final DOMRpcResult expResult = mock(DOMRpcResult.class);
        final QName path = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast");

        final Module rpcModule = schemaContext.findModules("toaster").iterator().next();
        assertNotNull(rpcModule);
        final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast");

        RpcDefinition rpcDef = null;
        for (final RpcDefinition rpc : rpcModule.getRpcs()) {
            if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
                rpcDef = rpc;
                break;
            }
        }

        assertNotNull(rpcDef);

        final NormalizedNodeContext payload = new NormalizedNodeContext(
                InstanceIdentifierContext.ofLocalRpcInput(schemaContext, rpcDef),
                SchemaAwareBuilders.containerBuilder(rpcDef.getInput()).build());

        doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(path), any(NormalizedNode.class));

        final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
        assertNotNull(output);
        assertEquals(null, output.getData());
        // additional validation in the fact that the restconfImpl does not
        // throw an exception.
    }

    @Test
    public void testThrowExceptionWhenSlashInModuleName() {
        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
            () -> restconfImpl.invokeRpc("toaster/slash", null, uriInfo));
        verifyRestconfDocumentedException(ex, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
            Optional.empty(), Optional.empty());
    }

    @Test
    public void testInvokeRpcWithNoPayloadWithOutput_Success() {
        final SchemaContext schema = controllerContext.getGlobalSchema();
        final Module rpcModule = schema.findModules("toaster").iterator().next();
        assertNotNull(rpcModule);
        final QName rpcQName = QName.create(rpcModule.getQNameModule(), "testOutput");
        final QName rpcOutputQName = QName.create(rpcModule.getQNameModule(),"output");

        RpcDefinition rpcDef = null;
        ContainerLike rpcOutputSchemaNode = null;
        for (final RpcDefinition rpc : rpcModule.getRpcs()) {
            if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
                rpcOutputSchemaNode = rpc.getOutput();
                rpcDef = rpc;
                break;
            }
        }
        assertNotNull(rpcDef);
        assertNotNull(rpcOutputSchemaNode);
        final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder =
                SchemaAwareBuilders.containerBuilder(rpcOutputSchemaNode);
        final DataSchemaNode leafSchema = rpcOutputSchemaNode
                .getDataChildByName(QName.create(rpcModule.getQNameModule(), "textOut"));
        assertTrue(leafSchema instanceof LeafSchemaNode);
        final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
                SchemaAwareBuilders.leafBuilder((LeafSchemaNode) leafSchema);
        leafBuilder.withValue("brm");
        containerBuilder.withChild(leafBuilder.build());
        final ContainerNode container = containerBuilder.build();

        final DOMRpcResult result = new DefaultDOMRpcResult(container);

        doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(rpcDef.getQName()), any());

        final NormalizedNodeContext output = restconfImpl.invokeRpc("toaster:testOutput", null, uriInfo);
        assertNotNull(output);
        assertNotNull(output.getData());
        assertSame(container, output.getData());
        assertNotNull(output.getInstanceIdentifierContext());
        assertNotNull(output.getInstanceIdentifierContext().getSchemaContext());
    }

    /**
     * Tests calling of RestConfImpl method invokeRpc. In the method there is searched rpc in remote schema context.
     * This rpc is then executed.
     * I wasn't able to simulate calling of rpc on remote device therefore this testing method raise method when rpc is
     * invoked.
     */
    @Test
    public void testMountedRpcCallNoPayload_Success() throws Exception {
        // FIXME find how to use mockito for it
    }
}