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.SettableFuture;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.ArgumentMatcher;
16 import org.mockito.Matchers;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 * Created by kramesha on 9/1/15.
43 public class RoleContextImplTest {
46 private EntityOwnershipService entityOwnershipService;
49 private OpenflowOwnershipListener openflowOwnershipListener;
52 private RpcProviderRegistry rpcProviderRegistry;
55 private DeviceContext deviceContext;
58 private ConnectionContext connectionContext;
61 private DeviceState deviceState;
64 private SalRoleService salRoleService;
67 private GetFeaturesOutput getFeaturesOutput;
70 private FeaturesReply featuresReply;
72 private NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
73 private KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
77 MockitoAnnotations.initMocks(this);
78 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
79 when(deviceContext.getDeviceState()).thenReturn(deviceState);
80 when(connectionContext.getNodeId()).thenReturn(nodeId);
81 when(deviceState.getNodeInstanceIdentifier()).thenReturn(instanceIdentifier);
82 when(rpcProviderRegistry.getRpcService(SalRoleService.class)).thenReturn(salRoleService);
83 when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
84 when(getFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
85 when(deviceContext.getPrimaryConnectionContext().getFeatures()).thenReturn(featuresReply);
86 when(deviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
90 public void testOnRoleChanged() {
91 OfpRole newRole = OfpRole.BECOMEMASTER;
93 SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
94 future.set(RpcResultBuilder.<SetRoleOutput>success().build());
95 when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
98 RoleContextImpl roleContext = new RoleContextImpl(deviceContext, rpcProviderRegistry, entityOwnershipService, openflowOwnershipListener);
99 roleContext.setSalRoleService(salRoleService);
101 roleContext.onRoleChanged(OfpRole.BECOMESLAVE, newRole);
103 verify(deviceState).setRole(newRole);
107 private class SetRoleInputMatcher extends ArgumentMatcher<SetRoleInput> {
109 private OfpRole ofpRole;
110 private NodeRef nodeRef;
111 public SetRoleInputMatcher(OfpRole ofpRole, KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
112 this.ofpRole = ofpRole;
113 nodeRef = new NodeRef(instanceIdentifier);
118 public boolean matches(Object o) {
119 SetRoleInput input = (SetRoleInput) o;
120 if (input.getControllerRole() == ofpRole &&
121 input.getNode().equals(nodeRef)) {