2 * Copyright (c) 2013 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.openflow.md.core;
10 import java.util.ArrayList;
11 import java.util.List;
13 import org.junit.After;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.sal.common.util.Futures;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.collect.Lists;
44 @RunWith(MockitoJUnitRunner.class)
45 public class HandshakeManagerImplTest {
47 private static final Logger LOG = LoggerFactory
48 .getLogger(HandshakeManagerImplTest.class);
50 private HandshakeManagerImpl handshakeManager;
52 private ConnectionAdapter adapter;
54 private ErrorHandler errorHandler;
56 private HandshakeListener handshakeListener;
58 private RpcResult<GetFeaturesOutput> resultFeatures;
60 private long helloXid = 42L;
62 private int expectedErrors = 0;
65 * invoked before every test method
69 handshakeManager = new HandshakeManagerImpl(adapter, OFConstants.OFP_VERSION_1_3,
70 ConnectionConductor.versionOrder);
71 handshakeManager.setErrorHandler(errorHandler);
72 handshakeManager.setHandshakeListener(handshakeListener);
73 handshakeManager.setUseVersionBitmap(false);
75 resultFeatures = RpcResultsUtil.createRpcResult(true, new GetFeaturesOutputBuilder().build(), null);
77 Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
78 .thenReturn(Futures.immediateFuture(RpcResultsUtil.createRpcResult(true, (Void) null, null)));
82 * invoked after each test method
85 public void teardown() {
86 // logging errors if occurred
87 ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
88 Mockito.verify(errorHandler, Mockito.atMost(1)).handleException(
89 errorCaptor.capture(), Matchers.any(SessionContext.class));
90 for (Throwable problem : errorCaptor.getAllValues()) {
91 LOG.warn(problem.getMessage(), problem);
94 Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
95 Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
99 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeCommonBitmapVersion(java.util.List)}.
102 public void testProposeCommonBitmapVersion() {
103 Boolean[][] versions = new Boolean[][] {
104 {true, true, true, false, false, false},
105 {true, true, true, false, false}
108 for (Boolean[] verasionList : versions) {
109 ElementsBuilder elementsBuilder = new ElementsBuilder();
110 elementsBuilder.setVersionBitmap(Lists.newArrayList(verasionList));
111 Elements element = elementsBuilder.build();
112 List<Elements> elements = Lists.newArrayList(element );
113 Short proposal = handshakeManager.proposeCommonBitmapVersion(elements);
114 Assert.assertEquals(Short.valueOf((short)1), proposal);
119 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeNextVersion(short)}.
122 public void testProposeNextVersion() {
123 short[] remoteVer = new short[] { 0x05, 0x04, 0x03, 0x02, 0x01, 0x8f,
125 short[] expectedProposal = new short[] { 0x04, 0x04, 0x01, 0x01, 0x01,
128 for (int i = 0; i < remoteVer.length; i++) {
129 short actualProposal = handshakeManager
130 .proposeNextVersion(remoteVer[i]);
132 String.format("proposing for version: %04x", remoteVer[i]),
133 expectedProposal[i], actualProposal);
137 handshakeManager.proposeNextVersion((short) 0);
138 Assert.fail("there should be no proposition for this version");
139 } catch (Exception e) {
144 //////// Version Negotiation Tests //////////////
147 * Test of version negotiation Where switch version = 1.0
152 public void testVersionNegotiation10() throws Exception {
153 LOG.debug("testVersionNegotiation10");
154 Short version = OFConstants.OFP_VERSION_1_0;
156 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
157 .thenReturn(Futures.immediateFuture(resultFeatures));
159 handshakeManager.shake();
161 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
162 handshakeManager.shake();
164 Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
168 * Test of version negotiation Where switch version = 1.0
173 public void testVersionNegotiation10SwitchStarts() throws Exception {
174 LOG.debug("testVersionNegotiation10-ss");
175 Short version = OFConstants.OFP_VERSION_1_0;
177 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
178 .thenReturn(Futures.immediateFuture(resultFeatures));
180 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
181 handshakeManager.shake();
183 Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
187 * Test of version negotiation Where switch version < 1.0
188 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
192 public void testVersionNegotiation00() throws Exception {
193 LOG.debug("testVersionNegotiation00");
195 Short version = (short) 0x00;
197 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
198 handshakeManager.shake();
200 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
201 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
205 * Test of version negotiation Where switch version < 1.0
206 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
210 public void testVersionNegotiation00SwitchStarts() throws Exception {
211 LOG.debug("testVersionNegotiation00-ss");
213 Short version = (short) 0x00;
215 handshakeManager.shake();
217 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
218 handshakeManager.shake();
220 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
221 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
225 * Test of version negotiation Where 1.0 < switch version < 1.3
230 public void testVersionNegotiation11() throws Exception {
231 LOG.debug("testVersionNegotiation11");
232 Short version = (short) 0x02;
233 Short expVersion = (short) 0x01;
235 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
236 .thenReturn(Futures.immediateFuture(resultFeatures));
238 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
239 handshakeManager.shake();
241 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
242 handshakeManager.shake();
244 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
245 resultFeatures.getResult(), expVersion);
249 * Test of version negotiation Where 1.0 < switch version < 1.3
254 public void testVersionNegotiation11SwitchStarts() throws Exception {
255 LOG.debug("testVersionNegotiation11-ss");
256 Short version = (short) 0x02;
257 Short expVersion = (short) 0x01;
259 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
260 .thenReturn(Futures.immediateFuture(resultFeatures));
262 handshakeManager.shake();
264 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
265 handshakeManager.shake();
267 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
268 handshakeManager.shake();
270 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
271 resultFeatures.getResult(), expVersion);
275 * Test of version negotiation Where switch version = 1.3
280 public void testVersionNegotiation13() throws Exception {
281 LOG.debug("testVersionNegotiation13");
282 Short version = OFConstants.OFP_VERSION_1_3;
284 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
285 .thenReturn(Futures.immediateFuture(resultFeatures));
287 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
288 handshakeManager.shake();
290 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
291 resultFeatures.getResult(), version);
295 * Test of version negotiation Where switch version = 1.3
300 public void testVersionNegotiation13SwitchStarts() throws Exception {
301 LOG.debug("testVersionNegotiation13-ss");
302 Short version = OFConstants.OFP_VERSION_1_3;
304 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
305 .thenReturn(Futures.immediateFuture(resultFeatures));
307 handshakeManager.shake();
309 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
310 handshakeManager.shake();
312 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
313 resultFeatures.getResult(), version);
317 * Test of version negotiation Where switch version >= 1.3
322 public void testVersionNegotiation15() throws Exception {
323 LOG.debug("testVersionNegotiation15");
324 Short version = (short) 0x06;
325 Short expVersion = OFConstants.OFP_VERSION_1_3;
327 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
328 .thenReturn(Futures.immediateFuture(resultFeatures));
330 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
331 handshakeManager.shake();
333 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
334 handshakeManager.shake();
336 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
337 resultFeatures.getResult(), expVersion);
341 * Test of version negotiation Where switch version >= 1.3
346 public void testVersionNegotiation15SwitchStart() throws Exception {
347 LOG.debug("testVersionNegotiation15-ss");
348 Short version = (short) 0x06;
349 Short expVersion = OFConstants.OFP_VERSION_1_3;
351 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
352 .thenReturn(Futures.immediateFuture(resultFeatures));
354 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
355 handshakeManager.shake();
357 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
358 handshakeManager.shake();
360 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
361 resultFeatures.getResult(), expVersion);
365 * Test of version negotiation Where switch version > 1.3
370 public void testVersionNegotiation15_MultipleCall() throws Exception {
371 LOG.debug("testVersionNegotiation15_MultipleCall");
372 Short version = (short) 0x06;
375 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
376 handshakeManager.shake();
378 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
379 handshakeManager.shake();
381 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
382 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
386 * Test of version negotiation Where switch version > 1.3
391 public void testVersionNegotiation15_MultipleCallSwitchStarts() throws Exception {
392 LOG.debug("testVersionNegotiation15_MultipleCall-ss");
393 Short version = (short) 0x06;
396 handshakeManager.shake();
398 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
399 handshakeManager.shake();
401 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
402 handshakeManager.shake();
404 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
405 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
409 * Test of version negotiation Where bitmap version {0x05,0x01}
414 public void testVersionNegotiation10InBitmap() throws Exception {
415 LOG.debug("testVersionNegotiation10InBitmap");
416 Short version = OFConstants.OFP_VERSION_1_0;
417 handshakeManager.setUseVersionBitmap(true);
419 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
420 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
422 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
423 .thenReturn(Futures.immediateFuture(resultFeatures));
425 handshakeManager.setReceivedHello(helloMessage.build());
426 handshakeManager.shake();
428 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
429 resultFeatures.getResult(), version);
433 * Test of version negotiation Where bitmap version {0x05,0x01}
438 public void testVersionNegotiation10InBitmapSwitchStarts() throws Exception {
439 LOG.debug("testVersionNegotiation10InBitmap-ss");
440 Short version = OFConstants.OFP_VERSION_1_0;
441 handshakeManager.setUseVersionBitmap(true);
443 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
444 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
446 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
447 .thenReturn(Futures.immediateFuture(resultFeatures));
449 handshakeManager.shake();
451 handshakeManager.setReceivedHello(helloMessage.build());
452 handshakeManager.shake();
454 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
455 resultFeatures.getResult(), version);
459 * Test of version negotiation Where bitmap version {0x05,0x04}
464 public void testVersionNegotiation13InBitmap() throws Exception {
465 LOG.debug("testVersionNegotiation13InBitmap");
466 Short version = OFConstants.OFP_VERSION_1_3;
467 handshakeManager.setUseVersionBitmap(true);
469 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
470 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
472 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
473 .thenReturn(Futures.immediateFuture(resultFeatures));
475 handshakeManager.setReceivedHello(helloMessage.build());
476 handshakeManager.shake();
478 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
479 resultFeatures.getResult(), version);
483 * Test of version negotiation Where bitmap version {0x05,0x04}
488 public void testVersionNegotiation13InBitmapSwitchFirst() throws Exception {
489 LOG.debug("testVersionNegotiation13InBitmap-ss");
490 Short version = OFConstants.OFP_VERSION_1_3;
491 handshakeManager.setUseVersionBitmap(true);
493 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
494 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
496 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
497 .thenReturn(Futures.immediateFuture(resultFeatures));
499 handshakeManager.shake();
501 handshakeManager.setReceivedHello(helloMessage.build());
502 handshakeManager.shake();
504 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
505 resultFeatures.getResult(), version);
509 * Test of version negotiation Where bitmap version {0x05,0x02}
514 public void testVersionNegotiationNoCommonVersionInBitmap() throws Exception {
515 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap");
516 Short version = (short) 0x05;
518 handshakeManager.setUseVersionBitmap(true);
520 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
521 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
523 handshakeManager.setReceivedHello(helloMessage.build());
524 handshakeManager.shake();
526 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
527 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
531 * Test of version negotiation Where bitmap version {0x05,0x02}
536 public void testVersionNegotiationNoCommonVersionInBitmapSwitchStarts() throws Exception {
537 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap-ss");
538 Short version = (short) 0x05;
540 handshakeManager.setUseVersionBitmap(true);
542 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
543 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
545 handshakeManager.shake();
547 handshakeManager.setReceivedHello(helloMessage.build());
548 handshakeManager.shake();
550 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
551 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
556 * @param ofpVersion10
560 private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
561 return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
565 * @param versionOrder
566 * @param helloBuilder
569 private static HelloMessageBuilder addVersionBitmap(List<Short> versionOrder,
570 HelloMessageBuilder helloBuilder) {
571 short highestVersion = versionOrder.get(0);
572 int elementsCount = highestVersion / Integer.SIZE;
573 ElementsBuilder elementsBuilder = new ElementsBuilder();
575 List<Elements> elementList = new ArrayList<>();
576 int orderIndex = versionOrder.size();
577 int value = versionOrder.get(--orderIndex);
578 for (int index = 0; index <= elementsCount; index++) {
579 List<Boolean> booleanList = new ArrayList<>();
580 for (int i = 0; i < Integer.SIZE; i++) {
581 if (value == ((index * Integer.SIZE) + i)) {
582 booleanList.add(true);
583 value = (orderIndex == 0) ? highestVersion : versionOrder.get(--orderIndex);
585 booleanList.add(false);
588 elementsBuilder.setType(HelloElementType.forValue(1));
589 elementsBuilder.setVersionBitmap(booleanList);
590 elementList.add(elementsBuilder.build());
593 helloBuilder.setElements(elementList);