3174908fb88747c53900896fc8025db7b21e05df
[netconf.git] / restconf / restconf-nb-bierman02 / 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.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.mockito.Mockito.withSettings;
21 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22
23 import java.io.FileNotFoundException;
24 import java.net.URI;
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;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Optional;
33 import java.util.Set;
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.common.YangConstants;
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.InputSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.Module;
77 import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode;
78 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
79 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
80 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
81 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
82 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
83 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
84 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
85
86 /**
87  * See {@link InvokeRpcMethodTest}.
88  */
89 public class RestconfImplTest {
90
91     private static EffectiveModelContext schemaContext;
92
93     private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
94     private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
95     private final RestconfImpl restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
96
97     @BeforeClass
98     public static void init() throws FileNotFoundException, ReactorException {
99         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/modules/restconf-module-testing");
100     }
101
102     @AfterClass
103     public static void cleanUp() {
104         WebSocketServer.destroyInstance(); // NETCONF-604
105     }
106
107     @Test
108     public void binaryKeyTest() {
109         final List<Byte> al = new ArrayList<>();
110         al.add((byte) 1);
111         binaryKeyTest(al, al);
112     }
113
114     private static void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
115
116         final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");
117
118         final Map<QName, Object> uriKeyValues = new HashMap<>();
119         uriKeyValues.put(keyDef, al.toArray());
120
121         final MapEntryNode payload = mock(MapEntryNode.class);
122         final NodeIdentifierWithPredicates nodeIdWithPred =
123                 NodeIdentifierWithPredicates.of(keyDef, keyDef, al2.toArray());
124         when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
125
126         final List<QName> keyDefinitions = new ArrayList<>();
127         keyDefinitions.add(keyDef);
128         RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
129     }
130
131     @Test
132     public void binaryKeyFailTest() {
133         final List<Byte> al = new ArrayList<>();
134         al.add((byte) 1);
135         final List<Byte> al2 = new ArrayList<>();
136         try {
137             binaryKeyTest(al, al2);
138         } catch (final RestconfDocumentedException e) {
139             final RestconfError err = e.getErrors().iterator().next();
140             assertEquals(ErrorType.PROTOCOL, err.getErrorType());
141             assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
142         }
143     }
144
145     @Test
146     public void testExample() throws FileNotFoundException, ParseException {
147         final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
148         when(brokerFacade.readOperationalData(isNull())).thenReturn(normalizedNodeData);
149         assertEquals(normalizedNodeData,
150                 brokerFacade.readOperationalData(null));
151     }
152
153     @Test
154     public void testRpcForMountpoint() throws Exception {
155         final QName qname = QName.create("namespace", "2010-10-10", "localname");
156         final UriInfo uriInfo = mock(UriInfo.class);
157         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
158
159         final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
160         final RpcDefinition rpc = mock(RpcDefinition.class,
161             withSettings().extraInterfaces(RpcEffectiveStatement.class));
162         doReturn(qname).when(rpc).getQName();
163
164         final InputSchemaNode input = mock(InputSchemaNode.class,
165             withSettings().extraInterfaces(InputEffectiveStatement.class));
166         final QName inputQName = YangConstants.operationInputQName(qname.getModule());
167         doReturn(input).when(rpc).getInput();
168         doReturn(inputQName).when(input).getQName();
169         doReturn(Optional.of(input)).when((RpcEffectiveStatement) rpc).findSchemaTreeNode(inputQName);
170
171         final OutputSchemaNode output = mock(OutputSchemaNode.class,
172             withSettings().extraInterfaces(OutputEffectiveStatement.class));
173         final QName outputQName = YangConstants.operationInputQName(qname.getModule());
174         doReturn(output).when(rpc).getOutput();
175         doReturn(outputQName).when(output).getQName();
176         doReturn(Optional.of(output)).when((RpcEffectiveStatement) rpc).findSchemaTreeNode(outputQName);
177
178         final EffectiveModelContext mountContext = mock(EffectiveModelContext.class);
179         final ModuleEffectiveStatement mountModule = mock(ModuleEffectiveStatement.class);
180         doReturn(Map.of(qname.getModule(), mountModule)).when(mountContext).getModuleStatements();
181         doReturn(Optional.of(rpc)).when(mountModule).findSchemaTreeNode(qname);
182
183         final DOMMountPoint mount = mock(DOMMountPoint.class);
184         doReturn(Optional.of(FixedDOMSchemaService.of(mountContext))).when(mount).getService(DOMSchemaService.class);
185
186         doReturn(InstanceIdentifierContext.ofRpcInput(mountContext, rpc, mount))
187             .when(ctx).getInstanceIdentifierContext();
188
189         final DOMRpcService rpcService = mock(DOMRpcService.class);
190         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
191         doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService)
192                 .invokeRpc(any(QName.class), any(NormalizedNode.class));
193         restconfImpl.invokeRpc("randomId", ctx, uriInfo);
194         restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
195         verify(rpcService, times(2)).invokeRpc(any(QName.class), any());
196     }
197
198     /**
199      * Create notification stream for toaster module.
200      */
201     @Test
202     public void createNotificationStreamTest() {
203         final QName rpcQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
204             "2014-01-14", "create-notification-stream");
205
206         final RpcDefinition schemaNode = schemaContext.getOperations().stream()
207             .filter(rpc -> rpc.getQName().equals(rpcQName))
208             .findFirst()
209             .orElseThrow();
210
211         final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
212         doReturn(InstanceIdentifierContext.ofRpcInput(schemaContext, schemaNode, null)).when(payload)
213                 .getInstanceIdentifierContext();
214
215         final Set<DataContainerChild> children = new HashSet<>();
216         final LeafSetNode child = mock(LeafSetNode.class);
217
218         final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
219         when(entryNode.body()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
220         when(child.body()).thenReturn(Set.of(entryNode));
221         children.add(child);
222
223         final ContainerNode normalizedNode = mock(ContainerNode.class);
224         doReturn(normalizedNode).when(payload).getData();
225         doReturn(children).when(normalizedNode).body();
226
227         // register notification
228         final NormalizedNodeContext context = restconfImpl
229                 .invokeRpc("sal-remote:create-notification-stream", payload, null);
230         assertNotNull(context);
231     }
232
233     /**
234      * Tests stream entry node.
235      */
236     @Test
237     public void toStreamEntryNodeTest() {
238         final Module restconfModule = controllerContext.getRestconfModule();
239         final DataSchemaNode streamSchemaNode = controllerContext
240                 .getRestconfModuleRestConfSchemaNode(restconfModule, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
241         final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamSchemaNode;
242         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues =
243             SchemaAwareBuilders.mapEntryBuilder(listStreamSchemaNode);
244         var instanceDataChildrenByName =
245                 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "name");
246         final DataSchemaNode nameSchemaNode = instanceDataChildrenByName.get(0).child;
247         streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
248             .withValue("")
249             .build());
250
251         instanceDataChildrenByName =
252                 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "description");
253         final DataSchemaNode descriptionSchemaNode = instanceDataChildrenByName.get(0).child;
254         streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
255             .withValue("DESCRIPTION_PLACEHOLDER")
256             .build());
257
258         instanceDataChildrenByName =
259                 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-support");
260         final DataSchemaNode replaySupportSchemaNode = instanceDataChildrenByName.get(0).child;
261         streamNodeValues.withChild(
262             SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replaySupportSchemaNode).withValue(Boolean.TRUE).build());
263
264         instanceDataChildrenByName =
265                 ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-log-creation-time");
266         final DataSchemaNode replayLogCreationTimeSchemaNode = instanceDataChildrenByName.get(0).child;
267         streamNodeValues.withChild(
268             SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replayLogCreationTimeSchemaNode).withValue("").build());
269         instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "events");
270         final DataSchemaNode eventsSchemaNode = instanceDataChildrenByName.get(0).child;
271         streamNodeValues.withChild(
272             SchemaAwareBuilders.leafBuilder((LeafSchemaNode) eventsSchemaNode).withValue(Empty.value()).build());
273         assertNotNull(streamNodeValues.build());
274     }
275
276     /**
277      * Subscribe for notification stream of toaster module.
278      */
279     @Test
280     public void subscribeToNotificationStreamTest() throws Exception {
281         final String identifier = "create-notification-stream/toaster:toastDone";
282
283         // register test notification stream
284         Notificator.createNotificationListener(
285             List.of(Absolute.of(QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"))),
286             identifier, "XML", controllerContext);
287
288         final UriInfo uriInfo = mock(UriInfo.class);
289         final UriBuilder uriBuilder = mock(UriBuilder.class);
290         when(uriBuilder.port(8181)).thenReturn(uriBuilder);
291         when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
292         when(uriBuilder.build()).thenReturn(new URI(""));
293         when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
294         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
295         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
296         final Set<Entry<String, List<String>>> set = new HashSet<>();
297         when(map.entrySet()).thenReturn(set);
298         when(uriInfo.getQueryParameters()).thenReturn(map);
299
300         // subscribe to stream and verify response
301         final NormalizedNodeContext response = restconfImpl.subscribeToStream(identifier, uriInfo);
302
303         // remove test notification stream
304         Notificator.removeAllListeners();
305     }
306 }