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.notNull;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.Futures;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
35 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
36 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
37 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
38 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
39 import org.opendaylight.controller.messagebus.spi.EventSource;
40 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
41 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 public class NetconfEventSourceManagerTest {
52 NetconfEventSourceManager netconfEventSourceManager;
53 ListenerRegistration listenerRegistrationMock;
54 DOMMountPointService domMountPointServiceMock;
55 MountPointService mountPointServiceMock;
56 EventSourceRegistry eventSourceTopologyMock;
57 DataTreeModification<Node> dataTreeModificationMock;
58 RpcProviderRegistry rpcProviderRegistryMock;
59 EventSourceRegistry eventSourceRegistry;
62 public static void initTestClass() throws IllegalAccessException, InstantiationException {
65 @SuppressWarnings("unchecked")
67 public void setUp() throws Exception {
68 final DataBroker dataBrokerMock = mock(DataBroker.class);
69 final DOMNotificationPublishService domNotificationPublishServiceMock =
70 mock(DOMNotificationPublishService.class);
71 domMountPointServiceMock = mock(DOMMountPointService.class);
72 eventSourceTopologyMock = mock(EventSourceRegistry.class);
73 rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
74 eventSourceRegistry = mock(EventSourceRegistry.class);
76 listenerRegistrationMock = mock(ListenerRegistration.class);
77 doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
78 any(DataTreeIdentifier.class), any(NetconfEventSourceManager.class));
80 DOMMountPoint domMountPointMock = mock(DOMMountPoint.class);
81 Optional<DOMMountPoint> optionalDomMountServiceMock = Optional.of(domMountPointMock);
82 doReturn(optionalDomMountServiceMock).when(domMountPointServiceMock).getMountPoint((YangInstanceIdentifier)
84 DOMDataBroker mpDataBroker = mock(DOMDataBroker.class);
85 doReturn(Optional.of(mpDataBroker)).when(domMountPointMock).getService(DOMDataBroker.class);
86 doReturn(Optional.of(mock(DOMRpcService.class))).when(domMountPointMock).getService(DOMRpcService.class);
87 doReturn(Optional.of(mock(DOMNotificationService.class))).when(domMountPointMock)
88 .getService(DOMNotificationService.class);
90 DOMDataReadOnlyTransaction rtx = mock(DOMDataReadOnlyTransaction.class);
91 doReturn(rtx).when(mpDataBroker).newReadOnlyTransaction();
92 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkFeature = Futures
93 .immediateCheckedFuture(Optional.of(NetconfTestUtils.getStreamsNode("stream-1")));
95 YangInstanceIdentifier pathStream = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
97 doReturn(checkFeature).when(rtx).read(LogicalDatastoreType.OPERATIONAL, pathStream);
99 netconfEventSourceManager = new NetconfEventSourceManager(dataBrokerMock,
100 domNotificationPublishServiceMock, domMountPointServiceMock, eventSourceRegistry);
101 netconfEventSourceManager.setStreamMap(new HashMap<>());
105 public void onDataChangedCreateEventSourceTestByCreateEntry() throws Exception {
106 onDataChangedTestHelper(true, false, true, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
107 netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
108 verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
112 public void onDataChangedCreateEventSourceTestByUpdateEntry() throws Exception {
113 onDataChangedTestHelper(false, true, true, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
114 netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
115 verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
119 public void onDataChangedCreateEventSourceTestNotNeconf() throws Exception {
120 onDataChangedTestHelper(false, true, false, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
121 netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
122 verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
126 public void onDataChangedCreateEventSourceTestNotNotificationCapability() throws Exception {
127 onDataChangedTestHelper(true, false, true, "bad-prefix");
128 netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
129 verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
132 @SuppressWarnings("unchecked")
133 private void onDataChangedTestHelper(boolean create, boolean update, boolean isNetconf, String
134 notificationCapabilityPrefix) throws Exception {
135 dataTreeModificationMock = mock(DataTreeModification.class);
136 DataObjectModification<Node> mockModification = mock(DataObjectModification.class);
137 doReturn(create ? DataObjectModification.ModificationType.WRITE :
138 DataObjectModification.ModificationType.SUBTREE_MODIFIED).when(mockModification).getModificationType();
139 doReturn(mockModification).when(dataTreeModificationMock).getRootNode();
142 String nodeId = "Node01";
144 node01 = NetconfTestUtils
145 .getNetconfNode(nodeId, "node01.test.local", ConnectionStatus.Connected,
146 notificationCapabilityPrefix);
149 node01 = NetconfTestUtils.getNode(nodeId);
152 doReturn(node01).when(mockModification).getDataAfter();
154 doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
155 NetconfTestUtils.getInstanceIdentifier(node01))).when(dataTreeModificationMock).getRootPath();