55de43760c581205d832cd4dcaadb67c28ebbccb
[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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.ArgumentMatchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
26 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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 RoutedRpcRegistration routedRpcReg;
45     @Mock
46     private MastershipChangeServiceManager mastershipChangeServiceManager;
47     @Mock
48     private DeviceInfo deviceInfo;
49     @Mock
50     private NodeId nodeId;
51
52     @Before
53     public void setUp() throws Exception {
54         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, reconciliationAgent, dataBroker,
55                 mastershipChangeServiceManager);
56         deviceMastershipManager.setRoutedRpcReg(routedRpcReg);
57         Mockito.lenient().when(clusterSingletonService
58                 .registerClusterSingletonService(ArgumentMatchers.<ClusterSingletonService>any()))
59                 .thenReturn(registration);
60         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
61         Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
62     }
63
64     @Test
65     public void testOnDeviceConnectedAndDisconnected() throws Exception {
66         // no context
67         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
68         deviceMastershipManager.onBecomeOwner(deviceInfo);
69         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId());
70         Assert.assertNotNull(serviceInstance);
71         // destroy context - unregister
72         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
73         deviceMastershipManager.onLoseOwnership(deviceInfo);
74         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
75     }
76
77     @Test
78     public void testIsDeviceMasteredOrSlaved() {
79         // no context
80         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
81         deviceMastershipManager.onBecomeOwner(deviceInfo);
82         // is master
83         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
84         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
85         // is not master
86         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
87         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
88     }
89 }