2 * Copyright (c) 2014 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.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyBoolean;
14 import static org.mockito.ArgumentMatchers.isNull;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22 import com.google.common.collect.Iterables;
23 import java.io.FileNotFoundException;
25 import java.text.ParseException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.Optional;
34 import javax.ws.rs.core.MultivaluedHashMap;
35 import javax.ws.rs.core.MultivaluedMap;
36 import javax.ws.rs.core.UriBuilder;
37 import javax.ws.rs.core.UriInfo;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
42 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
43 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
44 import org.opendaylight.mdsal.dom.api.DOMRpcService;
45 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
46 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
47 import org.opendaylight.netconf.sal.rest.api.Draft02;
48 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
49 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
50 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
51 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
52 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
53 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
54 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
55 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
56 import org.opendaylight.restconf.common.errors.RestconfError;
57 import org.opendaylight.yangtools.yang.common.Empty;
58 import org.opendaylight.yangtools.yang.common.ErrorTag;
59 import org.opendaylight.yangtools.yang.common.ErrorType;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
64 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
69 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
70 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
71 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
72 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.Module;
75 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
76 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
77 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
78 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
81 * See {@link InvokeRpcMethodTest}.
83 public class RestconfImplTest {
85 private static EffectiveModelContext schemaContext;
87 private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
88 private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
89 private final RestconfImpl restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
92 public static void init() throws FileNotFoundException, ReactorException {
93 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/modules/restconf-module-testing");
97 public static void cleanUp() {
98 WebSocketServer.destroyInstance(); // NETCONF-604
102 public void binaryKeyTest() {
103 final List<Byte> al = new ArrayList<>();
105 binaryKeyTest(al, al);
108 private static void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
110 final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");
112 final Map<QName, Object> uriKeyValues = new HashMap<>();
113 uriKeyValues.put(keyDef, al.toArray());
115 final MapEntryNode payload = mock(MapEntryNode.class);
116 final NodeIdentifierWithPredicates nodeIdWithPred =
117 NodeIdentifierWithPredicates.of(keyDef, keyDef, al2.toArray());
118 when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
120 final List<QName> keyDefinitions = new ArrayList<>();
121 keyDefinitions.add(keyDef);
122 RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
126 public void binaryKeyFailTest() {
127 final List<Byte> al = new ArrayList<>();
129 final List<Byte> al2 = new ArrayList<>();
131 binaryKeyTest(al, al2);
132 } catch (final RestconfDocumentedException e) {
133 final RestconfError err = e.getErrors().iterator().next();
134 assertEquals(ErrorType.PROTOCOL, err.getErrorType());
135 assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
140 public void testExample() throws FileNotFoundException, ParseException {
141 final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
142 when(brokerFacade.readOperationalData(isNull())).thenReturn(normalizedNodeData);
143 assertEquals(normalizedNodeData,
144 brokerFacade.readOperationalData(null));
148 public void testRpcForMountpoint() throws Exception {
149 final UriInfo uriInfo = mock(UriInfo.class);
150 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
152 final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
153 final RpcDefinition schemaNode = mock(RpcDefinition.class);
154 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
155 doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
157 final DOMMountPoint mount = mock(DOMMountPoint.class);
158 doReturn(Optional.of(FixedDOMSchemaService.of(schemaContext))).when(mount).getService(DOMSchemaService.class);
160 doReturn(InstanceIdentifierContext.ofMountPointRpc(mount, schemaContext, schemaNode))
161 .when(ctx).getInstanceIdentifierContext();
163 final DOMRpcService rpcService = mock(DOMRpcService.class);
164 doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
165 doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService)
166 .invokeRpc(any(QName.class), any(NormalizedNode.class));
167 restconfImpl.invokeRpc("randomId", ctx, uriInfo);
168 restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
169 verify(rpcService, times(2)).invokeRpc(any(QName.class), any());
173 * Create notification stream for toaster module.
176 public void createNotificationStreamTest() {
177 final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
179 final RpcDefinition schemaNode = mock(RpcDefinition.class);
180 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
181 doReturn(InstanceIdentifierContext.ofLocalRpc(schemaContext, schemaNode)).when(payload)
182 .getInstanceIdentifierContext();
184 doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
185 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
187 final Set<DataContainerChild> children = new HashSet<>();
188 final LeafSetNode child = mock(LeafSetNode.class);
190 final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
191 when(entryNode.body()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
192 when(child.body()).thenReturn(Set.of(entryNode));
195 final ContainerNode normalizedNode = mock(ContainerNode.class);
196 doReturn(normalizedNode).when(payload).getData();
197 doReturn(children).when(normalizedNode).body();
199 // register notification
200 final NormalizedNodeContext context = restconfImpl
201 .invokeRpc("sal-remote:create-notification-stream", payload, null);
202 assertNotNull(context);
206 * Tests stream entry node.
209 public void toStreamEntryNodeTest() {
210 final Module restconfModule = controllerContext.getRestconfModule();
211 final DataSchemaNode streamSchemaNode = controllerContext
212 .getRestconfModuleRestConfSchemaNode(restconfModule, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
213 final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamSchemaNode;
214 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues =
215 SchemaAwareBuilders.mapEntryBuilder(listStreamSchemaNode);
216 List<DataSchemaNode> instanceDataChildrenByName =
217 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "name");
218 final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
219 streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
223 instanceDataChildrenByName =
224 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "description");
225 final DataSchemaNode descriptionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
226 streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
227 .withValue("DESCRIPTION_PLACEHOLDER")
230 instanceDataChildrenByName =
231 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-support");
232 final DataSchemaNode replaySupportSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
233 streamNodeValues.withChild(
234 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replaySupportSchemaNode).withValue(Boolean.TRUE).build());
236 instanceDataChildrenByName =
237 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-log-creation-time");
238 final DataSchemaNode replayLogCreationTimeSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
239 streamNodeValues.withChild(
240 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replayLogCreationTimeSchemaNode).withValue("").build());
241 instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "events");
242 final DataSchemaNode eventsSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
243 streamNodeValues.withChild(
244 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) eventsSchemaNode).withValue(Empty.value()).build());
245 assertNotNull(streamNodeValues.build());
249 * Subscribe for notification stream of toaster module.
252 public void subscribeToNotificationStreamTest() throws Exception {
253 final String identifier = "create-notification-stream/toaster:toastDone";
255 // register test notification stream
256 final Absolute path = Absolute.of(
257 QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
258 Notificator.createNotificationListener(List.of(path), identifier, "XML", controllerContext);
260 final UriInfo uriInfo = mock(UriInfo.class);
261 final UriBuilder uriBuilder = mock(UriBuilder.class);
262 when(uriBuilder.port(8181)).thenReturn(uriBuilder);
263 when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
264 when(uriBuilder.build()).thenReturn(new URI(""));
265 when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
266 when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
267 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
268 final Set<Entry<String, List<String>>> set = new HashSet<>();
269 when(map.entrySet()).thenReturn(set);
270 when(uriInfo.getQueryParameters()).thenReturn(map);
272 // subscribe to stream and verify response
273 final NormalizedNodeContext response = restconfImpl.subscribeToStream(identifier, uriInfo);
275 // remove test notification stream
276 Notificator.removeAllListeners();