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.Matchers.eq;
12 import static org.mockito.Matchers.notNull;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.yang.messagebus.netconf.NamespaceToStream;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
30 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
31 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
33 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
36 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
37 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
38 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
39 import org.opendaylight.controller.messagebus.spi.EventSource;
40 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
41 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
42 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 public class NetconfEventSourceManagerTest {
54 NetconfEventSourceManager netconfEventSourceManager;
55 ListenerRegistration listenerRegistrationMock;
56 DOMMountPointService domMountPointServiceMock;
57 MountPointService mountPointServiceMock;
58 EventSourceRegistry eventSourceTopologyMock;
59 AsyncDataChangeEvent asyncDataChangeEventMock;
60 RpcProviderRegistry rpcProviderRegistryMock;
61 EventSourceRegistry eventSourceRegistry;
63 public static void initTestClass() throws IllegalAccessException, InstantiationException {
67 public void setUp() throws Exception {
68 DataBroker dataBrokerMock = mock(DataBroker.class);
69 DOMNotificationPublishService domNotificationPublishServiceMock = mock(DOMNotificationPublishService.class);
70 domMountPointServiceMock = mock(DOMMountPointService.class);
71 mountPointServiceMock = mock(MountPointService.class);
72 eventSourceTopologyMock = mock(EventSourceRegistry.class);
73 rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
74 eventSourceRegistry = mock(EventSourceRegistry.class);
75 List<NamespaceToStream> namespaceToStreamList = new ArrayList<>();
77 listenerRegistrationMock = mock(ListenerRegistration.class);
78 doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(NetconfEventSourceManager.class), eq(
79 AsyncDataBroker.DataChangeScope.SUBTREE));
81 Optional<DOMMountPoint> optionalDomMountServiceMock = (Optional<DOMMountPoint>) mock(Optional.class);
82 doReturn(true).when(optionalDomMountServiceMock).isPresent();
83 doReturn(optionalDomMountServiceMock).when(domMountPointServiceMock).getMountPoint((YangInstanceIdentifier)notNull());
85 DOMMountPoint domMountPointMock = mock(DOMMountPoint.class);
86 doReturn(domMountPointMock).when(optionalDomMountServiceMock).get();
89 Optional optionalBindingMountMock = mock(Optional.class);
90 doReturn(true).when(optionalBindingMountMock).isPresent();
92 MountPoint mountPointMock = mock(MountPoint.class);
93 doReturn(optionalBindingMountMock).when(mountPointServiceMock).getMountPoint(any(InstanceIdentifier.class));
94 doReturn(mountPointMock).when(optionalBindingMountMock).get();
96 Optional optionalMpDataBroker = mock(Optional.class);
97 DataBroker mpDataBroker = mock(DataBroker.class);
98 doReturn(optionalMpDataBroker).when(mountPointMock).getService(DataBroker.class);
99 doReturn(true).when(optionalMpDataBroker).isPresent();
100 doReturn(mpDataBroker).when(optionalMpDataBroker).get();
102 ReadOnlyTransaction rtx = mock(ReadOnlyTransaction.class);
103 doReturn(rtx).when(mpDataBroker).newReadOnlyTransaction();
104 CheckedFuture<Optional<Streams>, ReadFailedException> checkFeature = (CheckedFuture<Optional<Streams>, ReadFailedException>)mock(CheckedFuture.class);
105 InstanceIdentifier<Streams> pathStream = InstanceIdentifier.builder(Netconf.class).child(Streams.class).build();
106 doReturn(checkFeature).when(rtx).read(LogicalDatastoreType.OPERATIONAL, pathStream);
107 Optional<Streams> avStreams = NetconfTestUtils.getAvailableStream("stream01", true);
108 doReturn(avStreams).when(checkFeature).checkedGet();
110 EventSourceRegistration esrMock = mock(EventSourceRegistration.class);
112 netconfEventSourceManager =
113 NetconfEventSourceManager
114 .create(dataBrokerMock, domNotificationPublishServiceMock, domMountPointServiceMock,
115 mountPointServiceMock, eventSourceRegistry, namespaceToStreamList);
119 public void onDataChangedCreateEventSourceTestByCreateEntry() throws Exception {
120 onDataChangedTestHelper(true,false,true, NetconfTestUtils.notification_capability_prefix);
121 netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
122 verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
126 public void onDataChangedCreateEventSourceTestByUpdateEntry() throws Exception {
127 onDataChangedTestHelper(false,true,true, NetconfTestUtils.notification_capability_prefix);
128 netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
129 verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
133 public void onDataChangedCreateEventSourceTestNotNeconf() throws Exception {
134 onDataChangedTestHelper(false,true,false, NetconfTestUtils.notification_capability_prefix);
135 netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
136 verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
140 public void onDataChangedCreateEventSourceTestNotNotificationCapability() throws Exception {
141 onDataChangedTestHelper(true,false,true,"bad-prefix");
142 netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
143 verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
146 private void onDataChangedTestHelper(boolean create, boolean update, boolean isNetconf, String notificationCapabilityPrefix) throws Exception{
147 asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
148 Map<InstanceIdentifier, DataObject> mapCreate = new HashMap<>();
149 Map<InstanceIdentifier, DataObject> mapUpdate = new HashMap<>();
152 String nodeId = "Node01";
153 doReturn(mapCreate).when(asyncDataChangeEventMock).getCreatedData();
154 doReturn(mapUpdate).when(asyncDataChangeEventMock).getUpdatedData();
157 node01 = NetconfTestUtils
158 .getNetconfNode(nodeId, "node01.test.local", ConnectionStatus.Connected, notificationCapabilityPrefix);
161 node01 = NetconfTestUtils.getNode(nodeId);
165 mapCreate.put(NetconfTestUtils.getInstanceIdentifier(node01), node01);
168 mapUpdate.put(NetconfTestUtils.getInstanceIdentifier(node01), node01);