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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
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;
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 RoutedRpcRegistration routedRpcReg;
46 private MastershipChangeServiceManager mastershipChangeServiceManager;
48 private DeviceInfo deviceInfo;
50 private NodeId nodeId;
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");
65 public void testOnDeviceConnectedAndDisconnected() throws Exception {
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()));