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.base.Function;
16 import com.google.common.collect.Lists;
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.format.DateTimeFormatter;
20 import java.util.List;
21 import java.util.Optional;
22 import javax.annotation.Nullable;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
33 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
34 import org.opendaylight.mdsal.dom.api.DOMRpcService;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
41 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 public class NetconfEventSourceMountTest {
51 public static final String STREAM_1 = "stream-1";
52 public static final String STREAM_2 = "stream-2";
54 private DOMMountPoint domMountPoint;
56 DOMDataBroker dataBroker;
58 DOMRpcService rpcService;
60 private DOMDataTreeReadTransaction tx;
61 private NetconfEventSourceMount mount;
64 public void setUp() throws Exception {
65 MockitoAnnotations.initMocks(this);
66 doReturn(Optional.of(dataBroker)).when(domMountPoint).getService(DOMDataBroker.class);
67 doReturn(Optional.of(rpcService)).when(domMountPoint).getService(DOMRpcService.class);
68 doReturn(Optional.of(mock(DOMNotificationService.class))).when(domMountPoint)
69 .getService(DOMNotificationService.class);
70 doReturn(tx).when(dataBroker).newReadOnlyTransaction();
71 final YangInstanceIdentifier path = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
73 final NormalizedNode<?, ?> streamsNode = NetconfTestUtils.getStreamsNode(STREAM_1, STREAM_2);
74 doReturn(FluentFutures.immediateFluentFuture(Optional.of(streamsNode)))
75 .when(tx).read(LogicalDatastoreType.OPERATIONAL, path);
76 mount = new NetconfEventSourceMount(NetconfTestUtils.getNode("node-1"), domMountPoint);
80 public void testInvokeCreateSubscription() throws Exception {
81 Stream stream = new StreamBuilder()
82 .setName(new StreamNameType(STREAM_1))
84 mount.invokeCreateSubscription(stream, Optional.empty());
85 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
86 "create-subscription"));
87 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
88 verify(rpcService).invokeRpc(eq(type), captor.capture());
89 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
93 public void testInvokeCreateSubscription1() throws Exception {
94 Stream stream = new StreamBuilder()
95 .setName(new StreamNameType(STREAM_1))
96 .setReplaySupport(true)
98 final Instant date = Instant.now();
99 mount.invokeCreateSubscription(stream, Optional.of(date));
100 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
101 "create-subscription"));
102 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
103 verify(rpcService).invokeRpc(eq(type), captor.capture());
104 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
105 final String expDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.atZone(ZoneId.systemDefault()));
106 final Optional<LeafNode> actual = (Optional<LeafNode>) getDate(captor.getValue());
107 Assert.assertTrue(actual.isPresent());
108 String actualDate = (String) actual.get().getValue();
109 Assert.assertEquals(expDate, actualDate);
113 public void testInvokeCreateSubscription2() throws Exception {
114 Stream stream = new StreamBuilder()
115 .setName(new StreamNameType(STREAM_1))
116 .setReplaySupport(true)
118 mount.invokeCreateSubscription(stream, Optional.empty());
119 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
120 "create-subscription"));
121 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
122 verify(rpcService).invokeRpc(eq(type), captor.capture());
123 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
124 final Optional<LeafNode> date = (Optional<LeafNode>) getDate(captor.getValue());
125 Assert.assertFalse(date.isPresent());
130 public void testGetAvailableStreams() throws Exception {
131 final List<Stream> availableStreams = mount.getAvailableStreams();
132 Assert.assertEquals(2, availableStreams.size());
133 final List<String> streamNames = Lists.transform(availableStreams, new Function<Stream, String>() {
136 public String apply(@Nullable final Stream input) {
137 return input.getName().getValue();
140 streamNames.contains(STREAM_1);
141 streamNames.contains(STREAM_2);
144 private static String getStreamName(final ContainerNode value) {
145 YangInstanceIdentifier.NodeIdentifier stream =
146 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "stream"));
147 return (String) value.getChild(stream).get().getValue();
150 private static Optional<?> getDate(final ContainerNode value) {
151 YangInstanceIdentifier.NodeIdentifier startTime =
152 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "startTime"));
153 return value.getChild(startTime);