Bump versions to 2.0.0-SNAPSHOT
[netconf.git] / netconf / messagebus-netconf / src / test / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceTest.java
1 /*
2  * Copyright (c) 2016 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.netconf.messagebus.eventsources.netconf;
9
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;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.controller.messagebus.app.util.TopicDOMNotification;
30 import org.opendaylight.mdsal.dom.api.DOMNotification;
31 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
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.util.concurrent.FluentFutures;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
51 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
52 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
54 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
55
56 @Deprecated(forRemoval = true)
57 @RunWith(MockitoJUnitRunner.StrictStubs.class)
58 public class NetconfEventSourceTest {
59     private static final Absolute NOTIFICATION_1_PATH = Absolute.of(QName.create("ns1", "1970-01-15", "not1"));
60     private static final Absolute NOTIFICATION_2_PATH = Absolute.of(QName.create("ns2", "1980-02-18", "not2"));
61
62     NetconfEventSource netconfEventSource;
63
64     @Mock
65     DOMNotificationPublishService domNotificationPublishServiceMock;
66     @Mock
67     DOMNotification matchnigNotification;
68     @Mock
69     DOMNotification nonMachtingNotification;
70     @Mock
71     NetconfEventSourceMount mount;
72
73     @Before
74     public void setUp() throws Exception {
75         //init notification mocks
76         doReturn(NOTIFICATION_1_PATH).when(matchnigNotification).getType();
77         doReturn(NOTIFICATION_2_PATH).when(nonMachtingNotification).getType();
78         DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> body = Builders
79                 .containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("ns1",
80                         "1970-01-15", "not1data")));
81         doReturn(body.build()).when(matchnigNotification).getBody();
82         //init schema context mock
83         Set<NotificationDefinition> notifications = new HashSet<>();
84         notifications.add(getNotificationDefinitionMock(NOTIFICATION_1_PATH.lastNodeIdentifier()));
85         notifications.add(getNotificationDefinitionMock(NOTIFICATION_2_PATH.lastNodeIdentifier()));
86         EffectiveModelContext schemaContext = mock(EffectiveModelContext.class);
87         doReturn(notifications).when(schemaContext).getNotifications();
88         //init mount point mock
89         List<Stream> streams = new ArrayList<>();
90         streams.add(createStream("stream-1"));
91         streams.add(createStream("stream-2"));
92         doReturn(streams).when(mount).getAvailableStreams();
93         doReturn(schemaContext).when(mount).getSchemaContext();
94         doReturn(FluentFutures.immediateNullFluentFuture()).when(mount).invokeCreateSubscription(any());
95         doReturn(mock(ListenerRegistration.class)).when(mount).registerNotificationListener(any(), any());
96         final Node nodeId1 = NetconfTestUtils.getNetconfNode("NodeId1", "node.test.local", ConnectionStatus
97                 .Connected, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
98         doReturn(nodeId1).when(mount).getNode();
99         doReturn(nodeId1.getNodeId().getValue()).when(mount).getNodeId();
100
101         Map<String, String> streamMap = new HashMap<>();
102         streamMap.put(NOTIFICATION_1_PATH.lastNodeIdentifier().getNamespace().toString(), "stream-1");
103         netconfEventSource = new NetconfEventSource(
104                 streamMap,
105                 mount,
106                 domNotificationPublishServiceMock);
107
108     }
109
110     @Test
111     public void testJoinTopicOnNotification() throws Exception {
112         final JoinTopicInput topic1 = new JoinTopicInputBuilder()
113                 .setTopicId(TopicId.getDefaultInstance("topic1"))
114                 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
115                 .build();
116         netconfEventSource.joinTopic(topic1);
117
118         ArgumentCaptor<DOMNotification> captor = ArgumentCaptor.forClass(DOMNotification.class);
119         //handle notification matching topic namespace
120         netconfEventSource.onNotification(matchnigNotification);
121         //handle notification that does not match topic namespace
122         netconfEventSource.onNotification(nonMachtingNotification);
123         //only matching notification should be published
124         verify(domNotificationPublishServiceMock).putNotification(captor.capture());
125         final TopicDOMNotification value = (TopicDOMNotification) captor.getValue();
126         final QName qname = TopicNotification.QNAME;
127         final YangInstanceIdentifier.NodeIdentifier topicIdNode =
128                 new YangInstanceIdentifier.NodeIdentifier(QName.create(qname, "topic-id"));
129         final Object actualTopicId = value.getBody().getChild(topicIdNode).get().getValue();
130         Assert.assertEquals(topic1.getTopicId(), actualTopicId);
131     }
132
133     @Test
134     public void testDisjoinTopicOnNotification() throws Exception {
135         final TopicId topicId = TopicId.getDefaultInstance("topic1");
136         final JoinTopicInput topic1 = new JoinTopicInputBuilder()
137                 .setTopicId(topicId)
138                 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
139                 .build();
140         netconfEventSource.joinTopic(topic1);
141
142         //handle notification matching topic namespace
143         netconfEventSource.onNotification(matchnigNotification);
144         //disjoin topic
145         DisJoinTopicInput disjoinTopic = new DisJoinTopicInputBuilder().setTopicId(topicId).build();
146         netconfEventSource.disJoinTopic(disjoinTopic);
147         netconfEventSource.onNotification(matchnigNotification);
148         //topic notification published only once before disjoin
149         verify(domNotificationPublishServiceMock, only()).putNotification(any());
150     }
151
152     private static Stream createStream(final String name) {
153         return new StreamBuilder()
154                 .setName(new StreamNameType(name))
155                 .setReplaySupport(true)
156                 .build();
157     }
158
159     private static NotificationDefinition getNotificationDefinitionMock(final QName qualifiedName) {
160         NotificationDefinition notification = mock(NotificationDefinition.class);
161         doReturn(qualifiedName).when(notification).getQName();
162         doReturn(SchemaPath.create(true, qualifiedName)).when(notification).getPath();
163         return notification;
164     }
165
166 }