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.ArgumentMatchers.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 com.google.common.util.concurrent.ListenableFuture;
17 import io.netty.util.HashedWheelTimer;
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.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
40 @RunWith(MockitoJUnitRunner.class)
41 public class RoleContextImplTest {
43 private SalRoleService roleService;
45 private ContextChainMastershipWatcher contextChainMastershipWatcher;
47 private DeviceInfo deviceInfo;
49 private DeviceContext deviceContext;
51 private ListenableFuture<RpcResult<SetRoleOutput>> setRoleFuture;
53 private OpenflowProviderConfig config;
54 private RoleContext roleContext;
57 public void setUp() throws Exception {
58 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil
59 .createNodeInstanceIdentifier(new NodeId("openflow:1")));
60 when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
61 when(roleService.setRole(any())).thenReturn(Futures.immediateFuture(null));
63 roleContext = new RoleContextImpl(deviceInfo, new HashedWheelTimer(), 20000, config);
64 roleContext.registerMastershipWatcher(contextChainMastershipWatcher);
65 roleContext.setRoleService(roleService);
69 public void tearDown() throws Exception {
74 public void instantiateServiceInstance() throws Exception {
75 roleContext.instantiateServiceInstance();
76 verify(roleService).setRole(new SetRoleInputBuilder()
77 .setControllerRole(OfpRole.BECOMEMASTER)
78 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
80 verify(contextChainMastershipWatcher).onMasterRoleAcquired(
82 ContextChainMastershipState.MASTER_ON_DEVICE);
86 public void closeServiceInstance() throws Exception {
87 when(setRoleFuture.isCancelled()).thenReturn(false);
88 when(setRoleFuture.isDone()).thenReturn(false);
89 when(roleService.setRole(any())).thenReturn(setRoleFuture);
90 roleContext.instantiateServiceInstance();
91 roleContext.closeServiceInstance().get();
92 verify(setRoleFuture).cancel(true);