2 * Copyright (c) 2016, 2017 Pantheon Technologies s.r.o. 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.openflowplugin.applications.frm.impl;
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.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
24 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 * Test for {@link DeviceMastershipManager}.
30 @RunWith(MockitoJUnitRunner.class)
31 public class DeviceMastershipManagerTest {
32 private DeviceMastershipManager deviceMastershipManager;
34 private ClusterSingletonServiceRegistration registration;
36 private ClusterSingletonServiceProvider clusterSingletonService;
38 private FlowNodeReconciliation reconciliationAgent;
40 private DataBroker dataBroker;
42 private MastershipChangeServiceManager mastershipChangeServiceManager;
44 private DeviceInfo deviceInfo;
46 private NodeId nodeId;
48 private RpcProviderService rpcProviderService;
52 deviceMastershipManager = new DeviceMastershipManager(reconciliationAgent, dataBroker,
53 mastershipChangeServiceManager, rpcProviderService);
54 Mockito.lenient().when(clusterSingletonService
55 .registerClusterSingletonService(ArgumentMatchers.any()))
56 .thenReturn(registration);
57 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
58 Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
62 public void testOnDeviceConnectedAndDisconnected() {
64 Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
65 deviceMastershipManager.onBecomeOwner(deviceInfo);
66 DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId());
67 Assert.assertNotNull(serviceInstance);
68 // destroy context - unregister
69 Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
70 deviceMastershipManager.onLoseOwnership(deviceInfo);
71 Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
75 public void testIsDeviceMasteredOrSlaved() {
77 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
78 deviceMastershipManager.onBecomeOwner(deviceInfo);
80 deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
81 Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
83 deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
84 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));