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.Matchers.any;
13 import static org.mockito.Matchers.anyBoolean;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Sets;
22 import com.google.common.util.concurrent.Futures;
23 import java.io.FileNotFoundException;
25 import java.text.ParseException;
27 import javax.ws.rs.core.MultivaluedHashMap;
28 import javax.ws.rs.core.UriBuilder;
29 import javax.ws.rs.core.UriInfo;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
35 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
37 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
38 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
39 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
40 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
41 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
42 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
47 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.Module;
51 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
52 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
58 * @See {@link InvokeRpcMethodTest}
61 public class RestconfImplTest {
63 private RestconfImpl restconfImpl = null;
64 private static ControllerContext controllerContext = null;
67 public static void init() throws FileNotFoundException, ReactorException {
68 final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
70 final Set<Module> allModules = schemaContext.getModules();
71 assertNotNull(allModules);
73 controllerContext = ControllerContext.getInstance();
74 controllerContext.setSchemas(schemaContext);
78 public void initMethod() {
79 this.restconfImpl = RestconfImpl.getInstance();
80 this.restconfImpl.setControllerContext(controllerContext);
83 @SuppressWarnings("unchecked")
85 public void testExample() throws FileNotFoundException, ParseException {
86 @SuppressWarnings("rawtypes")
88 NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
89 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
90 when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
91 assertEquals(normalizedNodeData,
92 brokerFacade.readOperationalData(null));
96 public void testRpcForMountpoint() throws Exception {
97 final UriInfo uriInfo = mock(UriInfo.class);
98 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
100 final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
101 final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
102 doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
103 final SchemaNode schemaNode = mock(SchemaNode.class);
104 doReturn(schemaNode).when(iiCtx).getSchemaNode();
105 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
106 doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
108 final DOMMountPoint mount = mock(DOMMountPoint.class);
109 doReturn(mount).when(iiCtx).getMountPoint();
110 final DOMRpcService rpcService = mock(DOMRpcService.class);
111 doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
112 doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService)
113 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
114 this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
115 this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
116 verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
120 * Create notification stream for toaster module
123 public void createNotificationStreamTest() {
124 final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
125 final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
126 doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
128 final SchemaNode schemaNode = mock(SchemaNode.class,
129 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
130 doReturn(schemaNode).when(iiCtx).getSchemaNode();
131 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
133 doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
134 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
135 doReturn(null).when(iiCtx).getMountPoint();
137 final Set<DataContainerChild<?, ?>> children = Sets.newHashSet();
138 final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
139 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
141 final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
142 when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
143 when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
146 final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
147 Mockito.withSettings().extraInterfaces(ContainerNode.class));
148 doReturn(normalizedNode).when(payload).getData();
149 doReturn(children).when(normalizedNode).getValue();
151 // register notification
152 final NormalizedNodeContext context = this.restconfImpl
153 .invokeRpc("sal-remote:create-notification-stream", payload, null);
154 assertNotNull(context);
158 * Subscribe for notification stream of toaster module
161 public void subscribeToNotificationStreamTest() throws Exception {
162 final String identifier = "create-notification-stream/toaster:toastDone";
164 // register test notification stream
165 final SchemaPath path = SchemaPath.create(
166 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
167 Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML");
169 final UriInfo uriInfo = mock(UriInfo.class);
170 final UriBuilder uriBuilder = mock(UriBuilder.class);
171 when(uriBuilder.port(8181)).thenReturn(uriBuilder);
172 when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
173 when(uriBuilder.build()).thenReturn(new URI(""));
174 when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
175 when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
177 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
178 this.restconfImpl.setBroker(brokerFacade);
180 // subscribe to stream and verify response
181 final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
183 // remove test notification stream
184 Notificator.removeAllListeners();