Bug 1588 - OFConstants.java moved to openflowplugin-api module
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / HandshakeManagerImplTest.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
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;
38
39 import com.google.common.collect.Lists;
40
41 /**
42  * testing handshake
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 public class HandshakeManagerImplTest {
46     
47     private static final Logger LOG = LoggerFactory
48             .getLogger(HandshakeManagerImplTest.class);
49     
50     private HandshakeManagerImpl handshakeManager;
51     @Mock
52     private ConnectionAdapter adapter;
53     @Mock
54     private ErrorHandler errorHandler;
55     @Mock
56     private HandshakeListener handshakeListener;
57
58     private RpcResult<GetFeaturesOutput> resultFeatures;
59
60     private long helloXid = 42L;
61
62     private int expectedErrors = 0;
63     
64     /**
65      * invoked before every test method
66      */
67     @Before
68     public void setUp() {
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);
74         
75         resultFeatures = RpcResultsUtil.createRpcResult(true, new GetFeaturesOutputBuilder().build(), null);
76         
77         Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
78             .thenReturn(Futures.immediateFuture(RpcResultsUtil.createRpcResult(true, (Void) null, null)));
79     }
80     
81     /**
82      * invoked after each test method
83      */
84     @After
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);
92         }
93         
94         Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
95                 Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
96     }
97
98     /**
99      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeCommonBitmapVersion(java.util.List)}.
100      */
101     @Test
102     public void testProposeCommonBitmapVersion() {
103         Boolean[][] versions = new Boolean[][] {
104                 {true, true, true, false, false, false},
105                 {true, true, true, false, false}
106         };
107         
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);
115         }
116     }
117
118     /**
119      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl#proposeNextVersion(short)}.
120      */
121     @Test
122     public void testProposeNextVersion() {
123         short[] remoteVer = new short[] { 0x05, 0x04, 0x03, 0x02, 0x01, 0x8f,
124                 0xff };
125         short[] expectedProposal = new short[] { 0x04, 0x04, 0x01, 0x01, 0x01,
126                 0x04, 0x04 };
127
128         for (int i = 0; i < remoteVer.length; i++) {
129             short actualProposal = handshakeManager
130                     .proposeNextVersion(remoteVer[i]);
131             Assert.assertEquals(
132                     String.format("proposing for version: %04x", remoteVer[i]),
133                     expectedProposal[i], actualProposal);
134         }
135
136         try {
137             handshakeManager.proposeNextVersion((short) 0);
138             Assert.fail("there should be no proposition for this version");
139         } catch (Exception e) {
140             // expected
141         }
142     }
143
144     //////// Version Negotiation Tests //////////////
145     
146     /**
147      * Test of version negotiation Where switch version = 1.0
148      *
149      * @throws Exception
150      */
151     @Test
152     public void testVersionNegotiation10() throws Exception {
153         LOG.debug("testVersionNegotiation10");
154         Short version = OFConstants.OFP_VERSION_1_0;
155         
156         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
157             .thenReturn(Futures.immediateFuture(resultFeatures));
158         
159         handshakeManager.shake();
160         
161         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
162         handshakeManager.shake();
163         
164         Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
165     }
166     
167     /**
168      * Test of version negotiation Where switch version = 1.0
169      *
170      * @throws Exception
171      */
172     @Test
173     public void testVersionNegotiation10SwitchStarts() throws Exception {
174         LOG.debug("testVersionNegotiation10-ss");
175         Short version = OFConstants.OFP_VERSION_1_0;
176         
177         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
178             .thenReturn(Futures.immediateFuture(resultFeatures));
179         
180         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
181         handshakeManager.shake();
182         
183         Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
184     }
185
186     /**
187      * Test of version negotiation Where switch version < 1.0
188      * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful 
189      * @throws Exception
190      */
191     @Test
192     public void testVersionNegotiation00() throws Exception {
193         LOG.debug("testVersionNegotiation00");
194         expectedErrors = 1;
195         Short version = (short) 0x00;
196         
197         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
198         handshakeManager.shake();
199         
200         Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
201                 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
202     }
203     
204     /**
205      * Test of version negotiation Where switch version < 1.0
206      * Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful 
207      * @throws Exception
208      */
209     @Test
210     public void testVersionNegotiation00SwitchStarts() throws Exception {
211         LOG.debug("testVersionNegotiation00-ss");
212         expectedErrors = 1;
213         Short version = (short) 0x00;
214         
215         handshakeManager.shake();
216         
217         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
218         handshakeManager.shake();
219         
220         Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
221                 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
222     }
223
224     /**
225      * Test of version negotiation Where 1.0 < switch version < 1.3
226      *
227      * @throws Exception
228      */
229     @Test
230     public void testVersionNegotiation11() throws Exception {
231         LOG.debug("testVersionNegotiation11");
232         Short version = (short) 0x02;
233         Short expVersion = (short) 0x01;
234     
235         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
236             .thenReturn(Futures.immediateFuture(resultFeatures));
237         
238         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
239         handshakeManager.shake();
240         
241         handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
242         handshakeManager.shake();
243         
244         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
245                 resultFeatures.getResult(), expVersion);
246     }
247     
248     /**
249      * Test of version negotiation Where 1.0 < switch version < 1.3
250      *
251      * @throws Exception
252      */
253     @Test
254     public void testVersionNegotiation11SwitchStarts() throws Exception {
255         LOG.debug("testVersionNegotiation11-ss");
256         Short version = (short) 0x02;
257         Short expVersion = (short) 0x01;
258     
259         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
260             .thenReturn(Futures.immediateFuture(resultFeatures));
261         
262         handshakeManager.shake();
263         
264         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
265         handshakeManager.shake();
266         
267         handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
268         handshakeManager.shake();
269         
270         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
271                 resultFeatures.getResult(), expVersion);
272     }
273
274     /**
275      * Test of version negotiation Where switch version = 1.3
276      *
277      * @throws Exception
278      */
279     @Test
280     public void testVersionNegotiation13() throws Exception {
281         LOG.debug("testVersionNegotiation13");
282         Short version = OFConstants.OFP_VERSION_1_3;
283         
284         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
285             .thenReturn(Futures.immediateFuture(resultFeatures));
286         
287         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
288         handshakeManager.shake();
289         
290         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
291                 resultFeatures.getResult(), version);
292     }
293     
294     /**
295      * Test of version negotiation Where switch version = 1.3
296      *
297      * @throws Exception
298      */
299     @Test
300     public void testVersionNegotiation13SwitchStarts() throws Exception {
301         LOG.debug("testVersionNegotiation13-ss");
302         Short version = OFConstants.OFP_VERSION_1_3;
303         
304         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
305             .thenReturn(Futures.immediateFuture(resultFeatures));
306         
307         handshakeManager.shake();
308         
309         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
310         handshakeManager.shake();
311     
312         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
313                 resultFeatures.getResult(), version);
314     }
315
316     /**
317      * Test of version negotiation Where switch version >= 1.3
318      *
319      * @throws Exception
320      */
321     @Test
322     public void testVersionNegotiation15() throws Exception {
323         LOG.debug("testVersionNegotiation15");
324         Short version = (short) 0x06;
325         Short expVersion =  OFConstants.OFP_VERSION_1_3;
326         
327         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
328             .thenReturn(Futures.immediateFuture(resultFeatures));
329     
330         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
331         handshakeManager.shake();
332         
333         handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
334         handshakeManager.shake();
335         
336         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
337                 resultFeatures.getResult(), expVersion);
338     }
339     
340     /**
341      * Test of version negotiation Where switch version >= 1.3
342      *
343      * @throws Exception
344      */
345     @Test
346     public void testVersionNegotiation15SwitchStart() throws Exception {
347         LOG.debug("testVersionNegotiation15-ss");
348         Short version = (short) 0x06;
349         Short expVersion =  OFConstants.OFP_VERSION_1_3;
350         
351         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
352             .thenReturn(Futures.immediateFuture(resultFeatures));
353     
354         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
355         handshakeManager.shake();
356         
357         handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
358         handshakeManager.shake();
359         
360         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
361                 resultFeatures.getResult(), expVersion);
362     }
363
364     /**
365      * Test of version negotiation Where switch version > 1.3
366      *
367      * @throws Exception
368      */
369     @Test
370     public void testVersionNegotiation15_MultipleCall() throws Exception {
371         LOG.debug("testVersionNegotiation15_MultipleCall");
372         Short version = (short) 0x06;
373         expectedErrors = 1;
374     
375         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
376         handshakeManager.shake();
377         
378         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
379         handshakeManager.shake();
380         
381         Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
382                 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
383     }
384     
385     /**
386      * Test of version negotiation Where switch version > 1.3
387      *
388      * @throws Exception
389      */
390     @Test
391     public void testVersionNegotiation15_MultipleCallSwitchStarts() throws Exception {
392         LOG.debug("testVersionNegotiation15_MultipleCall-ss");
393         Short version = (short) 0x06;
394         expectedErrors = 1;
395     
396         handshakeManager.shake();
397         
398         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
399         handshakeManager.shake();
400         
401         handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
402         handshakeManager.shake();
403         
404         Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
405                 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
406     }
407
408     /**
409      * Test of version negotiation Where bitmap version {0x05,0x01}
410      *
411      * @throws Exception
412      */
413     @Test
414     public void testVersionNegotiation10InBitmap() throws Exception {
415         LOG.debug("testVersionNegotiation10InBitmap");
416         Short version = OFConstants.OFP_VERSION_1_0;
417         handshakeManager.setUseVersionBitmap(true);
418
419         HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
420         addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
421     
422         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
423             .thenReturn(Futures.immediateFuture(resultFeatures));
424         
425         handshakeManager.setReceivedHello(helloMessage.build());
426         handshakeManager.shake();
427         
428         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
429                 resultFeatures.getResult(), version);
430     }
431     
432     /**
433      * Test of version negotiation Where bitmap version {0x05,0x01}
434      *
435      * @throws Exception
436      */
437     @Test
438     public void testVersionNegotiation10InBitmapSwitchStarts() throws Exception {
439         LOG.debug("testVersionNegotiation10InBitmap-ss");
440         Short version = OFConstants.OFP_VERSION_1_0;
441         handshakeManager.setUseVersionBitmap(true);
442
443         HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
444         addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
445     
446         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
447             .thenReturn(Futures.immediateFuture(resultFeatures));
448         
449         handshakeManager.shake();
450         
451         handshakeManager.setReceivedHello(helloMessage.build());
452         handshakeManager.shake();
453         
454         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
455                 resultFeatures.getResult(), version);
456     }
457
458     /**
459      * Test of version negotiation Where bitmap version {0x05,0x04}
460      *
461      * @throws Exception
462      */
463     @Test
464     public void testVersionNegotiation13InBitmap() throws Exception {
465         LOG.debug("testVersionNegotiation13InBitmap");
466         Short version = OFConstants.OFP_VERSION_1_3;
467         handshakeManager.setUseVersionBitmap(true);
468
469         HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
470         addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
471     
472         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
473             .thenReturn(Futures.immediateFuture(resultFeatures));
474     
475         handshakeManager.setReceivedHello(helloMessage.build());
476         handshakeManager.shake();
477     
478         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
479                 resultFeatures.getResult(), version);
480     }
481     
482     /**
483      * Test of version negotiation Where bitmap version {0x05,0x04}
484      *
485      * @throws Exception
486      */
487     @Test
488     public void testVersionNegotiation13InBitmapSwitchFirst() throws Exception {
489         LOG.debug("testVersionNegotiation13InBitmap-ss");
490         Short version = OFConstants.OFP_VERSION_1_3;
491         handshakeManager.setUseVersionBitmap(true);
492
493         HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
494         addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
495     
496         Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
497             .thenReturn(Futures.immediateFuture(resultFeatures));
498     
499         handshakeManager.shake();
500         
501         handshakeManager.setReceivedHello(helloMessage.build());
502         handshakeManager.shake();
503     
504         Mockito.verify(handshakeListener).onHandshakeSuccessfull(
505                 resultFeatures.getResult(), version);
506     }
507
508     /**
509      * Test of version negotiation Where bitmap version {0x05,0x02}
510      *
511      * @throws Exception
512      */
513     @Test
514     public void testVersionNegotiationNoCommonVersionInBitmap() throws Exception {
515         LOG.debug("testVersionNegotiationNoCommonVersionInBitmap");
516         Short version = (short) 0x05;
517         expectedErrors = 1;
518         handshakeManager.setUseVersionBitmap(true);
519         
520         HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
521         addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
522         
523         handshakeManager.setReceivedHello(helloMessage.build());
524         handshakeManager.shake();
525         
526         Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
527                 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
528     }
529     
530     /**
531      * Test of version negotiation Where bitmap version {0x05,0x02}
532      *
533      * @throws Exception
534      */
535     @Test
536     public void testVersionNegotiationNoCommonVersionInBitmapSwitchStarts() throws Exception {
537         LOG.debug("testVersionNegotiationNoCommonVersionInBitmap-ss");
538         Short version = (short) 0x05;
539         expectedErrors = 1;
540         handshakeManager.setUseVersionBitmap(true);
541         
542         HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
543         addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
544         
545         handshakeManager.shake();
546         
547         handshakeManager.setReceivedHello(helloMessage.build());
548         handshakeManager.shake();
549         
550         Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
551                 Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
552     }
553
554
555     /**
556      * @param ofpVersion10
557      * @param helloXid
558      * @return
559      */
560     private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
561         return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
562     }
563     
564     /**
565      * @param versionOrder
566      * @param helloBuilder
567      * @return
568      */
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();
574
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);
584                 } else {
585                     booleanList.add(false);
586                 }
587             }
588             elementsBuilder.setType(HelloElementType.forValue(1));
589             elementsBuilder.setVersionBitmap(booleanList);
590             elementList.add(elementsBuilder.build());
591         }
592
593         helloBuilder.setElements(elementList);
594         return helloBuilder;
595     }
596
597 }