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 com.google.common.collect.Lists;
24 import com.google.common.collect.Sets;
25 import java.io.FileNotFoundException;
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Optional;
36 import javax.ws.rs.core.MultivaluedHashMap;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.UriBuilder;
39 import javax.ws.rs.core.UriInfo;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
45 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
46 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
47 import org.opendaylight.mdsal.dom.api.DOMRpcService;
48 import org.opendaylight.netconf.sal.rest.api.Draft02;
49 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
50 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
51 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
52 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
53 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
54 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
55 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
56 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
57 import org.opendaylight.restconf.common.errors.RestconfError;
58 import org.opendaylight.yangtools.yang.common.Empty;
59 import org.opendaylight.yangtools.yang.common.ErrorTag;
60 import org.opendaylight.yangtools.yang.common.ErrorType;
61 import org.opendaylight.yangtools.yang.common.QName;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
63 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
65 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
69 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
70 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
71 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
72 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
73 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.Module;
76 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
77 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
78 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
79 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
82 * See {@link InvokeRpcMethodTest}.
84 public class RestconfImplTest {
86 private static EffectiveModelContext schemaContext;
88 private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
89 private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
90 private final RestconfImpl restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
93 public static void init() throws FileNotFoundException, ReactorException {
94 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/modules/restconf-module-testing");
98 public static void cleanUp() {
99 WebSocketServer.destroyInstance(); // NETCONF-604
103 public void binaryKeyTest() {
104 final List<Byte> al = new ArrayList<>();
106 binaryKeyTest(al, al);
109 private static void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
111 final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");
113 final Map<QName, Object> uriKeyValues = new HashMap<>();
114 uriKeyValues.put(keyDef, al.toArray());
116 final MapEntryNode payload = mock(MapEntryNode.class);
117 final NodeIdentifierWithPredicates nodeIdWithPred =
118 NodeIdentifierWithPredicates.of(keyDef, keyDef, al2.toArray());
119 when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
121 final List<QName> keyDefinitions = new ArrayList<>();
122 keyDefinitions.add(keyDef);
123 RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
127 public void binaryKeyFailTest() {
128 final List<Byte> al = new ArrayList<>();
130 final List<Byte> al2 = new ArrayList<>();
132 binaryKeyTest(al, al2);
133 } catch (final RestconfDocumentedException e) {
134 final RestconfError err = e.getErrors().iterator().next();
135 assertEquals(ErrorType.PROTOCOL, err.getErrorType());
136 assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
140 @SuppressWarnings("unchecked")
142 public void testExample() throws FileNotFoundException, ParseException {
143 @SuppressWarnings("rawtypes")
144 final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
145 when(brokerFacade.readOperationalData(isNull())).thenReturn(normalizedNodeData);
146 assertEquals(normalizedNodeData,
147 brokerFacade.readOperationalData(null));
151 public void testRpcForMountpoint() throws Exception {
152 final UriInfo uriInfo = mock(UriInfo.class);
153 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
155 final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
156 final InstanceIdentifierContext<?> iiCtx = mock(InstanceIdentifierContext.class);
157 doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
158 final SchemaNode schemaNode = mock(SchemaNode.class);
159 doReturn(schemaNode).when(iiCtx).getSchemaNode();
160 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
161 doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
163 final DOMMountPoint mount = mock(DOMMountPoint.class);
164 doReturn(mount).when(iiCtx).getMountPoint();
165 final DOMRpcService rpcService = mock(DOMRpcService.class);
166 doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
167 doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService)
168 .invokeRpc(any(QName.class), any(NormalizedNode.class));
169 this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
170 this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
171 verify(rpcService, times(2)).invokeRpc(any(QName.class), any());
175 * Create notification stream for toaster module.
178 public void createNotificationStreamTest() {
179 final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
180 final InstanceIdentifierContext<?> iiCtx = mock(InstanceIdentifierContext.class);
181 doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
183 final SchemaNode schemaNode = mock(SchemaNode.class,
184 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
185 doReturn(schemaNode).when(iiCtx).getSchemaNode();
186 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
188 doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
189 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
190 doReturn(null).when(iiCtx).getMountPoint();
192 final Set<DataContainerChild> children = new HashSet<>();
193 final DataContainerChild child = mock(DataContainerChild.class,
194 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
196 final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
197 when(entryNode.body()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
198 when(((LeafSetNode) child).body()).thenReturn(Sets.newHashSet(entryNode));
201 final NormalizedNode normalizedNode = mock(NormalizedNode.class,
202 Mockito.withSettings().extraInterfaces(ContainerNode.class));
203 doReturn(normalizedNode).when(payload).getData();
204 doReturn(children).when(normalizedNode).body();
206 // register notification
207 final NormalizedNodeContext context = this.restconfImpl
208 .invokeRpc("sal-remote:create-notification-stream", payload, null);
209 assertNotNull(context);
213 * Tests stream entry node.
216 public void toStreamEntryNodeTest() {
217 final Module restconfModule = this.controllerContext.getRestconfModule();
218 final DataSchemaNode streamSchemaNode = this.controllerContext
219 .getRestconfModuleRestConfSchemaNode(restconfModule, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
220 final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamSchemaNode;
221 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues =
222 SchemaAwareBuilders.mapEntryBuilder(listStreamSchemaNode);
223 List<DataSchemaNode> instanceDataChildrenByName =
224 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "name");
225 final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
226 streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
230 instanceDataChildrenByName =
231 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "description");
232 final DataSchemaNode descriptionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
233 streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
234 .withValue("DESCRIPTION_PLACEHOLDER")
237 instanceDataChildrenByName =
238 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-support");
239 final DataSchemaNode replaySupportSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
240 streamNodeValues.withChild(
241 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replaySupportSchemaNode).withValue(Boolean.TRUE).build());
243 instanceDataChildrenByName =
244 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-log-creation-time");
245 final DataSchemaNode replayLogCreationTimeSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
246 streamNodeValues.withChild(
247 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replayLogCreationTimeSchemaNode).withValue("").build());
248 instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "events");
249 final DataSchemaNode eventsSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
250 streamNodeValues.withChild(
251 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) eventsSchemaNode).withValue(Empty.value()).build());
252 assertNotNull(streamNodeValues.build());
256 * Subscribe for notification stream of toaster module.
259 public void subscribeToNotificationStreamTest() throws Exception {
260 final String identifier = "create-notification-stream/toaster:toastDone";
262 // register test notification stream
263 final SchemaPath path = SchemaPath.create(
264 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
265 Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML", controllerContext);
267 final UriInfo uriInfo = mock(UriInfo.class);
268 final UriBuilder uriBuilder = mock(UriBuilder.class);
269 when(uriBuilder.port(8181)).thenReturn(uriBuilder);
270 when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
271 when(uriBuilder.build()).thenReturn(new URI(""));
272 when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
273 when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
274 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
275 final Set<Entry<String, List<String>>> set = new HashSet<>();
276 when(map.entrySet()).thenReturn(set);
277 when(uriInfo.getQueryParameters()).thenReturn(map);
279 // subscribe to stream and verify response
280 final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
282 // remove test notification stream
283 Notificator.removeAllListeners();