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 com.google.common.collect.Lists;
11 import com.google.common.util.concurrent.Futures;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.ErrorHandler;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
29 import org.opendaylight.openflowplugin.api.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.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
46 @RunWith(MockitoJUnitRunner.class)
47 public class HandshakeManagerImplTest {
49 private static final Logger LOG = LoggerFactory
50 .getLogger(HandshakeManagerImplTest.class);
52 private HandshakeManagerImpl handshakeManager;
54 private ConnectionAdapter adapter;
56 private ErrorHandler errorHandler;
58 private HandshakeListener handshakeListener;
60 private RpcResult<GetFeaturesOutput> resultFeatures;
62 private long helloXid = 42L;
64 private int expectedErrors = 0;
67 * invoked before every test method
71 handshakeManager = new HandshakeManagerImpl(adapter, OFConstants.OFP_VERSION_1_3,
72 ConnectionConductor.versionOrder);
73 handshakeManager.setErrorHandler(errorHandler);
74 handshakeManager.setHandshakeListener(handshakeListener);
75 handshakeManager.setUseVersionBitmap(false);
77 resultFeatures = RpcResultBuilder.success(new GetFeaturesOutputBuilder().build()).build();
79 Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
80 .thenReturn(Futures.immediateFuture(
81 RpcResultBuilder.success((Void) null).build()));
85 * invoked after each test method
88 public void teardown() {
89 // logging errors if occurred
90 ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
91 Mockito.verify(errorHandler, Mockito.atMost(1)).handleException(
92 errorCaptor.capture(), Matchers.any(SessionContext.class));
93 for (Throwable problem : errorCaptor.getAllValues()) {
94 LOG.warn(problem.getMessage(), problem);
97 Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
98 Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
102 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeCommonBitmapVersion(java.util.List)}.
105 public void testProposeCommonBitmapVersion() {
106 Boolean[][] versions = new Boolean[][] {
107 {true, true, true, false, false, false},
108 {true, true, true, false, false}
111 for (Boolean[] verasionList : versions) {
112 ElementsBuilder elementsBuilder = new ElementsBuilder();
113 elementsBuilder.setVersionBitmap(Lists.newArrayList(verasionList));
114 Elements element = elementsBuilder.build();
115 List<Elements> elements = Lists.newArrayList(element );
116 Short proposal = handshakeManager.proposeCommonBitmapVersion(elements);
117 Assert.assertEquals(Short.valueOf((short)1), proposal);
122 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeNextVersion(short)}.
125 public void testProposeNextVersion() {
126 short[] remoteVer = new short[] { 0x05, 0x04, 0x03, 0x02, 0x01, 0x8f,
128 short[] expectedProposal = new short[] { 0x04, 0x04, 0x01, 0x01, 0x01,
131 for (int i = 0; i < remoteVer.length; i++) {
132 short actualProposal = handshakeManager
133 .proposeNextVersion(remoteVer[i]);
135 String.format("proposing for version: %04x", remoteVer[i]),
136 expectedProposal[i], actualProposal);
140 handshakeManager.proposeNextVersion((short) 0);
141 Assert.fail("there should be no proposition for this version");
142 } catch (Exception e) {
147 //////// Version Negotiation Tests //////////////
150 * Test of version negotiation Where switch version = 1.0
155 public void testVersionNegotiation10() throws Exception {
156 LOG.debug("testVersionNegotiation10");
157 Short version = OFConstants.OFP_VERSION_1_0;
159 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
160 .thenReturn(Futures.immediateFuture(resultFeatures));
162 handshakeManager.shake(null);
164 handshakeManager.shake(createHelloMessage(version, helloXid).build());
166 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
170 * Test of version negotiation Where switch version = 1.0
175 public void testVersionNegotiation10SwitchStarts() throws Exception {
176 LOG.debug("testVersionNegotiation10-ss");
177 Short version = OFConstants.OFP_VERSION_1_0;
179 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
180 .thenReturn(Futures.immediateFuture(resultFeatures));
182 handshakeManager.shake(createHelloMessage(version, helloXid).build());
184 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
188 * Test of version negotiation Where switch version < 1.0
189 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
193 public void testVersionNegotiation00() throws Exception {
194 LOG.debug("testVersionNegotiation00");
196 Short version = (short) 0x00;
198 handshakeManager.shake(createHelloMessage(version, helloXid).build());
200 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessful(
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(null);
217 handshakeManager.shake(createHelloMessage(version, helloXid).build());
219 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessful(
220 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
224 * Test of version negotiation Where 1.0 < switch version < 1.3
229 public void testVersionNegotiation11() throws Exception {
230 LOG.debug("testVersionNegotiation11");
231 Short version = (short) 0x02;
232 Short expVersion = (short) 0x01;
234 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
235 .thenReturn(Futures.immediateFuture(resultFeatures));
237 handshakeManager.shake(createHelloMessage(version, helloXid).build());
239 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
241 Mockito.verify(handshakeListener).onHandshakeSuccessful(
242 resultFeatures.getResult(), expVersion);
246 * Test of version negotiation Where 1.0 < switch version < 1.3
251 public void testVersionNegotiation11SwitchStarts() throws Exception {
252 LOG.debug("testVersionNegotiation11-ss");
253 Short version = (short) 0x02;
254 Short expVersion = (short) 0x01;
256 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
257 .thenReturn(Futures.immediateFuture(resultFeatures));
259 handshakeManager.shake(null);
261 handshakeManager.shake(createHelloMessage(version, helloXid).build());
263 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
265 Mockito.verify(handshakeListener).onHandshakeSuccessful(
266 resultFeatures.getResult(), expVersion);
270 * Test of version negotiation Where switch version = 1.3
275 public void testVersionNegotiation13() throws Exception {
276 LOG.debug("testVersionNegotiation13");
277 Short version = OFConstants.OFP_VERSION_1_3;
279 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
280 .thenReturn(Futures.immediateFuture(resultFeatures));
282 handshakeManager.shake(createHelloMessage(version, helloXid).build());
284 Mockito.verify(handshakeListener).onHandshakeSuccessful(
285 resultFeatures.getResult(), version);
289 * Test of version negotiation Where switch version = 1.3
294 public void testVersionNegotiation13SwitchStarts() throws Exception {
295 LOG.debug("testVersionNegotiation13-ss");
296 Short version = OFConstants.OFP_VERSION_1_3;
298 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
299 .thenReturn(Futures.immediateFuture(resultFeatures));
301 handshakeManager.shake(null);
303 handshakeManager.shake(createHelloMessage(version, helloXid).build());
305 Mockito.verify(handshakeListener).onHandshakeSuccessful(
306 resultFeatures.getResult(), version);
310 * Test of version negotiation Where switch version >= 1.3
315 public void testVersionNegotiation15() throws Exception {
316 LOG.debug("testVersionNegotiation15");
317 Short version = (short) 0x06;
318 Short expVersion = OFConstants.OFP_VERSION_1_3;
320 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
321 .thenReturn(Futures.immediateFuture(resultFeatures));
323 handshakeManager.shake(createHelloMessage(version, helloXid).build());
325 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
327 Mockito.verify(handshakeListener).onHandshakeSuccessful(
328 resultFeatures.getResult(), expVersion);
332 * Test of version negotiation Where switch version >= 1.3
337 public void testVersionNegotiation15SwitchStart() throws Exception {
338 LOG.debug("testVersionNegotiation15-ss");
339 Short version = (short) 0x06;
340 Short expVersion = OFConstants.OFP_VERSION_1_3;
342 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
343 .thenReturn(Futures.immediateFuture(resultFeatures));
345 handshakeManager.shake(createHelloMessage(version, helloXid).build());
347 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
349 Mockito.verify(handshakeListener).onHandshakeSuccessful(
350 resultFeatures.getResult(), expVersion);
354 * Test of version negotiation Where switch version > 1.3
359 public void testVersionNegotiation15_MultipleCall() throws Exception {
360 LOG.debug("testVersionNegotiation15_MultipleCall");
361 Short version = (short) 0x06;
364 handshakeManager.shake(createHelloMessage(version, helloXid).build());
366 handshakeManager.shake(createHelloMessage(version, helloXid).build());
368 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessful(
369 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
373 * Test of version negotiation Where switch version > 1.3
378 public void testVersionNegotiation15_MultipleCallSwitchStarts() throws Exception {
379 LOG.debug("testVersionNegotiation15_MultipleCall-ss");
380 Short version = (short) 0x06;
383 handshakeManager.shake(null);
385 handshakeManager.shake(createHelloMessage(version, helloXid).build());
387 handshakeManager.shake(createHelloMessage(version, helloXid).build());
389 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessful(
390 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
394 * Test of version negotiation Where bitmap version {0x05,0x01}
399 public void testVersionNegotiation10InBitmap() throws Exception {
400 LOG.debug("testVersionNegotiation10InBitmap");
401 Short version = OFConstants.OFP_VERSION_1_0;
402 handshakeManager.setUseVersionBitmap(true);
404 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
405 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
407 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
408 .thenReturn(Futures.immediateFuture(resultFeatures));
410 handshakeManager.shake(helloMessage.build());
412 Mockito.verify(handshakeListener).onHandshakeSuccessful(
413 resultFeatures.getResult(), version);
417 * Test of version negotiation Where bitmap version {0x05,0x01}
422 public void testVersionNegotiation10InBitmapSwitchStarts() throws Exception {
423 LOG.debug("testVersionNegotiation10InBitmap-ss");
424 Short version = OFConstants.OFP_VERSION_1_0;
425 handshakeManager.setUseVersionBitmap(true);
427 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
428 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
430 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
431 .thenReturn(Futures.immediateFuture(resultFeatures));
433 handshakeManager.shake(null);
435 handshakeManager.shake(helloMessage.build());
437 Mockito.verify(handshakeListener).onHandshakeSuccessful(
438 resultFeatures.getResult(), version);
442 * Test of version negotiation Where bitmap version {0x05,0x04}
447 public void testVersionNegotiation13InBitmap() throws Exception {
448 LOG.debug("testVersionNegotiation13InBitmap");
449 Short version = OFConstants.OFP_VERSION_1_3;
450 handshakeManager.setUseVersionBitmap(true);
452 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
453 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
455 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
456 .thenReturn(Futures.immediateFuture(resultFeatures));
458 handshakeManager.shake(helloMessage.build());
460 Mockito.verify(handshakeListener).onHandshakeSuccessful(
461 resultFeatures.getResult(), version);
465 * Test of version negotiation Where bitmap version {0x05,0x04}
470 public void testVersionNegotiation13InBitmapSwitchFirst() throws Exception {
471 LOG.debug("testVersionNegotiation13InBitmap-ss");
472 Short version = OFConstants.OFP_VERSION_1_3;
473 handshakeManager.setUseVersionBitmap(true);
475 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
476 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
478 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
479 .thenReturn(Futures.immediateFuture(resultFeatures));
481 handshakeManager.shake(null);
483 handshakeManager.shake(helloMessage.build());
485 Mockito.verify(handshakeListener).onHandshakeSuccessful(
486 resultFeatures.getResult(), version);
490 * Test of version negotiation Where bitmap version {0x05,0x02}
495 public void testVersionNegotiationNoCommonVersionInBitmap() throws Exception {
496 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap");
497 Short version = (short) 0x05;
499 handshakeManager.setUseVersionBitmap(true);
501 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
502 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
504 handshakeManager.shake(helloMessage.build());
506 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessful(
507 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
511 * Test of version negotiation Where bitmap version {0x05,0x02}
516 public void testVersionNegotiationNoCommonVersionInBitmapSwitchStarts() throws Exception {
517 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap-ss");
518 Short version = (short) 0x05;
520 handshakeManager.setUseVersionBitmap(true);
522 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
523 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
525 handshakeManager.shake(null);
527 handshakeManager.shake(helloMessage.build());
529 Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessful(
530 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
535 * @param ofpVersion10
539 private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
540 return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
544 * @param versionOrder
545 * @param helloBuilder
548 private static HelloMessageBuilder addVersionBitmap(List<Short> versionOrder,
549 HelloMessageBuilder helloBuilder) {
550 short highestVersion = versionOrder.get(0);
551 int elementsCount = highestVersion / Integer.SIZE;
552 ElementsBuilder elementsBuilder = new ElementsBuilder();
554 List<Elements> elementList = new ArrayList<>();
555 int orderIndex = versionOrder.size();
556 int value = versionOrder.get(--orderIndex);
557 for (int index = 0; index <= elementsCount; index++) {
558 List<Boolean> booleanList = new ArrayList<>();
559 for (int i = 0; i < Integer.SIZE; i++) {
560 if (value == ((index * Integer.SIZE) + i)) {
561 booleanList.add(true);
562 value = (orderIndex == 0) ? highestVersion : versionOrder.get(--orderIndex);
564 booleanList.add(false);
567 elementsBuilder.setType(HelloElementType.forValue(1));
568 elementsBuilder.setVersionBitmap(booleanList);
569 elementList.add(elementsBuilder.build());
572 helloBuilder.setElements(elementList);