Merge "Bug 6198 - Use sal-netconf-connector to connet device costs too much time"
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
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
20 import com.google.common.base.Optional;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Sets;
23 import com.google.common.util.concurrent.Futures;
24 import java.io.FileNotFoundException;
25 import java.net.URI;
26 import java.text.ParseException;
27 import java.util.Set;
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.UriBuilder;
31 import javax.ws.rs.core.UriInfo;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
37 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
38 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
39 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
40 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
41 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
42 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
43 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
44 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
49 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.model.api.Module;
53 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
56 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
57 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
58
59 /**
60  * @See {@link InvokeRpcMethodTest}
61  *
62  */
63 public class RestconfImplTest {
64
65     private RestconfImpl restconfImpl = null;
66     private static ControllerContext controllerContext = null;
67
68     @BeforeClass
69     public static void init() throws FileNotFoundException, ReactorException {
70         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
71
72         final Set<Module> allModules = schemaContext.getModules();
73         assertNotNull(allModules);
74
75         controllerContext = ControllerContext.getInstance();
76         controllerContext.setSchemas(schemaContext);
77     }
78
79     @Before
80     public void initMethod() {
81         this.restconfImpl = RestconfImpl.getInstance();
82         this.restconfImpl.setControllerContext(controllerContext);
83     }
84
85     @SuppressWarnings("unchecked")
86     @Test
87     public void testExample() throws FileNotFoundException, ParseException {
88         @SuppressWarnings("rawtypes")
89         final
90         NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
91         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
92         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
93         assertEquals(normalizedNodeData,
94                 brokerFacade.readOperationalData(null));
95     }
96
97     @Test
98     public void testRpcForMountpoint() throws Exception {
99         final UriInfo uriInfo = mock(UriInfo.class);
100         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
101
102         final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
103         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
104         doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
105         final SchemaNode schemaNode = mock(SchemaNode.class);
106         doReturn(schemaNode).when(iiCtx).getSchemaNode();
107         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
108         doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
109
110         final DOMMountPoint mount = mock(DOMMountPoint.class);
111         doReturn(mount).when(iiCtx).getMountPoint();
112         final DOMRpcService rpcService = mock(DOMRpcService.class);
113         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
114         doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
115         this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
116         this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
117         verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
118     }
119
120     /**
121      * Create notification stream for toaster module
122      */
123     @Test
124     public void createNotificationStreamTest() {
125         final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
126         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
127         doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
128
129         final SchemaNode schemaNode = mock(SchemaNode.class,
130                 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
131         doReturn(schemaNode).when(iiCtx).getSchemaNode();
132         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
133
134         doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
135                 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
136         doReturn(null).when(iiCtx).getMountPoint();
137
138         final Set<DataContainerChild<?, ?>> children = Sets.newHashSet();
139         final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
140                 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
141
142         final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
143         when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
144         when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
145         children.add(child);
146
147         final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
148                 Mockito.withSettings().extraInterfaces(ContainerNode.class));
149         doReturn(normalizedNode).when(payload).getData();
150         doReturn(children).when(normalizedNode).getValue();
151
152         // register notification
153         final NormalizedNodeContext context = this.restconfImpl
154                 .invokeRpc("sal-remote:create-notification-stream", payload, null);
155         assertNotNull(context);
156     }
157
158     /**
159      * Subscribe for notification stream of toaster module
160      */
161     @Test
162     public void subscribeToNotificationStreamTest() throws Exception {
163         final String identifier = "create-notification-stream/toaster:toastDone";
164
165         // register test notification stream
166         final SchemaPath path = SchemaPath.create(
167                 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
168         Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML");
169
170         final UriInfo uriInfo = mock(UriInfo.class);
171         final UriBuilder uriBuilder = mock(UriBuilder.class);
172         when(uriBuilder.port(8181)).thenReturn(uriBuilder);
173         when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
174         when(uriBuilder.build()).thenReturn(new URI(""));
175         when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
176         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
177
178         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
179         this.restconfImpl.setBroker(brokerFacade);
180
181         // subscribe to stream and verify response
182         final Response response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
183         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
184
185         // remove test notification stream
186         Notificator.removeAllListeners();
187     }
188 }