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.impl.connection;
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.ErrorHandler;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
44 @RunWith(MockitoJUnitRunner.class)
45 public class HandshakeManagerImplTest {
47 private static final Logger LOG = LoggerFactory.getLogger(HandshakeManagerImplTest.class);
49 private HandshakeManagerImpl handshakeManager;
51 private ConnectionAdapter adapter;
53 private ErrorHandler errorHandler;
55 private HandshakeListener handshakeListener;
57 private RpcResult<GetFeaturesOutput> resultFeatures;
59 private long helloXid = 42L;
61 private int expectedErrors = 0;
64 * invoked before every test method.
68 handshakeManager = new HandshakeManagerImpl(adapter, OFConstants.OFP_VERSION_1_3, OFConstants.VERSION_ORDER);
69 handshakeManager.setErrorHandler(errorHandler);
70 handshakeManager.setHandshakeListener(handshakeListener);
71 handshakeManager.setUseVersionBitmap(false);
73 resultFeatures = RpcResultBuilder.success(new GetFeaturesOutputBuilder().build()).build();
75 Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
76 .thenReturn(Futures.immediateFuture(RpcResultBuilder.success((Void) null).build()));
80 * invoked after each test method.
83 public void teardown() {
84 // logging errors if occurred
85 ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
86 Mockito.verify(errorHandler, Mockito.atMost(1)).handleException(errorCaptor.capture());
87 for (Throwable problem : errorCaptor.getAllValues()) {
88 LOG.warn(problem.getMessage(), problem);
91 Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(Matchers.any(Throwable.class));
96 * {@link org.opendaylight.openflowplugin.openflow.md.core
97 * .HandshakeManagerImpl#proposeCommonBitmapVersion(java.util.List)}.
100 public void testProposeCommonBitmapVersion() {
102 = new Boolean[][]{{true, true, true, false, false, false}, {true, true, true, false, false}};
104 for (Boolean[] verasionList : versions) {
105 ElementsBuilder elementsBuilder = new ElementsBuilder();
106 elementsBuilder.setVersionBitmap(Lists.newArrayList(verasionList));
107 Elements element = elementsBuilder.build();
108 List<Elements> elements = Lists.newArrayList(element);
109 Short proposal = handshakeManager.proposeCommonBitmapVersion(elements);
110 Assert.assertEquals(Short.valueOf((short) 1), proposal);
116 * {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeNextVersion(short)}.
119 @SuppressWarnings("checkstyle:Illegalcatch")
120 public void testProposeNextVersion() {
121 short[] remoteVer = new short[]{0x05, 0x04, 0x03, 0x02, 0x01, 0x8f, 0xff};
122 short[] expectedProposal = new short[]{0x04, 0x04, 0x01, 0x01, 0x01, 0x04, 0x04};
124 for (int i = 0; i < remoteVer.length; i++) {
125 short actualProposal = handshakeManager.proposeNextVersion(remoteVer[i]);
126 Assert.assertEquals(String.format("proposing for version: %04x", remoteVer[i]), expectedProposal[i],
131 handshakeManager.proposeNextVersion((short) 0);
132 Assert.fail("there should be no proposition for this version");
133 } catch (Exception e) {
138 //////// Version Negotiation Tests //////////////
141 * Test of version negotiation Where switch version = 1.0.
145 public void testVersionNegotiation10() throws Exception {
146 LOG.debug("testVersionNegotiation10");
147 Short version = OFConstants.OFP_VERSION_1_0;
149 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
150 .thenReturn(Futures.immediateFuture(resultFeatures));
152 handshakeManager.shake(null);
154 handshakeManager.shake(createHelloMessage(version, helloXid).build());
156 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
160 * Test of version negotiation Where switch version = 1.0.
164 public void testVersionNegotiation10SwitchStarts() throws Exception {
165 LOG.debug("testVersionNegotiation10-ss");
166 Short version = OFConstants.OFP_VERSION_1_0;
168 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
169 .thenReturn(Futures.immediateFuture(resultFeatures));
171 handshakeManager.shake(createHelloMessage(version, helloXid).build());
173 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
177 * Test of version negotiation Where switch version < 1.0.
178 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
181 public void testVersionNegotiation00() throws Exception {
182 LOG.debug("testVersionNegotiation00");
184 Short version = (short) 0x00;
186 handshakeManager.shake(createHelloMessage(version, helloXid).build());
188 Mockito.verify(handshakeListener, Mockito.never())
189 .onHandshakeSuccessful(Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
193 * Test of version negotiation Where switch version < 1.0.
194 * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
197 public void testVersionNegotiation00SwitchStarts() throws Exception {
198 LOG.debug("testVersionNegotiation00-ss");
200 Short version = (short) 0x00;
202 handshakeManager.shake(null);
204 handshakeManager.shake(createHelloMessage(version, helloXid).build());
206 Mockito.verify(handshakeListener, Mockito.never())
207 .onHandshakeSuccessful(Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
211 * Test of version negotiation Where 1.0 < switch version < 1.3.
215 public void testVersionNegotiation11() throws Exception {
216 LOG.debug("testVersionNegotiation11");
217 Short version = (short) 0x02;
218 Short expVersion = (short) 0x01;
220 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
221 .thenReturn(Futures.immediateFuture(resultFeatures));
223 handshakeManager.shake(createHelloMessage(version, helloXid).build());
225 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
227 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), expVersion);
231 * Test of version negotiation Where 1.0 < switch version < 1.3.
234 public void testVersionNegotiation11SwitchStarts() throws Exception {
235 LOG.debug("testVersionNegotiation11-ss");
236 final Short version = (short) 0x02;
237 final Short expVersion = (short) 0x01;
239 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
240 .thenReturn(Futures.immediateFuture(resultFeatures));
242 handshakeManager.shake(null);
244 handshakeManager.shake(createHelloMessage(version, helloXid).build());
246 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
248 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), expVersion);
252 * Test of version negotiation Where switch version = 1.3.
256 public void testVersionNegotiation13() throws Exception {
257 LOG.debug("testVersionNegotiation13");
258 Short version = OFConstants.OFP_VERSION_1_3;
260 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
261 .thenReturn(Futures.immediateFuture(resultFeatures));
263 handshakeManager.shake(createHelloMessage(version, helloXid).build());
265 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
269 * Test of version negotiation Where switch version = 1.3.
273 public void testVersionNegotiation13SwitchStarts() throws Exception {
274 LOG.debug("testVersionNegotiation13-ss");
275 Short version = OFConstants.OFP_VERSION_1_3;
277 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
278 .thenReturn(Futures.immediateFuture(resultFeatures));
280 handshakeManager.shake(null);
282 handshakeManager.shake(createHelloMessage(version, helloXid).build());
284 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
288 * Test of version negotiation Where switch version >= 1.3.
292 public void testVersionNegotiation15() throws Exception {
293 LOG.debug("testVersionNegotiation15");
294 Short version = (short) 0x06;
295 Short expVersion = OFConstants.OFP_VERSION_1_3;
297 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
298 .thenReturn(Futures.immediateFuture(resultFeatures));
300 handshakeManager.shake(createHelloMessage(version, helloXid).build());
302 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
304 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), expVersion);
308 * Test of version negotiation Where switch version >= 1.3.
312 public void testVersionNegotiation15SwitchStart() throws Exception {
313 LOG.debug("testVersionNegotiation15-ss");
314 Short version = (short) 0x06;
315 Short expVersion = OFConstants.OFP_VERSION_1_3;
317 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
318 .thenReturn(Futures.immediateFuture(resultFeatures));
320 handshakeManager.shake(createHelloMessage(version, helloXid).build());
322 handshakeManager.shake(createHelloMessage(expVersion, helloXid).build());
324 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), expVersion);
328 * Test of version negotiation Where switch version > 1.3.
332 public void testVersionNegotiation15_MultipleCall() throws Exception {
333 LOG.debug("testVersionNegotiation15_MultipleCall");
334 Short version = (short) 0x06;
337 handshakeManager.shake(createHelloMessage(version, helloXid).build());
339 handshakeManager.shake(createHelloMessage(version, helloXid).build());
341 Mockito.verify(handshakeListener, Mockito.never())
342 .onHandshakeSuccessful(Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
346 * Test of version negotiation Where switch version > 1.3.
350 public void testVersionNegotiation15_MultipleCallSwitchStarts() throws Exception {
351 LOG.debug("testVersionNegotiation15_MultipleCall-ss");
352 Short version = (short) 0x06;
355 handshakeManager.shake(null);
357 handshakeManager.shake(createHelloMessage(version, helloXid).build());
359 handshakeManager.shake(createHelloMessage(version, helloXid).build());
361 Mockito.verify(handshakeListener, Mockito.never())
362 .onHandshakeSuccessful(Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
366 * Test of version negotiation Where bitmap version {0x05,0x01}.
370 public void testVersionNegotiation10InBitmap() throws Exception {
371 LOG.debug("testVersionNegotiation10InBitmap");
372 Short version = OFConstants.OFP_VERSION_1_0;
373 handshakeManager.setUseVersionBitmap(true);
375 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
376 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
378 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
379 .thenReturn(Futures.immediateFuture(resultFeatures));
381 handshakeManager.shake(helloMessage.build());
383 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
387 * Test of version negotiation Where bitmap version {0x05,0x01}.
391 public void testVersionNegotiation10InBitmapSwitchStarts() throws Exception {
392 LOG.debug("testVersionNegotiation10InBitmap-ss");
393 Short version = OFConstants.OFP_VERSION_1_0;
394 handshakeManager.setUseVersionBitmap(true);
396 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
397 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
399 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
400 .thenReturn(Futures.immediateFuture(resultFeatures));
402 handshakeManager.shake(null);
404 handshakeManager.shake(helloMessage.build());
406 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
410 * Test of version negotiation Where bitmap version {0x05,0x04}.
414 public void testVersionNegotiation13InBitmap() throws Exception {
415 LOG.debug("testVersionNegotiation13InBitmap");
416 Short version = OFConstants.OFP_VERSION_1_3;
417 handshakeManager.setUseVersionBitmap(true);
419 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
420 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
422 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
423 .thenReturn(Futures.immediateFuture(resultFeatures));
425 handshakeManager.shake(helloMessage.build());
427 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
431 * Test of version negotiation Where bitmap version {0x05,0x04}.
435 public void testVersionNegotiation13InBitmapSwitchFirst() throws Exception {
436 LOG.debug("testVersionNegotiation13InBitmap-ss");
437 Short version = OFConstants.OFP_VERSION_1_3;
438 handshakeManager.setUseVersionBitmap(true);
440 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
441 addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
443 Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
444 .thenReturn(Futures.immediateFuture(resultFeatures));
446 handshakeManager.shake(null);
448 handshakeManager.shake(helloMessage.build());
450 Mockito.verify(handshakeListener).onHandshakeSuccessful(resultFeatures.getResult(), version);
454 * Test of version negotiation Where bitmap version {0x05,0x02}.
458 public void testVersionNegotiationNoCommonVersionInBitmap() throws Exception {
459 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap");
460 Short version = (short) 0x05;
462 handshakeManager.setUseVersionBitmap(true);
464 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
465 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
467 handshakeManager.shake(helloMessage.build());
469 Mockito.verify(handshakeListener, Mockito.never())
470 .onHandshakeSuccessful(Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
474 * Test of version negotiation Where bitmap version {0x05,0x02}.
478 public void testVersionNegotiationNoCommonVersionInBitmapSwitchStarts() throws Exception {
479 LOG.debug("testVersionNegotiationNoCommonVersionInBitmap-ss");
480 Short version = (short) 0x05;
482 handshakeManager.setUseVersionBitmap(true);
484 HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
485 addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
487 handshakeManager.shake(null);
489 handshakeManager.shake(helloMessage.build());
491 Mockito.verify(handshakeListener, Mockito.never())
492 .onHandshakeSuccessful(Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
497 * Creates hello message.
499 * @param ofpVersion10 version
500 * @param helloXid hello xid
503 private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
504 return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
508 * Adds version bitmap.
509 * @param versionOrder version order
510 * @param helloBuilder hello builder
513 private static HelloMessageBuilder addVersionBitmap(List<Short> versionOrder, HelloMessageBuilder helloBuilder) {
514 short highestVersion = versionOrder.get(0);
515 int elementsCount = highestVersion / Integer.SIZE;
516 ElementsBuilder elementsBuilder = new ElementsBuilder();
518 List<Elements> elementList = new ArrayList<>();
519 int orderIndex = versionOrder.size();
520 int value = versionOrder.get(--orderIndex);
521 for (int index = 0; index <= elementsCount; index++) {
522 List<Boolean> booleanList = new ArrayList<>();
523 for (int i = 0; i < Integer.SIZE; i++) {
524 if (value == ((index * Integer.SIZE) + i)) {
525 booleanList.add(true);
526 value = (orderIndex == 0) ? highestVersion : versionOrder.get(--orderIndex);
528 booleanList.add(false);
531 elementsBuilder.setType(HelloElementType.forValue(1));
532 elementsBuilder.setVersionBitmap(booleanList);
533 elementList.add(elementsBuilder.build());
536 helloBuilder.setElements(elementList);