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.Collection;
12 import java.util.Collections;
13 import java.util.List;
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.sal.common.util.Futures;
26 import org.opendaylight.controller.sal.common.util.Rpcs;
27 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
28 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
29 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
38 import org.opendaylight.yangtools.yang.common.RpcError;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.collect.Lists;
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 = RpcResultsUtil.createRpcResult(true, new GetFeaturesOutputBuilder().build(), null);
81 Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
82 .thenReturn(Futures.immediateFuture(RpcResultsUtil.createRpcResult(true, (Void) null, null)));
86 * invoked after each test method
89 public void teardown() {
90 // logging errors if occurred
91 ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
92 Mockito.verify(errorHandler, Mockito.atMost(1)).handleException(
93 errorCaptor.capture(), Matchers.any(SessionContext.class));
94 for (Throwable problem : errorCaptor.getAllValues()) {
95 LOG.warn(problem.getMessage(), problem);
98 Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
99 Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
103 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeCommonBitmapVersion(java.util.List)}.
106 public void testProposeCommonBitmapVersion() {
107 Boolean[][] versions = new Boolean[][] {
108 {true, true, true, false, false, false},
109 {true, true, true, false, false}
112 for (Boolean[] verasionList : versions) {
113 ElementsBuilder elementsBuilder = new ElementsBuilder();
114 elementsBuilder.setVersionBitmap(Lists.newArrayList(verasionList));
115 Elements element = elementsBuilder.build();
116 List<Elements> elements = Lists.newArrayList(element );
117 Short proposal = handshakeManager.proposeCommonBitmapVersion(elements);
118 Assert.assertEquals(Short.valueOf((short)1), proposal);
123 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeNextVersion(short)}.
126 public void testProposeNextVersion() {
127 short[] remoteVer = new short[] { 0x05, 0x04, 0x03, 0x02, 0x01, 0x8f,
129 short[] expectedProposal = new short[] { 0x04, 0x04, 0x01, 0x01, 0x01,
132 for (int i = 0; i < remoteVer.length; i++) {
133 short actualProposal = handshakeManager
134 .proposeNextVersion(remoteVer[i]);
136 String.format("proposing for version: %04x", remoteVer[i]),
137 expectedProposal[i], actualProposal);
141 handshakeManager.proposeNextVersion((short) 0);
142 Assert.fail("there should be no proposition for this version");
143 } catch (Exception e) {
148 //////// Version Negotiation Tests //////////////
151 * Test of version negotiation Where switch version = 1.0
156 public void testVersionNegotiation10() throws Exception {
157 LOG.debug("testVersionNegotiation10");
158 Short version = OFConstants.OFP_VERSION_1_0;
160 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
161 .thenReturn(Futures.immediateFuture(resultFeatures));
163 handshakeManager.shake();
165 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
166 handshakeManager.shake();
168 Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
172 * Test of version negotiation Where switch version = 1.0
177 public void testVersionNegotiation10SwitchStarts() throws Exception {
178 LOG.debug("testVersionNegotiation10-ss");
179 Short version = OFConstants.OFP_VERSION_1_0;
181 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
182 .thenReturn(Futures.immediateFuture(resultFeatures));
184 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
185 handshakeManager.shake();
187 Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
191 * Test of version negotiation Where switch version < 1.0
192 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
196 public void testVersionNegotiation00() throws Exception {
197 LOG.debug("testVersionNegotiation00");
199 Short version = (short) 0x00;
201 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
202 handshakeManager.shake();
204 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
205 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
209 * Test of version negotiation Where switch version < 1.0
210 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
214 public void testVersionNegotiation00SwitchStarts() throws Exception {
215 LOG.debug("testVersionNegotiation00-ss");
217 Short version = (short) 0x00;
219 handshakeManager.shake();
221 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
222 handshakeManager.shake();
224 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
225 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
229 * Test of version negotiation Where 1.0 < switch version < 1.3
234 public void testVersionNegotiation11() throws Exception {
235 LOG.debug("testVersionNegotiation11");
236 Short version = (short) 0x02;
237 Short expVersion = (short) 0x01;
239 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
240 .thenReturn(Futures.immediateFuture(resultFeatures));
242 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
243 handshakeManager.shake();
245 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
246 handshakeManager.shake();
248 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
249 resultFeatures.getResult(), expVersion);
253 * Test of version negotiation Where 1.0 < switch version < 1.3
258 public void testVersionNegotiation11SwitchStarts() throws Exception {
259 LOG.debug("testVersionNegotiation11-ss");
260 Short version = (short) 0x02;
261 Short expVersion = (short) 0x01;
263 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
264 .thenReturn(Futures.immediateFuture(resultFeatures));
266 handshakeManager.shake();
268 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
269 handshakeManager.shake();
271 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
272 handshakeManager.shake();
274 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
275 resultFeatures.getResult(), expVersion);
279 * Test of version negotiation Where switch version = 1.3
284 public void testVersionNegotiation13() throws Exception {
285 LOG.debug("testVersionNegotiation13");
286 Short version = OFConstants.OFP_VERSION_1_3;
288 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
289 .thenReturn(Futures.immediateFuture(resultFeatures));
291 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
292 handshakeManager.shake();
294 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
295 resultFeatures.getResult(), version);
299 * Test of version negotiation Where switch version = 1.3
304 public void testVersionNegotiation13SwitchStarts() throws Exception {
305 LOG.debug("testVersionNegotiation13-ss");
306 Short version = OFConstants.OFP_VERSION_1_3;
308 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
309 .thenReturn(Futures.immediateFuture(resultFeatures));
311 handshakeManager.shake();
313 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
314 handshakeManager.shake();
316 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
317 resultFeatures.getResult(), version);
321 * Test of version negotiation Where switch version >= 1.3
326 public void testVersionNegotiation15() throws Exception {
327 LOG.debug("testVersionNegotiation15");
328 Short version = (short) 0x06;
329 Short expVersion = OFConstants.OFP_VERSION_1_3;
331 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
332 .thenReturn(Futures.immediateFuture(resultFeatures));
334 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
335 handshakeManager.shake();
337 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
338 handshakeManager.shake();
340 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
341 resultFeatures.getResult(), expVersion);
345 * Test of version negotiation Where switch version >= 1.3
350 public void testVersionNegotiation15SwitchStart() throws Exception {
351 LOG.debug("testVersionNegotiation15-ss");
352 Short version = (short) 0x06;
353 Short expVersion = OFConstants.OFP_VERSION_1_3;
355 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
356 .thenReturn(Futures.immediateFuture(resultFeatures));
358 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
359 handshakeManager.shake();
361 handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
362 handshakeManager.shake();
364 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
365 resultFeatures.getResult(), expVersion);
369 * Test of version negotiation Where switch version > 1.3
374 public void testVersionNegotiation15_MultipleCall() throws Exception {
375 LOG.debug("testVersionNegotiation15_MultipleCall");
376 Short version = (short) 0x06;
379 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
380 handshakeManager.shake();
382 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
383 handshakeManager.shake();
385 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
386 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
390 * Test of version negotiation Where switch version > 1.3
395 public void testVersionNegotiation15_MultipleCallSwitchStarts() throws Exception {
396 LOG.debug("testVersionNegotiation15_MultipleCall-ss");
397 Short version = (short) 0x06;
400 handshakeManager.shake();
402 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
403 handshakeManager.shake();
405 handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
406 handshakeManager.shake();
408 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
409 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
413 * Test of version negotiation Where bitmap version {0x05,0x01}
418 public void testVersionNegotiation10InBitmap() throws Exception {
419 LOG.debug("testVersionNegotiation10InBitmap");
420 Short version = OFConstants.OFP_VERSION_1_0;
421 handshakeManager.setUseVersionBitmap(true);
423 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
424 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
426 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
427 .thenReturn(Futures.immediateFuture(resultFeatures));
429 handshakeManager.setReceivedHello(helloMessage.build());
430 handshakeManager.shake();
432 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
433 resultFeatures.getResult(), version);
437 * Test of version negotiation Where bitmap version {0x05,0x01}
442 public void testVersionNegotiation10InBitmapSwitchStarts() throws Exception {
443 LOG.debug("testVersionNegotiation10InBitmap-ss");
444 Short version = OFConstants.OFP_VERSION_1_0;
445 handshakeManager.setUseVersionBitmap(true);
447 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
448 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
450 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
451 .thenReturn(Futures.immediateFuture(resultFeatures));
453 handshakeManager.shake();
455 handshakeManager.setReceivedHello(helloMessage.build());
456 handshakeManager.shake();
458 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
459 resultFeatures.getResult(), version);
463 * Test of version negotiation Where bitmap version {0x05,0x04}
468 public void testVersionNegotiation13InBitmap() throws Exception {
469 LOG.debug("testVersionNegotiation13InBitmap");
470 Short version = OFConstants.OFP_VERSION_1_3;
471 handshakeManager.setUseVersionBitmap(true);
473 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
474 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
476 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
477 .thenReturn(Futures.immediateFuture(resultFeatures));
479 handshakeManager.setReceivedHello(helloMessage.build());
480 handshakeManager.shake();
482 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
483 resultFeatures.getResult(), version);
487 * Test of version negotiation Where bitmap version {0x05,0x04}
492 public void testVersionNegotiation13InBitmapSwitchFirst() throws Exception {
493 LOG.debug("testVersionNegotiation13InBitmap-ss");
494 Short version = OFConstants.OFP_VERSION_1_3;
495 handshakeManager.setUseVersionBitmap(true);
497 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
498 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
500 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
501 .thenReturn(Futures.immediateFuture(resultFeatures));
503 handshakeManager.shake();
505 handshakeManager.setReceivedHello(helloMessage.build());
506 handshakeManager.shake();
508 Mockito.verify(handshakeListener).onHandshakeSuccessfull(
509 resultFeatures.getResult(), version);
513 * Test of version negotiation Where bitmap version {0x05,0x02}
518 public void testVersionNegotiationNoCommonVersionInBitmap() throws Exception {
519 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap");
520 Short version = (short) 0x05;
522 handshakeManager.setUseVersionBitmap(true);
524 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
525 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
527 handshakeManager.setReceivedHello(helloMessage.build());
528 handshakeManager.shake();
530 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
531 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
535 * Test of version negotiation Where bitmap version {0x05,0x02}
540 public void testVersionNegotiationNoCommonVersionInBitmapSwitchStarts() throws Exception {
541 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap-ss");
542 Short version = (short) 0x05;
544 handshakeManager.setUseVersionBitmap(true);
546 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
547 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
549 handshakeManager.shake();
551 handshakeManager.setReceivedHello(helloMessage.build());
552 handshakeManager.shake();
554 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
555 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
560 * @param ofpVersion10
564 private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
565 return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
569 * @param versionOrder
570 * @param helloBuilder
573 private static HelloMessageBuilder addVersionBitmap(List<Short> versionOrder,
574 HelloMessageBuilder helloBuilder) {
575 short highestVersion = versionOrder.get(0);
576 int elementsCount = highestVersion / Integer.SIZE;
577 ElementsBuilder elementsBuilder = new ElementsBuilder();
579 List<Elements> elementList = new ArrayList<>();
580 int orderIndex = versionOrder.size();
581 int value = versionOrder.get(--orderIndex);
582 for (int index = 0; index <= elementsCount; index++) {
583 List<Boolean> booleanList = new ArrayList<>();
584 for (int i = 0; i < Integer.SIZE; i++) {
585 if (value == ((index * Integer.SIZE) + i)) {
586 booleanList.add(true);
587 value = (orderIndex == 0) ? highestVersion : versionOrder.get(--orderIndex);
589 booleanList.add(false);
592 elementsBuilder.setType(HelloElementType.forValue(1));
593 elementsBuilder.setVersionBitmap(booleanList);
594 elementList.add(elementsBuilder.build());
597 helloBuilder.setElements(elementList);