OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.role;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
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;
39
40 @RunWith(MockitoJUnitRunner.class)
41 public class RoleContextImplTest {
42     @Mock
43     private SalRoleService roleService;
44     @Mock
45     private ContextChainMastershipWatcher contextChainMastershipWatcher;
46     @Mock
47     private DeviceInfo deviceInfo;
48     @Mock
49     private DeviceContext deviceContext;
50     @Mock
51     private ListenableFuture<RpcResult<SetRoleOutput>> setRoleFuture;
52     @Mock
53     private OpenflowProviderConfig config;
54     private RoleContext roleContext;
55
56     @Before
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));
62
63         roleContext = new RoleContextImpl(deviceInfo, new HashedWheelTimer(), 20000, config);
64         roleContext.registerMastershipWatcher(contextChainMastershipWatcher);
65         roleContext.setRoleService(roleService);
66     }
67
68     @After
69     public void tearDown() throws Exception {
70         roleContext.close();
71     }
72
73     @Test
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()))
79                 .build());
80         verify(contextChainMastershipWatcher).onMasterRoleAcquired(
81                 deviceInfo,
82                 ContextChainMastershipState.MASTER_ON_DEVICE);
83     }
84
85     @Test
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);
93     }
94
95 }