BUG 5523 RoleCtx changes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.impl.role;
9
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;
51
52 /**
53  * Created by kramesha on 9/1/15.
54  */
55 @RunWith(MockitoJUnitRunner.class)
56 public class RoleContextImplTest {
57
58     public static final int FUTURE_SAFETY_TIMEOUT = 5;
59     @Mock
60     private EntityOwnershipService entityOwnershipService;
61
62     @Mock
63     private DataBroker dataBroker;
64
65     @Mock
66     private RpcProviderRegistry rpcProviderRegistry;
67
68     @Mock
69     private DeviceContext deviceContext;
70
71     @Mock
72     private ConnectionContext connectionContext;
73
74     @Mock
75     private DeviceState deviceState;
76
77     @Mock
78     private SalRoleService salRoleService;
79
80     @Mock
81     private GetFeaturesOutput getFeaturesOutput;
82
83     @Mock
84     private FeaturesReply featuresReply;
85     @Mock
86     private MessageSpy mockedMessageSpy;
87
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;
93
94     @Before
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));
109
110         roleContext = new RoleContextImpl(deviceContext, entityOwnershipService, entity, txEntity);
111         roleContext.initializationRoleContext();
112     }
113
114     @Test
115     public void testOnRoleChangedStartingMaster() throws InterruptedException, ExecutionException, TimeoutException {
116         final OfpRole oldRole = OfpRole.BECOMESLAVE;
117         final OfpRole newRole = OfpRole.BECOMEMASTER;
118
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))))
122                 .thenReturn(future);
123
124         roleContext.setSalRoleService(salRoleService);
125
126         final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
127         onRoleChanged.get(FUTURE_SAFETY_TIMEOUT, TimeUnit.SECONDS);
128
129         verify(deviceContext).onClusterRoleChange(oldRole, newRole);
130     }
131
132     @Test
133     public void testOnRoleChangedStartingSlave() throws InterruptedException, ExecutionException, TimeoutException {
134         final OfpRole oldRole = OfpRole.BECOMEMASTER;
135         final OfpRole newRole = OfpRole.BECOMESLAVE;
136
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))))
140                 .thenReturn(future);
141
142         roleContext.setSalRoleService(salRoleService);
143
144         final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
145         onRoleChanged.get(5, TimeUnit.SECONDS);
146
147         verify(deviceContext).onClusterRoleChange(oldRole, newRole);
148     }
149
150     @Test
151     public void testOnRoleChangedWorkingMaster() throws InterruptedException, ExecutionException, TimeoutException {
152         final OfpRole oldRole = OfpRole.BECOMESLAVE;
153         final OfpRole newRole = OfpRole.BECOMEMASTER;
154
155         final ListenableFuture<RpcResult<SetRoleOutput>> future =
156                 RpcResultBuilder.success(new SetRoleOutputBuilder().build()).buildFuture();
157         when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
158                 .thenReturn(future);
159
160         roleContext.setSalRoleService(salRoleService);
161
162         final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
163         onRoleChanged.get(5, TimeUnit.SECONDS);
164
165         verify(deviceContext).onClusterRoleChange(oldRole, newRole);
166     }
167
168     @Test
169     public void testOnRoleChangedWorkingSlave() throws InterruptedException, ExecutionException, TimeoutException {
170         final OfpRole oldRole = OfpRole.BECOMEMASTER;
171         final OfpRole newRole = OfpRole.BECOMESLAVE;
172
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))))
176                 .thenReturn(future);
177
178         roleContext.setSalRoleService(salRoleService);
179
180         final ListenableFuture<Void> onRoleChanged = roleContext.onRoleChanged(oldRole, newRole);
181         onRoleChanged.get(5, TimeUnit.SECONDS);
182
183         verify(deviceContext).onClusterRoleChange(oldRole, newRole);
184     }
185
186     private class SetRoleInputMatcher extends ArgumentMatcher<SetRoleInput> {
187
188         private final OfpRole ofpRole;
189         private final NodeRef nodeRef;
190
191         public SetRoleInputMatcher(final OfpRole ofpRole, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
192             this.ofpRole = ofpRole;
193             nodeRef = new NodeRef(instanceIdentifier);
194
195         }
196
197         @Override
198         public boolean matches(final Object o) {
199             final SetRoleInput input = (SetRoleInput) o;
200             if (input.getControllerRole() == ofpRole &&
201                     input.getNode().equals(nodeRef)) {
202                 return true;
203             }
204             return false;
205         }
206     }
207 }