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.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
28 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 * Created by kramesha on 9/1/15.
46 public class RoleContextImplTest {
49 private EntityOwnershipService entityOwnershipService;
52 private DataBroker dataBroker;
55 private RpcProviderRegistry rpcProviderRegistry;
58 private DeviceContext deviceContext;
61 private ConnectionContext connectionContext;
64 private DeviceState deviceState;
67 private SalRoleService salRoleService;
70 private GetFeaturesOutput getFeaturesOutput;
73 private FeaturesReply featuresReply;
75 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
76 private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
77 private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
81 MockitoAnnotations.initMocks(this);
82 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
83 when(deviceContext.getDeviceState()).thenReturn(deviceState);
84 when(connectionContext.getNodeId()).thenReturn(nodeId);
85 when(deviceState.getNodeInstanceIdentifier()).thenReturn(instanceIdentifier);
86 when(rpcProviderRegistry.getRpcService(SalRoleService.class)).thenReturn(salRoleService);
87 when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
88 when(getFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
89 when(deviceContext.getPrimaryConnectionContext().getFeatures()).thenReturn(featuresReply);
90 when(deviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
94 public void testOnRoleChanged() {
95 final OfpRole newRole = OfpRole.BECOMEMASTER;
97 final SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
98 future.set(RpcResultBuilder.<SetRoleOutput>success().build());
99 when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
102 final RoleContextImpl roleContext = new RoleContextImpl(deviceContext, entityOwnershipService, entity);
103 roleContext.setSalRoleService(salRoleService);
105 roleContext.onRoleChanged(OfpRole.BECOMESLAVE, newRole);
107 verify(deviceState).setRole(newRole);
111 private class SetRoleInputMatcher extends ArgumentMatcher<SetRoleInput> {
113 private final OfpRole ofpRole;
114 private final NodeRef nodeRef;
115 public SetRoleInputMatcher(final OfpRole ofpRole, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
116 this.ofpRole = ofpRole;
117 nodeRef = new NodeRef(instanceIdentifier);
122 public boolean matches(final Object o) {
123 final SetRoleInput input = (SetRoleInput) o;
124 if (input.getControllerRole() == ofpRole &&
125 input.getNode().equals(nodeRef)) {