Merge "Add missing copyright text"
[controller.git] / opendaylight / md-sal / messagebus-impl / src / test / java / org / opendaylight / controller / messagebus / app / impl / NetconfEventSourceManagerTest.java
1 /*
2  * Copyright (c) 2015 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.controller.messagebus.app.impl;
9
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;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.concurrent.ExecutionException;
23
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.yang.messagebus.app.impl.NamespaceToStream;
28 import org.opendaylight.controller.md.sal.binding.api.BindingService;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
31 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
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.dom.api.DOMMountPoint;
36 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
37 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
38 import org.opendaylight.controller.messagebus.eventsources.netconf.NetconfEventSourceManager;
39 import org.opendaylight.controller.messagebus.spi.EventSource;
40 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
41 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
42 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeFields;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.fields.AvailableCapabilities;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53
54 import com.google.common.base.Optional;
55
56 public class NetconfEventSourceManagerTest {
57
58     private static final String notification_capability_prefix = "(urn:ietf:params:xml:ns:netconf:notification";
59     NetconfEventSourceManager netconfEventSourceManager;
60     ListenerRegistration listenerRegistrationMock;
61     DOMMountPointService domMountPointServiceMock;
62     MountPointService mountPointServiceMock;
63     EventSourceTopology eventSourceTopologyMock;
64     AsyncDataChangeEvent asyncDataChangeEventMock;
65     RpcProviderRegistry rpcProviderRegistryMock;
66     EventSourceRegistry eventSourceRegistry;
67     @BeforeClass
68     public static void initTestClass() throws IllegalAccessException, InstantiationException {
69     }
70
71     @Before
72     public void setUp() throws Exception {
73         DataBroker dataBrokerMock = mock(DataBroker.class);
74         DOMNotificationPublishService domNotificationPublishServiceMock = mock(DOMNotificationPublishService.class);
75         domMountPointServiceMock = mock(DOMMountPointService.class);
76         mountPointServiceMock = mock(MountPointService.class);
77         eventSourceTopologyMock = mock(EventSourceTopology.class);
78         rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
79         eventSourceRegistry = mock(EventSourceRegistry.class);
80         List<NamespaceToStream> namespaceToStreamList = new ArrayList<>();
81
82         listenerRegistrationMock = mock(ListenerRegistration.class);
83         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(NetconfEventSourceManager.class), eq(AsyncDataBroker.DataChangeScope.SUBTREE));
84
85         netconfEventSourceManager =
86                 NetconfEventSourceManager.create(dataBrokerMock,
87                         domNotificationPublishServiceMock,
88                         domMountPointServiceMock,
89                         mountPointServiceMock,
90                         eventSourceRegistry,
91                         namespaceToStreamList);
92     }
93
94     @Test
95     public void onDataChangedCreateEventSourceTestByCreateEntry() throws InterruptedException, ExecutionException {
96         onDataChangedTestHelper(true,false,true,notification_capability_prefix);
97         netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
98         verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
99     }
100
101     @Test
102     public void onDataChangedCreateEventSourceTestByUpdateEntry() throws InterruptedException, ExecutionException {
103         onDataChangedTestHelper(false,true,true, notification_capability_prefix);
104         netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
105         verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
106     }
107
108     @Test
109     public void onDataChangedCreateEventSourceTestNotNeconf() throws InterruptedException, ExecutionException {
110         onDataChangedTestHelper(false,true,false,notification_capability_prefix);
111         netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
112         verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
113     }
114
115     @Test
116     public void onDataChangedCreateEventSourceTestNotNotificationCapability() throws InterruptedException, ExecutionException {
117         onDataChangedTestHelper(false,true,true,"bad-prefix");
118         netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
119         verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
120     }
121
122     private void onDataChangedTestHelper(boolean create, boolean update, boolean isNetconf, String notificationCapabilityPrefix) throws InterruptedException, ExecutionException{
123         asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
124         Map<InstanceIdentifier, DataObject> mapCreate = new HashMap<>();
125         Map<InstanceIdentifier, DataObject> mapUpdate = new HashMap<>();
126         InstanceIdentifier instanceIdentifierMock = mock(InstanceIdentifier.class);
127         Node dataObjectMock = mock(Node.class);
128
129         if(create){
130             mapCreate.put(instanceIdentifierMock, dataObjectMock);
131         }
132         if(update){
133             mapUpdate.put(instanceIdentifierMock, dataObjectMock);
134         }
135
136         doReturn(mapCreate).when(asyncDataChangeEventMock).getCreatedData();
137         doReturn(mapUpdate).when(asyncDataChangeEventMock).getUpdatedData();
138         NetconfNode netconfNodeMock = mock(NetconfNode.class);
139         AvailableCapabilities availableCapabilitiesMock = mock(AvailableCapabilities.class);
140         if(isNetconf){
141             doReturn(netconfNodeMock).when(dataObjectMock).getAugmentation(NetconfNode.class);
142             doReturn(availableCapabilitiesMock).when(netconfNodeMock).getAvailableCapabilities();
143             List<String> availableCapabilityList = new ArrayList<>();
144             availableCapabilityList.add(notificationCapabilityPrefix +"_availableCapabilityString1");
145             doReturn(availableCapabilityList).when(availableCapabilitiesMock).getAvailableCapability();
146             doReturn(NetconfNodeFields.ConnectionStatus.Connected).when(netconfNodeMock).getConnectionStatus();
147         } else {
148             doReturn(null).when(dataObjectMock).getAugmentation(NetconfNode.class);
149         }
150
151         Optional optionalMock = mock(Optional.class);
152         Optional optionalBindingMountMock = mock(Optional.class);
153         NodeId nodeId = new NodeId("nodeId1");
154         doReturn(nodeId).when(dataObjectMock).getNodeId();
155         doReturn(optionalMock).when(domMountPointServiceMock).getMountPoint((YangInstanceIdentifier)notNull());
156         doReturn(optionalBindingMountMock).when(mountPointServiceMock).getMountPoint(any(InstanceIdentifier.class));
157         doReturn(true).when(optionalMock).isPresent();
158         doReturn(true).when(optionalBindingMountMock).isPresent();
159
160         DOMMountPoint domMountPointMock = mock(DOMMountPoint.class);
161         MountPoint mountPointMock = mock(MountPoint.class);
162         doReturn(domMountPointMock).when(optionalMock).get();
163         doReturn(mountPointMock).when(optionalBindingMountMock).get();
164
165         RpcConsumerRegistry rpcConsumerRegistryMock = mock(RpcConsumerRegistry.class);
166         Optional<BindingService> onlyOptionalMock = (Optional<BindingService>) mock(Optional.class);
167         NotificationsService notificationsServiceMock = mock(NotificationsService.class);
168
169         doReturn(onlyOptionalMock).when(mountPointMock).getService(RpcConsumerRegistry.class);
170         doReturn(rpcConsumerRegistryMock).when(onlyOptionalMock).get();
171         doReturn(notificationsServiceMock).when(rpcConsumerRegistryMock).getRpcService(NotificationsService.class);
172         EventSourceRegistrationImpl esrMock = mock(EventSourceRegistrationImpl.class);
173         doReturn(esrMock).when(eventSourceRegistry).registerEventSource(any(EventSource.class));
174     }
175
176 }