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;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentMatcher;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
27 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
28 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.openflowplugin.api.OFConstants;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
34 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
35 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
36 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 * Created by kramesha on 9/1/15.
55 @RunWith(MockitoJUnitRunner.class)
56 public class RoleContextImplTest {
58 public static final int FUTURE_SAFETY_TIMEOUT = 5;
60 private EntityOwnershipService entityOwnershipService;
63 private DataBroker dataBroker;
66 private RpcProviderRegistry rpcProviderRegistry;
69 private DeviceContext deviceContext;
72 private ConnectionContext connectionContext;
75 private DeviceState deviceState;
78 private SalRoleService salRoleService;
81 private GetFeaturesOutput getFeaturesOutput;
84 private FeaturesReply featuresReply;
86 private MessageSpy mockedMessageSpy;
88 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
89 private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
90 private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
91 private final Entity txEntity = new Entity(RoleManager.TX_ENTITY_TYPE, nodeId.getValue());
92 private RoleContextImpl roleContext;
95 public void setup() throws CandidateAlreadyRegisteredException {
96 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
97 when(deviceContext.getDeviceState()).thenReturn(deviceState);
98 when(deviceContext.getMessageSpy()).thenReturn(mockedMessageSpy);
99 when(connectionContext.getNodeId()).thenReturn(nodeId);
100 when(deviceState.getNodeInstanceIdentifier()).thenReturn(instanceIdentifier);
101 when(deviceState.getNodeId()).thenReturn(nodeId);
102 when(rpcProviderRegistry.getRpcService(SalRoleService.class)).thenReturn(salRoleService);
103 when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
104 when(getFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
105 when(deviceContext.getPrimaryConnectionContext().getFeatures()).thenReturn(featuresReply);
106 when(deviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
107 when(deviceContext.onClusterRoleChange(Matchers.<OfpRole>any(), Matchers.<OfpRole>any()))
108 .thenReturn(Futures.immediateFuture((Void) null));
110 roleContext = new RoleContextImpl(deviceContext, entityOwnershipService, entity, txEntity);
111 roleContext.initializationRoleContext();
115 public void testOnRoleChangedStartingMaster() throws InterruptedException, ExecutionException, TimeoutException {
116 final OfpRole oldRole = OfpRole.BECOMESLAVE;
117 final OfpRole newRole = OfpRole.BECOMEMASTER;
119 final SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
120 future.set(RpcResultBuilder.<SetRoleOutput>success().build());
121 when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
124 roleContext.setSalRoleService(salRoleService);
126 final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
127 onRoleChanged.get(FUTURE_SAFETY_TIMEOUT, TimeUnit.SECONDS);
129 verify(deviceContext).onClusterRoleChange(oldRole, newRole);
133 public void testOnRoleChangedStartingSlave() throws InterruptedException, ExecutionException, TimeoutException {
134 final OfpRole oldRole = OfpRole.BECOMEMASTER;
135 final OfpRole newRole = OfpRole.BECOMESLAVE;
137 final SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
138 future.set(RpcResultBuilder.<SetRoleOutput>success().build());
139 when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
142 roleContext.setSalRoleService(salRoleService);
144 final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
145 onRoleChanged.get(5, TimeUnit.SECONDS);
147 verify(deviceContext).onClusterRoleChange(oldRole, newRole);
151 public void testOnRoleChangedWorkingMaster() throws InterruptedException, ExecutionException, TimeoutException {
152 final OfpRole oldRole = OfpRole.BECOMESLAVE;
153 final OfpRole newRole = OfpRole.BECOMEMASTER;
155 final ListenableFuture<RpcResult<SetRoleOutput>> future =
156 RpcResultBuilder.success(new SetRoleOutputBuilder().build()).buildFuture();
157 when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
160 roleContext.setSalRoleService(salRoleService);
162 final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
163 onRoleChanged.get(5, TimeUnit.SECONDS);
165 verify(deviceContext).onClusterRoleChange(oldRole, newRole);
169 public void testOnRoleChangedWorkingSlave() throws InterruptedException, ExecutionException, TimeoutException {
170 final OfpRole oldRole = OfpRole.BECOMEMASTER;
171 final OfpRole newRole = OfpRole.BECOMESLAVE;
173 final SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
174 future.set(RpcResultBuilder.<SetRoleOutput>success().build());
175 when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
178 roleContext.setSalRoleService(salRoleService);
180 final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
181 onRoleChanged.get(5, TimeUnit.SECONDS);
183 verify(deviceContext).onClusterRoleChange(oldRole, newRole);
186 private class SetRoleInputMatcher extends ArgumentMatcher<SetRoleInput> {
188 private final OfpRole ofpRole;
189 private final NodeRef nodeRef;
191 public SetRoleInputMatcher(final OfpRole ofpRole, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
192 this.ofpRole = ofpRole;
193 nodeRef = new NodeRef(instanceIdentifier);
198 public boolean matches(final Object o) {
199 final SetRoleInput input = (SetRoleInput) o;
200 if (input.getControllerRole() == ofpRole &&
201 input.getNode().equals(nodeRef)) {