Switch to MD-SAL APIs
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManagerTest.java
1 /*
2  * Copyright (c) 2016, 2017 Pantheon Technologies s.r.o. 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.openflowplugin.applications.frm.impl;
9
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.ArgumentMatchers;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.junit.MockitoJUnitRunner;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.RpcProviderService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
24 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
25 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
28
29 /**
30  * Test for {@link DeviceMastershipManager}.
31  */
32 @RunWith(MockitoJUnitRunner.class)
33 public class DeviceMastershipManagerTest {
34     private DeviceMastershipManager deviceMastershipManager;
35     @Mock
36     private ClusterSingletonServiceRegistration registration;
37     @Mock
38     private ClusterSingletonServiceProvider clusterSingletonService;
39     @Mock
40     private FlowNodeReconciliation reconciliationAgent;
41     @Mock
42     private DataBroker dataBroker;
43     @Mock
44     private MastershipChangeServiceManager mastershipChangeServiceManager;
45     @Mock
46     private DeviceInfo deviceInfo;
47     @Mock
48     private NodeId nodeId;
49     @Mock
50     private RpcProviderService rpcProviderService;
51     @Mock
52     private FrmReconciliationService reconciliationService;
53
54     @Before
55     public void setUp() throws Exception {
56         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, reconciliationAgent, dataBroker,
57                 mastershipChangeServiceManager, rpcProviderService, reconciliationService);
58         Mockito.lenient().when(clusterSingletonService
59                 .registerClusterSingletonService(ArgumentMatchers.<ClusterSingletonService>any()))
60                 .thenReturn(registration);
61         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
62         Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
63     }
64
65     @Test
66     public void testOnDeviceConnectedAndDisconnected() throws Exception {
67         // no context
68         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
69         deviceMastershipManager.onBecomeOwner(deviceInfo);
70         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId());
71         Assert.assertNotNull(serviceInstance);
72         // destroy context - unregister
73         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
74         deviceMastershipManager.onLoseOwnership(deviceInfo);
75         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
76     }
77
78     @Test
79     public void testIsDeviceMasteredOrSlaved() {
80         // no context
81         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
82         deviceMastershipManager.onBecomeOwner(deviceInfo);
83         // is master
84         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
85         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
86         // is not master
87         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
88         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
89     }
90 }