Merge "Revert "Add PasswordCredentialAuth interface""
[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.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;
15
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;
21 import java.util.Map;
22 import java.util.Set;
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;
53
54 public class NetconfEventSourceTest {
55
56
57     private static final SchemaPath notification1Path = SchemaPath.create(true, QName.create("ns1", "1970-01-15", "not1"));
58     private static final SchemaPath notification2Path = SchemaPath.create(true, QName.create("ns2", "1980-02-18", "not2"));
59
60     NetconfEventSource netconfEventSource;
61
62     @Mock
63     DOMNotificationPublishService domNotificationPublishServiceMock;
64     @Mock
65     DOMNotification matchnigNotification;
66     @Mock
67     DOMNotification nonMachtingNotification;
68     @Mock
69     NetconfEventSourceMount mount;
70
71     @Before
72     public void setUp() throws Exception {
73         MockitoAnnotations.initMocks(this);
74         //init notification mocks
75         doReturn(notification1Path).when(matchnigNotification).getType();
76         doReturn(notification2Path).when(nonMachtingNotification).getType();
77         DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> body = Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("ns1", "1970-01-15", "not1data")));
78         doReturn(body.build()).when(matchnigNotification).getBody();
79         //init schema context mock
80         Set<NotificationDefinition> notifications = new HashSet<>();
81         notifications.add(getNotificationDefinitionMock(notification1Path.getLastComponent()));
82         notifications.add(getNotificationDefinitionMock(notification2Path.getLastComponent()));
83         SchemaContext schemaContext = mock(SchemaContext.class);
84         doReturn(notifications).when(schemaContext).getNotifications();
85         //init mount point mock
86         List<Stream> streams = new ArrayList<>();
87         streams.add(createStream("stream-1"));
88         streams.add(createStream("stream-2"));
89         doReturn(streams).when(mount).getAvailableStreams();
90         doReturn(schemaContext).when(mount).getSchemaContext();
91         doReturn(Futures.immediateCheckedFuture(null)).when(mount).invokeCreateSubscription(any(), any());
92         doReturn(Futures.immediateCheckedFuture(null)).when(mount).invokeCreateSubscription(any());
93         doReturn(mock(ListenerRegistration.class)).when(mount).registerNotificationListener(any(), any());
94         final Node nodeId1 = NetconfTestUtils.getNetconfNode("NodeId1", "node.test.local", ConnectionStatus.Connected, NetconfTestUtils.notification_capability_prefix);
95         doReturn(nodeId1).when(mount).getNode();
96
97         Map<String, String> streamMap = new HashMap<>();
98         streamMap.put(notification1Path.getLastComponent().getNamespace().toString(), "stream-1");
99         netconfEventSource = new NetconfEventSource(
100                 streamMap,
101                 mount,
102                 domNotificationPublishServiceMock);
103
104     }
105
106     @Test
107     public void testJoinTopicOnNotification() throws Exception {
108         final JoinTopicInput topic1 = new JoinTopicInputBuilder()
109                 .setTopicId(TopicId.getDefaultInstance("topic1"))
110                 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
111                 .build();
112         netconfEventSource.joinTopic(topic1);
113
114         ArgumentCaptor<DOMNotification> captor = ArgumentCaptor.forClass(DOMNotification.class);
115         //handle notification matching topic namespace
116         netconfEventSource.onNotification(matchnigNotification);
117         //handle notification that does not match topic namespace
118         netconfEventSource.onNotification(nonMachtingNotification);
119         //only matching notification should be published
120         verify(domNotificationPublishServiceMock).putNotification(captor.capture());
121         final TopicDOMNotification value = (TopicDOMNotification) captor.getValue();
122         final QName qname = TopicNotification.QNAME;
123         final YangInstanceIdentifier.NodeIdentifier topicIdNode =
124                 new YangInstanceIdentifier.NodeIdentifier(QName.create(qname.getNamespace().toString(), qname.getFormattedRevision(), "topic-id"));
125         final Object actualTopicId = value.getBody().getChild(topicIdNode).get().getValue();
126         Assert.assertEquals(topic1.getTopicId(), actualTopicId);
127     }
128
129     @Test
130     public void testDisjoinTopicOnNotification() throws Exception {
131         final TopicId topicId = TopicId.getDefaultInstance("topic1");
132         final JoinTopicInput topic1 = new JoinTopicInputBuilder()
133                 .setTopicId(topicId)
134                 .setNotificationPattern(NotificationPattern.getDefaultInstance(".*ns1"))
135                 .build();
136         netconfEventSource.joinTopic(topic1);
137
138         //handle notification matching topic namespace
139         netconfEventSource.onNotification(matchnigNotification);
140         //disjoin topic
141         DisJoinTopicInput disjoinTopic = new DisJoinTopicInputBuilder().setTopicId(topicId).build();
142         netconfEventSource.disJoinTopic(disjoinTopic);
143         netconfEventSource.onNotification(matchnigNotification);
144         //topic notification published only once before disjoin
145         verify(domNotificationPublishServiceMock, only()).putNotification(any());
146     }
147
148     private Stream createStream(String name) {
149         return new StreamBuilder()
150                 .setName(new StreamNameType(name))
151                 .setReplaySupport(true)
152                 .build();
153     }
154
155     private NotificationDefinition getNotificationDefinitionMock(QName qName) {
156         NotificationDefinition notification = mock(NotificationDefinition.class);
157         doReturn(qName).when(notification).getQName();
158         doReturn(SchemaPath.create(true, qName)).when(notification).getPath();
159         return notification;
160     }
161
162 }