c4d509d6bd8cff29e65a12272bd5a6705c9fa72c
[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.Matchers;
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.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
58                 .thenReturn(registration);
59         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
60         Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
61     }
62
63     @Test
64     public void testOnDeviceConnectedAndDisconnected() throws Exception {
65         // no context
66         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
67         deviceMastershipManager.onBecomeOwner(deviceInfo);
68         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId());
69         Assert.assertNotNull(serviceInstance);
70         // destroy context - unregister
71         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
72         deviceMastershipManager.onLoseOwnership(deviceInfo);
73         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
74     }
75
76     @Test
77     public void testIsDeviceMasteredOrSlaved() {
78         // no context
79         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
80         deviceMastershipManager.onBecomeOwner(deviceInfo);
81         // is master
82         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
83         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
84         // is not master
85         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
86         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
87     }
88 }