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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
31 @RunWith(MockitoJUnitRunner.class)
32 public class RoleContextImplTest {
35 HashedWheelTimer hashedWheelTimer;
37 private DeviceInfo deviceInfo;
39 private RoleManager roleManager;
41 private LifecycleService lifecycleService;
43 private SalRoleService salRoleService;
45 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
46 private RoleContext roleContext;
47 private RoleContextImpl roleContextSpy;
50 public void setup() throws CandidateAlreadyRegisteredException {
51 roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager);
52 roleContext.setSalRoleService(salRoleService);
53 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
54 Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
55 roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
59 public void testCreateRequestContext() throws Exception {
60 roleContext.createRequestContext();
61 Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
64 @Test(expected = NullPointerException.class)
65 public void testSetSalRoleService() throws Exception {
66 roleContext.setSalRoleService(null);
70 public void testGetNodeId() throws Exception {
71 Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
75 public void startupClusterServices() throws Exception {
76 Mockito.when(deviceInfo.getVersion()).thenReturn(null);
77 roleContextSpy.startupClusterServices();
78 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
82 public void startupClusterServicesVersion10() throws Exception {
83 Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
84 roleContextSpy.startupClusterServices();
85 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
89 public void startupClusterServicesVersion13() throws Exception {
90 Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
91 roleContextSpy.startupClusterServices();
92 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
96 public void stopClusterServicesNotDisconnected() throws Exception {
97 roleContextSpy.stopClusterServices(false);
98 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
99 Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
103 public void stopClusterServicesDisconnected() throws Exception {
104 roleContextSpy.stopClusterServices(true);
105 Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
109 public void makeDeviceSlave() throws Exception {
110 roleContextSpy.makeDeviceSlave();
111 Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);