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.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.only;
14 import static org.mockito.Mockito.verify;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.messagebus.app.util.TopicDOMNotification;
29 import org.opendaylight.mdsal.dom.api.DOMNotification;
30 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicNotification;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInput;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
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.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
44 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
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.impl.schema.Builders;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
50 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
54 public class NetconfEventSourceTest {
57 private static final SchemaPath NOTIFICATION_1_PATH = SchemaPath.create(true, QName.create("ns1", "1970-01-15",
59 private static final SchemaPath NOTIFICATION_2_PATH = SchemaPath.create(true, QName.create("ns2", "1980-02-18",
62 NetconfEventSource netconfEventSource;
65 DOMNotificationPublishService domNotificationPublishServiceMock;
67 DOMNotification matchnigNotification;
69 DOMNotification nonMachtingNotification;
71 NetconfEventSourceMount mount;
74 public void setUp() throws Exception {
75 MockitoAnnotations.initMocks(this);
76 //init notification mocks
77 doReturn(NOTIFICATION_1_PATH).when(matchnigNotification).getType();
78 doReturn(NOTIFICATION_2_PATH).when(nonMachtingNotification).getType();
79 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> body = Builders
80 .containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("ns1",
81 "1970-01-15", "not1data")));
82 doReturn(body.build()).when(matchnigNotification).getBody();
83 //init schema context mock
84 Set<NotificationDefinition> notifications = new HashSet<>();
85 notifications.add(getNotificationDefinitionMock(NOTIFICATION_1_PATH.getLastComponent()));
86 notifications.add(getNotificationDefinitionMock(NOTIFICATION_2_PATH.getLastComponent()));
87 SchemaContext schemaContext = mock(SchemaContext.class);
88 doReturn(notifications).when(schemaContext).getNotifications();
89 //init mount point mock
90 List<Stream> streams = new ArrayList<>();
91 streams.add(createStream("stream-1"));
92 streams.add(createStream("stream-2"));
93 doReturn(streams).when(mount).getAvailableStreams();
94 doReturn(schemaContext).when(mount).getSchemaContext();
95 doReturn(FluentFutures.immediateNullFluentFuture()).when(mount).invokeCreateSubscription(any(), any());
96 doReturn(FluentFutures.immediateNullFluentFuture()).when(mount).invokeCreateSubscription(any());
97 doReturn(mock(ListenerRegistration.class)).when(mount).registerNotificationListener(any(), any());
98 final Node nodeId1 = NetconfTestUtils.getNetconfNode("NodeId1", "node.test.local", ConnectionStatus
99 .Connected, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
100 doReturn(nodeId1).when(mount).getNode();
101 doReturn(nodeId1.getNodeId().getValue()).when(mount).getNodeId();
103 Map<String, String> streamMap = new HashMap<>();
104 streamMap.put(NOTIFICATION_1_PATH.getLastComponent().getNamespace().toString(), "stream-1");
105 netconfEventSource = new NetconfEventSource(
108 domNotificationPublishServiceMock);
113 public void testJoinTopicOnNotification() throws Exception {
114 final JoinTopicInput topic1 = new JoinTopicInputBuilder()
115 .setTopicId(TopicId.getDefaultInstance("topic1"))
116 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
118 netconfEventSource.joinTopic(topic1);
120 ArgumentCaptor<DOMNotification> captor = ArgumentCaptor.forClass(DOMNotification.class);
121 //handle notification matching topic namespace
122 netconfEventSource.onNotification(matchnigNotification);
123 //handle notification that does not match topic namespace
124 netconfEventSource.onNotification(nonMachtingNotification);
125 //only matching notification should be published
126 verify(domNotificationPublishServiceMock).putNotification(captor.capture());
127 final TopicDOMNotification value = (TopicDOMNotification) captor.getValue();
128 final QName qname = TopicNotification.QNAME;
129 final YangInstanceIdentifier.NodeIdentifier topicIdNode =
130 new YangInstanceIdentifier.NodeIdentifier(QName.create(qname, "topic-id"));
131 final Object actualTopicId = value.getBody().getChild(topicIdNode).get().getValue();
132 Assert.assertEquals(topic1.getTopicId(), actualTopicId);
136 public void testDisjoinTopicOnNotification() throws Exception {
137 final TopicId topicId = TopicId.getDefaultInstance("topic1");
138 final JoinTopicInput topic1 = new JoinTopicInputBuilder()
140 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
142 netconfEventSource.joinTopic(topic1);
144 //handle notification matching topic namespace
145 netconfEventSource.onNotification(matchnigNotification);
147 DisJoinTopicInput disjoinTopic = new DisJoinTopicInputBuilder().setTopicId(topicId).build();
148 netconfEventSource.disJoinTopic(disjoinTopic);
149 netconfEventSource.onNotification(matchnigNotification);
150 //topic notification published only once before disjoin
151 verify(domNotificationPublishServiceMock, only()).putNotification(any());
154 private static Stream createStream(final String name) {
155 return new StreamBuilder()
156 .setName(new StreamNameType(name))
157 .setReplaySupport(true)
161 private static NotificationDefinition getNotificationDefinitionMock(final QName qualifiedName) {
162 NotificationDefinition notification = mock(NotificationDefinition.class);
163 doReturn(qualifiedName).when(notification).getQName();
164 doReturn(SchemaPath.create(true, qualifiedName)).when(notification).getPath();