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.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;
30 * Test for {@link DeviceMastershipManager}.
32 @RunWith(MockitoJUnitRunner.class)
33 public class DeviceMastershipManagerTest {
34 private DeviceMastershipManager deviceMastershipManager;
36 private ClusterSingletonServiceRegistration registration;
38 private ClusterSingletonServiceProvider clusterSingletonService;
40 private FlowNodeReconciliation reconciliationAgent;
42 private DataBroker dataBroker;
44 private MastershipChangeServiceManager mastershipChangeServiceManager;
46 private DeviceInfo deviceInfo;
48 private NodeId nodeId;
50 private RpcProviderService rpcProviderService;
52 private FrmReconciliationService reconciliationService;
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");
66 public void testOnDeviceConnectedAndDisconnected() throws Exception {
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()));
79 public void testIsDeviceMasteredOrSlaved() {
81 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
82 deviceMastershipManager.onBecomeOwner(deviceInfo);
84 deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
85 Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
87 deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
88 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));