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;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
29 * Test for {@link DeviceMastershipManager}.
31 @RunWith(MockitoJUnitRunner.class)
32 public class DeviceMastershipManagerTest {
33 private DeviceMastershipManager deviceMastershipManager;
35 private ClusterSingletonServiceRegistration registration;
37 private ClusterSingletonServiceProvider clusterSingletonService;
39 private FlowNodeReconciliation reconciliationAgent;
41 private DataBroker dataBroker;
43 private MastershipChangeServiceManager mastershipChangeServiceManager;
45 private DeviceInfo deviceInfo;
47 private NodeId nodeId;
49 private RpcProviderService rpcProviderService;
51 private FrmReconciliationService reconciliationService;
55 deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, reconciliationAgent, dataBroker,
56 mastershipChangeServiceManager, rpcProviderService, reconciliationService);
57 Mockito.lenient().when(clusterSingletonService
58 .registerClusterSingletonService(ArgumentMatchers.any()))
59 .thenReturn(registration);
60 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
61 Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
65 public void testOnDeviceConnectedAndDisconnected() {
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()));
78 public void testIsDeviceMasteredOrSlaved() {
80 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
81 deviceMastershipManager.onBecomeOwner(deviceInfo);
83 deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
84 Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
86 deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
87 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));