2 * Copyright (c) 2016 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.netconf.messagebus.eventsources.netconf;
10 import static org.mockito.ArgumentMatchers.eq;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
15 import com.google.common.collect.Lists;
16 import java.time.Instant;
17 import java.time.ZoneId;
18 import java.time.format.DateTimeFormatter;
19 import java.util.List;
20 import java.util.Optional;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
30 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
31 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
32 import org.opendaylight.mdsal.dom.api.DOMRpcService;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
39 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 public class NetconfEventSourceMountTest {
49 public static final String STREAM_1 = "stream-1";
50 public static final String STREAM_2 = "stream-2";
52 private DOMMountPoint domMountPoint;
54 DOMDataBroker dataBroker;
56 DOMRpcService rpcService;
58 private DOMDataTreeReadTransaction tx;
59 private NetconfEventSourceMount mount;
62 public void setUp() throws Exception {
63 MockitoAnnotations.initMocks(this);
64 doReturn(Optional.of(dataBroker)).when(domMountPoint).getService(DOMDataBroker.class);
65 doReturn(Optional.of(rpcService)).when(domMountPoint).getService(DOMRpcService.class);
66 doReturn(Optional.of(mock(DOMNotificationService.class))).when(domMountPoint)
67 .getService(DOMNotificationService.class);
68 doReturn(tx).when(dataBroker).newReadOnlyTransaction();
69 final YangInstanceIdentifier path = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
71 final NormalizedNode<?, ?> streamsNode = NetconfTestUtils.getStreamsNode(STREAM_1, STREAM_2);
72 doReturn(FluentFutures.immediateFluentFuture(Optional.of(streamsNode)))
73 .when(tx).read(LogicalDatastoreType.OPERATIONAL, path);
74 mount = new NetconfEventSourceMount(NetconfTestUtils.getNode("node-1"), domMountPoint);
78 public void testInvokeCreateSubscription() throws Exception {
79 Stream stream = new StreamBuilder()
80 .setName(new StreamNameType(STREAM_1))
82 mount.invokeCreateSubscription(stream, Optional.empty());
83 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
84 "create-subscription"));
85 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
86 verify(rpcService).invokeRpc(eq(type), captor.capture());
87 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
91 public void testInvokeCreateSubscription1() throws Exception {
92 Stream stream = new StreamBuilder()
93 .setName(new StreamNameType(STREAM_1))
94 .setReplaySupport(true)
96 final Instant date = Instant.now();
97 mount.invokeCreateSubscription(stream, Optional.of(date));
98 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
99 "create-subscription"));
100 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
101 verify(rpcService).invokeRpc(eq(type), captor.capture());
102 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
103 final String expDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.atZone(ZoneId.systemDefault()));
104 final Optional<LeafNode> actual = (Optional<LeafNode>) getDate(captor.getValue());
105 Assert.assertTrue(actual.isPresent());
106 String actualDate = (String) actual.get().getValue();
107 Assert.assertEquals(expDate, actualDate);
111 public void testInvokeCreateSubscription2() throws Exception {
112 Stream stream = new StreamBuilder()
113 .setName(new StreamNameType(STREAM_1))
114 .setReplaySupport(true)
116 mount.invokeCreateSubscription(stream, Optional.empty());
117 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
118 "create-subscription"));
119 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
120 verify(rpcService).invokeRpc(eq(type), captor.capture());
121 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
122 final Optional<LeafNode> date = (Optional<LeafNode>) getDate(captor.getValue());
123 Assert.assertFalse(date.isPresent());
128 public void testGetAvailableStreams() throws Exception {
129 final List<Stream> availableStreams = mount.getAvailableStreams();
130 Assert.assertEquals(2, availableStreams.size());
131 final List<String> streamNames = Lists.transform(availableStreams, input -> input.getName().getValue());
132 streamNames.contains(STREAM_1);
133 streamNames.contains(STREAM_2);
136 private static String getStreamName(final ContainerNode value) {
137 YangInstanceIdentifier.NodeIdentifier stream =
138 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "stream"));
139 return (String) value.getChild(stream).get().getValue();
142 private static Optional<?> getDate(final ContainerNode value) {
143 YangInstanceIdentifier.NodeIdentifier startTime =
144 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "startTime"));
145 return value.getChild(startTime);