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.Matchers.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 com.google.common.util.concurrent.Futures;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
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.controller.md.sal.dom.api.DOMNotification;
30 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
31 import org.opendaylight.controller.messagebus.app.util.TopicDOMNotification;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicNotification;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInput;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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.DataContainerNodeAttrBuilder;
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 DataContainerNodeAttrBuilder<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(Futures.immediateCheckedFuture(null)).when(mount).invokeCreateSubscription(any(), any());
96 doReturn(Futures.immediateCheckedFuture(null)).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();
102 Map<String, String> streamMap = new HashMap<>();
103 streamMap.put(NOTIFICATION_1_PATH.getLastComponent().getNamespace().toString(), "stream-1");
104 netconfEventSource = new NetconfEventSource(
107 domNotificationPublishServiceMock);
112 public void testJoinTopicOnNotification() throws Exception {
113 final JoinTopicInput topic1 = new JoinTopicInputBuilder()
114 .setTopicId(TopicId.getDefaultInstance("topic1"))
115 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
117 netconfEventSource.joinTopic(topic1);
119 ArgumentCaptor<DOMNotification> captor = ArgumentCaptor.forClass(DOMNotification.class);
120 //handle notification matching topic namespace
121 netconfEventSource.onNotification(matchnigNotification);
122 //handle notification that does not match topic namespace
123 netconfEventSource.onNotification(nonMachtingNotification);
124 //only matching notification should be published
125 verify(domNotificationPublishServiceMock).putNotification(captor.capture());
126 final TopicDOMNotification value = (TopicDOMNotification) captor.getValue();
127 final QName qname = TopicNotification.QNAME;
128 final YangInstanceIdentifier.NodeIdentifier topicIdNode =
129 new YangInstanceIdentifier.NodeIdentifier(QName.create(qname.getNamespace().toString(), qname
130 .getFormattedRevision(), "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 Stream createStream(String name) {
155 return new StreamBuilder()
156 .setName(new StreamNameType(name))
157 .setReplaySupport(true)
161 private NotificationDefinition getNotificationDefinitionMock(QName qualifiedName) {
162 NotificationDefinition notification = mock(NotificationDefinition.class);
163 doReturn(qualifiedName).when(notification).getQName();
164 doReturn(SchemaPath.create(true, qualifiedName)).when(notification).getPath();