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.openflowjava.protocol.api.connection.ConnectionAdapter;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.ErrorHandler;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.collect.Lists;
43 import com.google.common.util.concurrent.Futures;
48 @RunWith(MockitoJUnitRunner.class)
49 public class HandshakeManagerImplTest {
51 private static final Logger LOG = LoggerFactory
52 .getLogger(HandshakeManagerImplTest.class);
54 private HandshakeManagerImpl handshakeManager;
56 private ConnectionAdapter adapter;
58 private ErrorHandler errorHandler;
60 private HandshakeListener handshakeListener;
62 private RpcResult<GetFeaturesOutput> resultFeatures;
64 private long helloXid = 42L;
66 private int expectedErrors = 0;
69 * invoked before every test method
73 handshakeManager = new HandshakeManagerImpl(adapter, OFConstants.OFP_VERSION_1_3,
74 ConnectionConductor.versionOrder);
75 handshakeManager.setErrorHandler(errorHandler);
76 handshakeManager.setHandshakeListener(handshakeListener);
77 handshakeManager.setUseVersionBitmap(false);
79 resultFeatures = RpcResultBuilder.success(new GetFeaturesOutputBuilder().build()).build();
81 Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
82 .thenReturn(Futures.immediateFuture(
83 RpcResultBuilder.success((Void) null).build()));
87 * invoked after each test method
90 public void teardown() {
91 // logging errors if occurred
92 ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
93 Mockito.verify(errorHandler, Mockito.atMost(1)).handleException(
94 errorCaptor.capture(), Matchers.any(SessionContext.class));
95 for (Throwable problem : errorCaptor.getAllValues()) {
96 LOG.warn(problem.getMessage(), problem);
99 Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
100 Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
104 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeCommonBitmapVersion(java.util.List)}.
107 public void testProposeCommonBitmapVersion() {
108 Boolean[][] versions = new Boolean[][] {
109 {true, true, true, false, false, false},
110 {true, true, true, false, false}
113 for (Boolean[] verasionList : versions) {
114 ElementsBuilder elementsBuilder = new ElementsBuilder();
115 elementsBuilder.setVersionBitmap(Lists.newArrayList(verasionList));
116 Elements element = elementsBuilder.build();
117 List<Elements> elements = Lists.newArrayList(element );
118 Short proposal = handshakeManager.proposeCommonBitmapVersion(elements);
119 Assert.assertEquals(Short.valueOf((short)1), proposal);
124 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeNextVersion(short)}.
127 public void testProposeNextVersion() {
128 short[] remoteVer = new short[] { 0x05, 0x04, 0x03, 0x02, 0x01, 0x8f,
130 short[] expectedProposal = new short[] { 0x04, 0x04, 0x01, 0x01, 0x01,
133 for (int i = 0; i < remoteVer.length; i++) {
134 short actualProposal = handshakeManager
135 .proposeNextVersion(remoteVer[i]);
137 String.format("proposing for version: %04x", remoteVer[i]),
138 expectedProposal[i], actualProposal);
142 handshakeManager.proposeNextVersion((short) 0);
143 Assert.fail("there should be no proposition for this version");
144 } catch (Exception e) {
149 //////// Version Negotiation Tests //////////////
152 * Test of version negotiation Where switch version = 1.0
157 public void testVersionNegotiation10() throws Exception {
158 LOG.debug("testVersionNegotiation10");
159 Short version = OFConstants.OFP_VERSION_1_0;
161 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
162 .thenReturn(Futures.immediateFuture(resultFeatures));
164 handshakeManager.shake();
166 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
167 handshakeManager.shake();
169 Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
173 * Test of version negotiation Where switch version = 1.0
178 public void testVersionNegotiation10SwitchStarts() throws Exception {
179 LOG.debug("testVersionNegotiation10-ss");
180 Short version = OFConstants.OFP_VERSION_1_0;
182 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
183 .thenReturn(Futures.immediateFuture(resultFeatures));
185 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
186 handshakeManager.shake();
188 Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
192 * Test of version negotiation Where switch version < 1.0
193 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
197 public void testVersionNegotiation00() throws Exception {
198 LOG.debug("testVersionNegotiation00");
200 Short version = (short) 0x00;
202 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
203 handshakeManager.shake();
205 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
206 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
210 * Test of version negotiation Where switch version < 1.0
211 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
215 public void testVersionNegotiation00SwitchStarts() throws Exception {
216 LOG.debug("testVersionNegotiation00-ss");
218 Short version = (short) 0x00;
220 handshakeManager.shake();
222 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
223 handshakeManager.shake();
225 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
226 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
230 * Test of version negotiation Where 1.0 < switch version < 1.3
235 public void testVersionNegotiation11() throws Exception {
236 LOG.debug("testVersionNegotiation11");
237 Short version = (short) 0x02;
238 Short expVersion = (short) 0x01;
240 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
241 .thenReturn(Futures.immediateFuture(resultFeatures));
243 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
244 handshakeManager.shake();
246 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
247 handshakeManager.shake();
249 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
250 resultFeatures.getResult(), expVersion);
254 * Test of version negotiation Where 1.0 < switch version < 1.3
259 public void testVersionNegotiation11SwitchStarts() throws Exception {
260 LOG.debug("testVersionNegotiation11-ss");
261 Short version = (short) 0x02;
262 Short expVersion = (short) 0x01;
264 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
265 .thenReturn(Futures.immediateFuture(resultFeatures));
267 handshakeManager.shake();
269 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
270 handshakeManager.shake();
272 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
273 handshakeManager.shake();
275 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
276 resultFeatures.getResult(), expVersion);
280 * Test of version negotiation Where switch version = 1.3
285 public void testVersionNegotiation13() throws Exception {
286 LOG.debug("testVersionNegotiation13");
287 Short version = OFConstants.OFP_VERSION_1_3;
289 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
290 .thenReturn(Futures.immediateFuture(resultFeatures));
292 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
293 handshakeManager.shake();
295 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
296 resultFeatures.getResult(), version);
300 * Test of version negotiation Where switch version = 1.3
305 public void testVersionNegotiation13SwitchStarts() throws Exception {
306 LOG.debug("testVersionNegotiation13-ss");
307 Short version = OFConstants.OFP_VERSION_1_3;
309 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
310 .thenReturn(Futures.immediateFuture(resultFeatures));
312 handshakeManager.shake();
314 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
315 handshakeManager.shake();
317 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
318 resultFeatures.getResult(), version);
322 * Test of version negotiation Where switch version >= 1.3
327 public void testVersionNegotiation15() throws Exception {
328 LOG.debug("testVersionNegotiation15");
329 Short version = (short) 0x06;
330 Short expVersion = OFConstants.OFP_VERSION_1_3;
332 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
333 .thenReturn(Futures.immediateFuture(resultFeatures));
335 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
336 handshakeManager.shake();
338 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
339 handshakeManager.shake();
341 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
342 resultFeatures.getResult(), expVersion);
346 * Test of version negotiation Where switch version >= 1.3
351 public void testVersionNegotiation15SwitchStart() throws Exception {
352 LOG.debug("testVersionNegotiation15-ss");
353 Short version = (short) 0x06;
354 Short expVersion = OFConstants.OFP_VERSION_1_3;
356 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
357 .thenReturn(Futures.immediateFuture(resultFeatures));
359 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
360 handshakeManager.shake();
362 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
363 handshakeManager.shake();
365 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
366 resultFeatures.getResult(), expVersion);
370 * Test of version negotiation Where switch version > 1.3
375 public void testVersionNegotiation15_MultipleCall() throws Exception {
376 LOG.debug("testVersionNegotiation15_MultipleCall");
377 Short version = (short) 0x06;
380 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
381 handshakeManager.shake();
383 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
384 handshakeManager.shake();
386 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
387 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
391 * Test of version negotiation Where switch version > 1.3
396 public void testVersionNegotiation15_MultipleCallSwitchStarts() throws Exception {
397 LOG.debug("testVersionNegotiation15_MultipleCall-ss");
398 Short version = (short) 0x06;
401 handshakeManager.shake();
403 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
404 handshakeManager.shake();
406 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
407 handshakeManager.shake();
409 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
410 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
414 * Test of version negotiation Where bitmap version {0x05,0x01}
419 public void testVersionNegotiation10InBitmap() throws Exception {
420 LOG.debug("testVersionNegotiation10InBitmap");
421 Short version = OFConstants.OFP_VERSION_1_0;
422 handshakeManager.setUseVersionBitmap(true);
424 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
425 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
427 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
428 .thenReturn(Futures.immediateFuture(resultFeatures));
430 handshakeManager.setReceivedHello(helloMessage.build());
431 handshakeManager.shake();
433 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
434 resultFeatures.getResult(), version);
438 * Test of version negotiation Where bitmap version {0x05,0x01}
443 public void testVersionNegotiation10InBitmapSwitchStarts() throws Exception {
444 LOG.debug("testVersionNegotiation10InBitmap-ss");
445 Short version = OFConstants.OFP_VERSION_1_0;
446 handshakeManager.setUseVersionBitmap(true);
448 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
449 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
451 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
452 .thenReturn(Futures.immediateFuture(resultFeatures));
454 handshakeManager.shake();
456 handshakeManager.setReceivedHello(helloMessage.build());
457 handshakeManager.shake();
459 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
460 resultFeatures.getResult(), version);
464 * Test of version negotiation Where bitmap version {0x05,0x04}
469 public void testVersionNegotiation13InBitmap() throws Exception {
470 LOG.debug("testVersionNegotiation13InBitmap");
471 Short version = OFConstants.OFP_VERSION_1_3;
472 handshakeManager.setUseVersionBitmap(true);
474 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
475 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
477 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
478 .thenReturn(Futures.immediateFuture(resultFeatures));
480 handshakeManager.setReceivedHello(helloMessage.build());
481 handshakeManager.shake();
483 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
484 resultFeatures.getResult(), version);
488 * Test of version negotiation Where bitmap version {0x05,0x04}
493 public void testVersionNegotiation13InBitmapSwitchFirst() throws Exception {
494 LOG.debug("testVersionNegotiation13InBitmap-ss");
495 Short version = OFConstants.OFP_VERSION_1_3;
496 handshakeManager.setUseVersionBitmap(true);
498 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
499 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
501 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
502 .thenReturn(Futures.immediateFuture(resultFeatures));
504 handshakeManager.shake();
506 handshakeManager.setReceivedHello(helloMessage.build());
507 handshakeManager.shake();
509 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
510 resultFeatures.getResult(), version);
514 * Test of version negotiation Where bitmap version {0x05,0x02}
519 public void testVersionNegotiationNoCommonVersionInBitmap() throws Exception {
520 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap");
521 Short version = (short) 0x05;
523 handshakeManager.setUseVersionBitmap(true);
525 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
526 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
528 handshakeManager.setReceivedHello(helloMessage.build());
529 handshakeManager.shake();
531 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
532 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
536 * Test of version negotiation Where bitmap version {0x05,0x02}
541 public void testVersionNegotiationNoCommonVersionInBitmapSwitchStarts() throws Exception {
542 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap-ss");
543 Short version = (short) 0x05;
545 handshakeManager.setUseVersionBitmap(true);
547 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
548 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
550 handshakeManager.shake();
552 handshakeManager.setReceivedHello(helloMessage.build());
553 handshakeManager.shake();
555 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
556 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
561 * @param ofpVersion10
565 private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
566 return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
570 * @param versionOrder
571 * @param helloBuilder
574 private static HelloMessageBuilder addVersionBitmap(List<Short> versionOrder,
575 HelloMessageBuilder helloBuilder) {
576 short highestVersion = versionOrder.get(0);
577 int elementsCount = highestVersion / Integer.SIZE;
578 ElementsBuilder elementsBuilder = new ElementsBuilder();
580 List<Elements> elementList = new ArrayList<>();
581 int orderIndex = versionOrder.size();
582 int value = versionOrder.get(--orderIndex);
583 for (int index = 0; index <= elementsCount; index++) {
584 List<Boolean> booleanList = new ArrayList<>();
585 for (int i = 0; i < Integer.SIZE; i++) {
586 if (value == ((index * Integer.SIZE) + i)) {
587 booleanList.add(true);
588 value = (orderIndex == 0) ? highestVersion : versionOrder.get(--orderIndex);
590 booleanList.add(false);
593 elementsBuilder.setType(HelloElementType.forValue(1));
594 elementsBuilder.setVersionBitmap(booleanList);
595 elementList.add(elementsBuilder.build());
598 helloBuilder.setElements(elementList);