2 * Copyright (c) 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.impl.role;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
15 import com.google.common.util.concurrent.Futures;
16 import io.netty.util.HashedWheelTimer;
17 import java.util.concurrent.Future;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
28 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
29 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
30 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
39 @RunWith(MockitoJUnitRunner.class)
40 public class RoleContextImplTest {
42 private SalRoleService roleService;
44 private ContextChainMastershipWatcher contextChainMastershipWatcher;
46 private DeviceInfo deviceInfo;
48 private DeviceContext deviceContext;
50 private Future<RpcResult<SetRoleOutput>> setRoleFuture;
51 private RoleContext roleContext;
54 public void setUp() throws Exception {
55 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil
56 .createNodeInstanceIdentifier(new NodeId("openflow:1")));
57 when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
58 when(roleService.setRole(any())).thenReturn(Futures.immediateFuture(null));
60 roleContext = new RoleContextImpl(deviceInfo, new HashedWheelTimer(), 20000);
61 roleContext.registerMastershipWatcher(contextChainMastershipWatcher);
62 roleContext.setRoleService(roleService);
66 public void tearDown() throws Exception {
71 public void instantiateServiceInstance() throws Exception {
72 roleContext.instantiateServiceInstance();
73 verify(roleService).setRole(new SetRoleInputBuilder()
74 .setControllerRole(OfpRole.BECOMEMASTER)
75 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
77 verify(contextChainMastershipWatcher).onMasterRoleAcquired(
79 ContextChainMastershipState.MASTER_ON_DEVICE);
83 public void closeServiceInstance() throws Exception {
84 when(setRoleFuture.isCancelled()).thenReturn(false);
85 when(setRoleFuture.isDone()).thenReturn(false);
86 when(roleService.setRole(any())).thenReturn(setRoleFuture);
87 roleContext.instantiateServiceInstance();
88 roleContext.closeServiceInstance().get();
89 verify(setRoleFuture).cancel(true);