2 * Copyright (c) 2015 Cisco Systems, Inc. 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.impl.role;
11 import com.google.common.util.concurrent.Futures;
12 import io.netty.util.HashedWheelTimer;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
24 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
25 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
26 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
32 @RunWith(MockitoJUnitRunner.class)
33 public class RoleContextImplTest {
36 HashedWheelTimer hashedWheelTimer;
38 private DeviceInfo deviceInfo;
40 private RoleManager roleManager;
42 private LifecycleService lifecycleService;
44 private SalRoleService salRoleService;
46 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
47 private RoleContext roleContext;
48 private RoleContextImpl roleContextSpy;
51 public void setup() throws CandidateAlreadyRegisteredException {
52 roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager, lifecycleService);
53 roleContext.setSalRoleService(salRoleService);
54 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
55 Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
56 Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil.createNodeInstanceIdentifier(nodeId));
57 roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
61 public void testCreateRequestContext() throws Exception {
62 roleContext.createRequestContext();
63 Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
66 @Test(expected = NullPointerException.class)
67 public void testSetSalRoleService() throws Exception {
68 roleContext.setSalRoleService(null);
72 public void testGetNodeId() throws Exception {
73 Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
77 public void startupClusterServices() throws Exception {
78 roleContextSpy.startupClusterServices();
79 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
83 public void startupClusterServicesVersion10() throws Exception {
84 Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
85 roleContextSpy.startupClusterServices();
86 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
90 public void startupClusterServicesVersion13() throws Exception {
91 Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
92 roleContextSpy.startupClusterServices();
93 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
97 public void stopClusterServicesNotDisconnected() throws Exception {
98 roleContextSpy.stopClusterServices(false);
99 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
100 Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
104 public void stopClusterServicesDisconnected() throws Exception {
105 roleContextSpy.stopClusterServices(true);
106 Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
110 public void makeDeviceSlave() throws Exception {
111 roleContextSpy.makeDeviceSlave();
112 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);