summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java
blob: 3174908fb88747c53900896fc8025db7b21e05df (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
/*
 * Copyright (c) 2014 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.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;

import java.io.FileNotFoundException;
import java.net.URI;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.mdsal.dom.api.DOMRpcResult;
import org.opendaylight.mdsal.dom.api.DOMRpcService;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
import org.opendaylight.netconf.sal.rest.api.Draft02;
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.netconf.sal.streams.listeners.Notificator;
import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
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.Empty;
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.YangConstants;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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.impl.schema.SchemaAwareBuilders;
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.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;

/**
 * See {@link InvokeRpcMethodTest}.
 */
public class RestconfImplTest {

    private static EffectiveModelContext schemaContext;

    private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
    private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
    private final RestconfImpl restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);

    @BeforeClass
    public static void init() throws FileNotFoundException, ReactorException {
        schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/modules/restconf-module-testing");
    }

    @AfterClass
    public static void cleanUp() {
        WebSocketServer.destroyInstance(); // NETCONF-604
    }

    @Test
    public void binaryKeyTest() {
        final List<Byte> al = new ArrayList<>();
        al.add((byte) 1);
        binaryKeyTest(al, al);
    }

    private static void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {

        final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");

        final Map<QName, Object> uriKeyValues = new HashMap<>();
        uriKeyValues.put(keyDef, al.toArray());

        final MapEntryNode payload = mock(MapEntryNode.class);
        final NodeIdentifierWithPredicates nodeIdWithPred =
                NodeIdentifierWithPredicates.of(keyDef, keyDef, al2.toArray());
        when(payload.getIdentifier()).thenReturn(nodeIdWithPred);

        final List<QName> keyDefinitions = new ArrayList<>();
        keyDefinitions.add(keyDef);
        RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
    }

    @Test
    public void binaryKeyFailTest() {
        final List<Byte> al = new ArrayList<>();
        al.add((byte) 1);
        final List<Byte> al2 = new ArrayList<>();
        try {
            binaryKeyTest(al, al2);
        } catch (final RestconfDocumentedException e) {
            final RestconfError err = e.getErrors().iterator().next();
            assertEquals(ErrorType.PROTOCOL, err.getErrorType());
            assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
        }
    }

    @Test
    public void testExample() throws FileNotFoundException, ParseException {
        final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
        when(brokerFacade.readOperationalData(isNull())).thenReturn(normalizedNodeData);
        assertEquals(normalizedNodeData,
                brokerFacade.readOperationalData(null));
    }

    @Test
    public void testRpcForMountpoint() throws Exception {
        final QName qname = QName.create("namespace", "2010-10-10", "localname");
        final UriInfo uriInfo = mock(UriInfo.class);
        doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());

        final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
        final RpcDefinition rpc = mock(RpcDefinition.class,
            withSettings().extraInterfaces(RpcEffectiveStatement.class));
        doReturn(qname).when(rpc).getQName();

        final InputSchemaNode input = mock(InputSchemaNode.class,
            withSettings().extraInterfaces(InputEffectiveStatement.class));
        final QName inputQName = YangConstants.operationInputQName(qname.getModule());
        doReturn(input).when(rpc).getInput();
        doReturn(inputQName).when(input).getQName();
        doReturn(Optional.of(input)).when((RpcEffectiveStatement) rpc).findSchemaTreeNode(inputQName);

        final OutputSchemaNode output = mock(OutputSchemaNode.class,
            withSettings().extraInterfaces(OutputEffectiveStatement.class));
        final QName outputQName = YangConstants.operationInputQName(qname.getModule());
        doReturn(output).when(rpc).getOutput();
        doReturn(outputQName).when(output).getQName();
        doReturn(Optional.of(output)).when((RpcEffectiveStatement) rpc).findSchemaTreeNode(outputQName);

        final EffectiveModelContext mountContext = mock(EffectiveModelContext.class);
        final ModuleEffectiveStatement mountModule = mock(ModuleEffectiveStatement.class);
        doReturn(Map.of(qname.getModule(), mountModule)).when(mountContext).getModuleStatements();
        doReturn(Optional.of(rpc)).when(mountModule).findSchemaTreeNode(qname);

        final DOMMountPoint mount = mock(DOMMountPoint.class);
        doReturn(Optional.of(FixedDOMSchemaService.of(mountContext))).when(mount).getService(DOMSchemaService.class);

        doReturn(InstanceIdentifierContext.ofRpcInput(mountContext, rpc, mount))
            .when(ctx).getInstanceIdentifierContext();

        final DOMRpcService rpcService = mock(DOMRpcService.class);
        doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
        doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService)
                .invokeRpc(any(QName.class), any(NormalizedNode.class));
        restconfImpl.invokeRpc("randomId", ctx, uriInfo);
        restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
        verify(rpcService, times(2)).invokeRpc(any(QName.class), any());
    }

    /**
     * Create notification stream for toaster module.
     */
    @Test
    public void createNotificationStreamTest() {
        final QName rpcQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
            "2014-01-14", "create-notification-stream");

        final RpcDefinition schemaNode = schemaContext.getOperations().stream()
            .filter(rpc -> rpc.getQName().equals(rpcQName))
            .findFirst()
            .orElseThrow();

        final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
        doReturn(InstanceIdentifierContext.ofRpcInput(schemaContext, schemaNode, null)).when(payload)
                .getInstanceIdentifierContext();

        final Set<DataContainerChild> children = new HashSet<>();
        final LeafSetNode child = mock(LeafSetNode.class);

        final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
        when(entryNode.body()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
        when(child.body()).thenReturn(Set.of(entryNode));
        children.add(child);

        final ContainerNode normalizedNode = mock(ContainerNode.class);
        doReturn(normalizedNode).when(payload).getData();
        doReturn(children).when(normalizedNode).body();

        // register notification
        final NormalizedNodeContext context = restconfImpl
                .invokeRpc("sal-remote:create-notification-stream", payload, null);
        assertNotNull(context);
    }

    /**
     * Tests stream entry node.
     */
    @Test
    public void toStreamEntryNodeTest() {
        final Module restconfModule = controllerContext.getRestconfModule();
        final DataSchemaNode streamSchemaNode = controllerContext
                .getRestconfModuleRestConfSchemaNode(restconfModule, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
        final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamSchemaNode;
        final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues =
            SchemaAwareBuilders.mapEntryBuilder(listStreamSchemaNode);
        var instanceDataChildrenByName =
                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "name");
        final DataSchemaNode nameSchemaNode = instanceDataChildrenByName.get(0).child;
        streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
            .withValue("")
            .build());

        instanceDataChildrenByName =
                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "description");
        final DataSchemaNode descriptionSchemaNode = instanceDataChildrenByName.get(0).child;
        streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
            .withValue("DESCRIPTION_PLACEHOLDER")
            .build());

        instanceDataChildrenByName =
                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-support");
        final DataSchemaNode replaySupportSchemaNode = instanceDataChildrenByName.get(0).child;
        streamNodeValues.withChild(
            SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replaySupportSchemaNode).withValue(Boolean.TRUE).build());

        instanceDataChildrenByName =
                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-log-creation-time");
        final DataSchemaNode replayLogCreationTimeSchemaNode = instanceDataChildrenByName.get(0).child;
        streamNodeValues.withChild(
            SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replayLogCreationTimeSchemaNode).withValue("").build());
        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "events");
        final DataSchemaNode eventsSchemaNode = instanceDataChildrenByName.get(0).child;
        streamNodeValues.withChild(
            SchemaAwareBuilders.leafBuilder((LeafSchemaNode) eventsSchemaNode).withValue(Empty.value()).build());
        assertNotNull(streamNodeValues.build());
    }

    /**
     * Subscribe for notification stream of toaster module.
     */
    @Test
    public void subscribeToNotificationStreamTest() throws Exception {
        final String identifier = "create-notification-stream/toaster:toastDone";

        // register test notification stream
        Notificator.createNotificationListener(
            List.of(Absolute.of(QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"))),
            identifier, "XML", controllerContext);

        final UriInfo uriInfo = mock(UriInfo.class);
        final UriBuilder uriBuilder = mock(UriBuilder.class);
        when(uriBuilder.port(8181)).thenReturn(uriBuilder);
        when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
        when(uriBuilder.build()).thenReturn(new URI(""));
        when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
        when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
        final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
        final Set<Entry<String, List<String>>> set = new HashSet<>();
        when(map.entrySet()).thenReturn(set);
        when(uriInfo.getQueryParameters()).thenReturn(map);

        // subscribe to stream and verify response
        final NormalizedNodeContext response = restconfImpl.subscribeToStream(identifier, uriInfo);

        // remove test notification stream
        Notificator.removeAllListeners();
    }
}