Bug 1588 - Move API classes from openflowplugin module to openflowplugin-api module
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImplTest.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
9 package org.opendaylight.openflowplugin.openflow.md.core;
10
11 import static org.junit.Assert.assertNotNull;
12
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Stack;
20 import java.util.concurrent.ScheduledThreadPoolExecutor;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Matchers;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.runners.MockitoJUnitRunner;
31 import org.opendaylight.openflowplugin.api.OFConstants;
32 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
33 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
34 import org.opendaylight.openflowplugin.openflow.md.core.plan.ConnectionAdapterStackImpl;
35 import org.opendaylight.openflowplugin.openflow.md.core.plan.EventFactory;
36 import org.opendaylight.openflowplugin.openflow.md.core.plan.SwitchTestEvent;
37 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
38 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
39 import org.opendaylight.openflowplugin.openflow.md.queue.QueueProcessorLightImpl;
40 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessageBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessageBuilder;
58 import org.opendaylight.yangtools.yang.binding.DataContainer;
59 import org.opendaylight.yangtools.yang.binding.DataObject;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 @RunWith(MockitoJUnitRunner.class)
64 public class ConnectionConductorImplTest {
65
66     protected static final Logger LOG = LoggerFactory
67             .getLogger(ConnectionConductorImplTest.class);
68
69     /**
70      * in [ms]
71      */
72     private final int maxProcessingTimeout = 500;
73
74     protected ConnectionAdapterStackImpl adapter;
75     private ConnectionConductorImpl connectionConductor;
76     private MDController controller;
77     private Stack<SwitchTestEvent> eventPlan;
78
79     private Thread libSimulation;
80     private ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(
81             8);
82
83     protected QueueProcessorLightImpl queueProcessor;
84
85     private PopListener<DataObject> popListener;
86
87     private int experimenterMessageCounter;
88     private int packetinMessageCounter;
89     private int flowremovedMessageCounter;
90     private int portstatusAddMessageCounter;
91     private int portstatusDeleteMessageCounter;
92     private int portstatusModifyMessageCounter;
93     private int errorMessageCounter;
94
95     @Mock
96     private ErrorHandlerSimpleImpl errorHandler;
97
98     private int expectedErrors = 0;
99     @Mock
100     private MessageSpy<DataContainer> messageSpy;
101
102     public void incrExperimenterMessageCounter() {
103         this.experimenterMessageCounter++;
104     }
105
106     public void incrPacketinMessageCounter() {
107         this.packetinMessageCounter++;
108     }
109
110     public void incrFlowremovedMessageCounter() {
111         this.flowremovedMessageCounter++;
112     }
113
114     public void incrPortstatusAddMessageCounter() {
115         this.portstatusAddMessageCounter++;
116     }
117
118     public void incrPortstatusDeleteMessageCounter() {
119         this.portstatusDeleteMessageCounter++;
120     }
121
122     public void incrPortstatusModifyMessageCounter() {
123         this.portstatusModifyMessageCounter++;
124     }
125
126     public void incrErrorMessageCounter() {
127         this.errorMessageCounter++;
128     }
129
130     @Test
131     /**
132      * Test for ConnectionConductorFactory#createConductor
133      */
134     public void testCreateConductor() {
135         ConnectionConductor connectionConductor = ConnectionConductorFactory.createConductor(adapter, queueProcessor);
136         assertNotNull(connectionConductor);
137     }
138
139     /**
140      * @throws java.lang.Exception
141      */
142     @Before
143     public void setUp() throws Exception {
144         adapter = new ConnectionAdapterStackImpl();
145
146         popListener = new PopListenerCountingImpl<>();
147
148         controller = new MDController();
149         controller.init();
150         controller.getMessageTranslators().putAll(assembleTranslatorMapping());
151
152         queueProcessor = new QueueProcessorLightImpl();
153         queueProcessor.setMessageSpy(messageSpy);
154         queueProcessor.setPopListenersMapping(assemblePopListenerMapping());
155         queueProcessor.setTranslatorMapping(controller.getMessageTranslators());
156         queueProcessor.init();
157
158         connectionConductor = new ConnectionConductorImpl(adapter);
159         connectionConductor.setQueueProcessor(queueProcessor);
160         connectionConductor.setErrorHandler(errorHandler);
161         connectionConductor.init();
162         eventPlan = new Stack<>();
163         adapter.setEventPlan(eventPlan);
164         adapter.setProceedTimeout(5000L);
165         adapter.checkListeners();
166     }
167
168     /**
169      * @return
170      */
171     private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> assemblePopListenerMapping() {
172         Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> mapping = new HashMap<>();
173         Collection<PopListener<DataObject>> popListenerBag = new ArrayList<>();
174         popListenerBag.add(popListener);
175         //TODO: add testing registered types
176         mapping.put(DataObject.class, popListenerBag);
177         return mapping;
178     }
179
180     /**
181      * @throws java.lang.Exception
182      */
183     @After
184     public void tearDown() throws Exception {
185         if (libSimulation != null) {
186             libSimulation.join();
187         }
188         queueProcessor.shutdown();
189         connectionConductor.shutdownPool();
190
191         for (Exception problem : adapter.getOccuredExceptions()) {
192             LOG.error("during simulation on adapter side: "
193                     + problem.getMessage());
194         }
195         Assert.assertEquals(0, adapter.getOccuredExceptions().size());
196         adapter = null;
197         if (LOG.isDebugEnabled()) {
198             if (eventPlan.size() > 0) {
199                 LOG.debug("eventPlan size: " + eventPlan.size());
200                 for (SwitchTestEvent event : eventPlan) {
201                     LOG.debug(" # EVENT:: " + event.toString());
202                 }
203             }
204         }
205         Assert.assertTrue("plan is not finished", eventPlan.isEmpty());
206         eventPlan = null;
207         controller = null;
208
209         // logging errors if occurred
210         ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
211         Mockito.verify(errorHandler, Mockito.atMost(1)).handleException(
212                 errorCaptor.capture(), Matchers.any(SessionContext.class));
213         for (Throwable problem : errorCaptor.getAllValues()) {
214             LOG.warn(problem.getMessage(), problem);
215         }
216
217         Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
218                 Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
219     }
220
221     /**
222      * Test method for
223      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onEchoRequestMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage)}
224      * .
225      *
226      * @throws Exception
227      */
228     @Test
229     public void testOnEchoRequestMessage() throws Exception {
230         simulateV13PostHandshakeState(connectionConductor);
231
232         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
233                 EventFactory.DEFAULT_VERSION, new EchoRequestMessageBuilder()));
234         eventPlan.add(0,
235                 EventFactory.createDefaultWaitForRpcEvent(42, "echoReply"));
236         executeNow();
237     }
238
239     /**
240      * Test of handshake, covering version negotiation and features.
241      * Switch delivers first helloMessage with default version.
242      *
243      * @throws Exception
244      */
245     @Test
246     public void testHandshake1() throws Exception {
247         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
248                 EventFactory.DEFAULT_VERSION, new HelloMessageBuilder()));
249         eventPlan.add(0, EventFactory.createDefaultWaitForAllEvent(
250                 EventFactory.createDefaultWaitForRpcEvent(43, "helloReply"),
251                 EventFactory.createDefaultWaitForRpcEvent(44, "getFeatures")));
252         eventPlan.add(0, EventFactory.createDefaultRpcResponseEvent(44,
253                 EventFactory.DEFAULT_VERSION, getFeatureResponseMsg()));
254
255         int i = 1;
256         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "setConfig"));
257         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
258         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
259         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
260         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
261         executeNow();
262
263         Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.WORKING,
264                 connectionConductor.getConductorState());
265         Assert.assertEquals((short) 0x04, connectionConductor.getVersion()
266                 .shortValue());
267     }
268
269     /**
270      * Test of handshake, covering version negotiation and features.
271      * Controller sends first helloMessage with default version
272      *
273      * @throws Exception
274      */
275     @Test
276     public void testHandshake1SwitchStarts() throws Exception {
277         eventPlan.add(0, EventFactory.createConnectionReadyCallback(connectionConductor));
278         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(21, "helloReply"));
279         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
280                 EventFactory.DEFAULT_VERSION, new HelloMessageBuilder()));
281         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(43, "getFeatures"));
282         eventPlan.add(0, EventFactory.createDefaultRpcResponseEvent(43,
283                 EventFactory.DEFAULT_VERSION, getFeatureResponseMsg()));
284
285         int i = 1;
286         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "setConfig"));
287         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
288         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
289         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
290         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(i++, "multipartRequestInput"));
291
292         executeNow();
293
294         Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.WORKING,
295                 connectionConductor.getConductorState());
296         Assert.assertEquals((short) 0x04, connectionConductor.getVersion()
297                 .shortValue());
298     }
299
300     /**
301      * Test of handshake, covering version negotiation and features.
302      * Switch delivers first helloMessage with version 0x05
303      * and negotiates following versions: 0x03, 0x01
304      *
305      * @throws Exception
306      */
307     @Test
308     public void testHandshake2() throws Exception {
309         connectionConductor.setBitmapNegotiationEnable(false);
310         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
311                 (short) 0x05, new HelloMessageBuilder()));
312         eventPlan.add(0,
313                 EventFactory.createDefaultWaitForRpcEvent(43, "helloReply"));
314         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(43L,
315                 (short) 0x03, new HelloMessageBuilder()));
316         eventPlan.add(0,
317                 EventFactory.createDefaultWaitForRpcEvent(44, "helloReply"));
318         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(44L,
319                 (short) 0x01, new HelloMessageBuilder()));
320         eventPlan.add(0,
321                 EventFactory.createDefaultWaitForRpcEvent(45, "getFeatures"));
322
323         eventPlan.add(0, EventFactory.createDefaultRpcResponseEvent(45,
324                 EventFactory.DEFAULT_VERSION, getFeatureResponseMsg()));
325
326         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(1, "multipartRequestInput"));
327
328         executeNow();
329
330         Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.WORKING,
331                 connectionConductor.getConductorState());
332         Assert.assertEquals((short) 0x01, connectionConductor.getVersion()
333                 .shortValue());
334     }
335
336     /**
337      * Test of handshake, covering version negotiation and features.
338      * Controller sends first helloMessage with default version
339      * and switch negotiates following versions: 0x05, 0x03, 0x01
340      *
341      * @throws Exception
342      */
343     @Test
344     public void testHandshake2SwitchStarts() throws Exception {
345         connectionConductor.setBitmapNegotiationEnable(false);
346         eventPlan.add(0, EventFactory.createConnectionReadyCallback(connectionConductor));
347         eventPlan.add(0,
348                 EventFactory.createDefaultWaitForRpcEvent(21, "helloReply"));
349         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
350                 (short) 0x05, new HelloMessageBuilder()));
351         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(43L,
352                 (short) 0x03, new HelloMessageBuilder()));
353         eventPlan.add(0,
354                 EventFactory.createDefaultWaitForRpcEvent(44, "helloReply"));
355         eventPlan.add(0, EventFactory.createDefaultNotificationEvent(44L,
356                 (short) 0x01, new HelloMessageBuilder()));
357         eventPlan.add(0,
358                 EventFactory.createDefaultWaitForRpcEvent(45, "getFeatures"));
359
360         eventPlan.add(0, EventFactory.createDefaultRpcResponseEvent(45,
361                 EventFactory.DEFAULT_VERSION, getFeatureResponseMsg()));
362
363         eventPlan.add(0, EventFactory.createDefaultWaitForRpcEvent(1, "multipartRequestInput"));
364
365         executeNow();
366
367         Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.WORKING,
368                 connectionConductor.getConductorState());
369         Assert.assertEquals((short) 0x01, connectionConductor.getVersion()
370                 .shortValue());
371     }
372
373     /**
374      * Test method for
375      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onFlowRemovedMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage)}
376      * .
377      *
378      * @throws InterruptedException
379      */
380     @Test
381     public void testOnFlowRemovedMessage() throws InterruptedException {
382         IMDMessageTranslator<OfHeader, List<DataObject>> objFms = new FlowRemovedMessageService();
383         controller.addMessageTranslator(FlowRemovedMessage.class, 4, objFms);
384
385         simulateV13PostHandshakeState(connectionConductor);
386
387         // Now send Flow Removed messages
388         FlowRemovedMessageBuilder builder1 = new FlowRemovedMessageBuilder();
389         builder1.setVersion((short) 4);
390         builder1.setXid(1L);
391         connectionConductor.onFlowRemovedMessage(builder1.build());
392         synchronized (popListener) {
393             LOG.debug("about to wait for popListener");
394             popListener.wait(maxProcessingTimeout);
395         }
396         Assert.assertEquals(1, flowremovedMessageCounter);
397         builder1.setXid(2L);
398         connectionConductor.onFlowRemovedMessage(builder1.build());
399         synchronized (popListener) {
400             popListener.wait(maxProcessingTimeout);
401         }
402         Assert.assertEquals(2, flowremovedMessageCounter);
403     }
404
405     /**
406      * Test method for
407      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onMultipartReplyMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage)}
408      * .
409      */
410     @Test
411     public void testOnMultipartReplyMessage() {
412         // fail("Not yet implemented");
413         // TODO:: add test
414     }
415
416     /**
417      * Test method for
418      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onPacketInMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage)}
419      * .
420      *
421      * @throws InterruptedException
422      */
423     @Test
424     public void testOnPacketInMessage() throws InterruptedException {
425         IMDMessageTranslator<OfHeader, List<DataObject>> objPms = new PacketInMessageService();
426         controller.addMessageTranslator(PacketInMessage.class, 4, objPms);
427
428         simulateV13PostHandshakeState(connectionConductor);
429
430         // Now send PacketIn
431         PacketInMessageBuilder builder1 = new PacketInMessageBuilder();
432         builder1.setVersion((short) 4);
433         builder1.setBufferId((long) 1);
434         connectionConductor.onPacketInMessage(builder1.build());
435         synchronized (popListener) {
436             popListener.wait(maxProcessingTimeout);
437         }
438         Assert.assertEquals(1, packetinMessageCounter);
439         builder1.setBufferId((long) 2);
440         connectionConductor.onPacketInMessage(builder1.build());
441         synchronized (popListener) {
442             popListener.wait(maxProcessingTimeout);
443         }
444         Assert.assertEquals(2, packetinMessageCounter);
445     }
446
447     /**
448      * Test method for
449      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onPortStatusMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage)}
450      * .
451      *
452      * @throws InterruptedException
453      */
454     @Test
455     public void testOnPortStatusMessage() throws InterruptedException {
456         IMDMessageTranslator<OfHeader, List<DataObject>> objPSms = new PortStatusMessageService();
457         controller.addMessageTranslator(PortStatusMessage.class, 4, objPSms);
458
459         simulateV13PostHandshakeState(connectionConductor);
460
461         // Send Port Status messages
462         PortStatusMessageBuilder builder1 = new PortStatusMessageBuilder();
463         builder1.setVersion((short) 4);
464         PortFeatures features = new PortFeatures(true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);
465         builder1.setPortNo(90L).setReason(PortReason.OFPPRADD).setCurrentFeatures(features);
466         connectionConductor.onPortStatusMessage(builder1.build());
467         synchronized (popListener) {
468             popListener.wait(maxProcessingTimeout);
469         }
470         Assert.assertEquals(1, portstatusAddMessageCounter);
471         builder1.setPortNo(90L).setReason(PortReason.OFPPRMODIFY).setCurrentFeatures(features);
472         connectionConductor.onPortStatusMessage(builder1.build());
473         synchronized (popListener) {
474             popListener.wait(maxProcessingTimeout);
475         }
476         Assert.assertEquals(1, portstatusModifyMessageCounter);
477         builder1.setPortNo(90L).setReason(PortReason.OFPPRDELETE).setCurrentFeatures(features);
478         connectionConductor.onPortStatusMessage(builder1.build());
479         synchronized (popListener) {
480             popListener.wait(maxProcessingTimeout);
481         }
482         Assert.assertEquals(1, portstatusDeleteMessageCounter);
483     }
484
485     /**
486      * @throws InterruptedException
487      */
488     private void executeLater() throws InterruptedException {
489         execute(false);
490     }
491
492     /**
493      * @throws InterruptedException
494      */
495     private void executeNow() throws InterruptedException {
496         execute(true);
497         connectionConductor.shutdownPool();
498     }
499
500     /**
501      * @throws InterruptedException
502      */
503     private void execute(boolean join) throws InterruptedException {
504         libSimulation = new Thread(adapter, "junit-adapter");
505         libSimulation.start();
506         if (join) {
507             libSimulation.join();
508         }
509     }
510
511     private static GetFeaturesOutputBuilder getFeatureResponseMsg() {
512         GetFeaturesOutputBuilder getFeaturesOutputBuilder = new GetFeaturesOutputBuilder();
513         getFeaturesOutputBuilder.setDatapathId(new BigInteger("102030405060"));
514         getFeaturesOutputBuilder.setAuxiliaryId((short) 0);
515         getFeaturesOutputBuilder.setBuffers(4L);
516         getFeaturesOutputBuilder.setReserved(0L);
517         getFeaturesOutputBuilder.setTables((short) 2);
518         getFeaturesOutputBuilder.setCapabilities(createCapabilities(84));
519
520         return getFeaturesOutputBuilder;
521     }
522
523     /**
524      * @return
525      */
526     private static Capabilities createCapabilities(long input) {
527         final Boolean FLOW_STATS = (input & (1 << 0)) != 0;
528         final Boolean TABLE_STATS = (input & (1 << 1)) != 0;
529         final Boolean PORT_STATS = (input & (1 << 2)) != 0;
530         final Boolean GROUP_STATS = (input & (1 << 3)) != 0;
531         final Boolean IP_REASM = (input & (1 << 5)) != 0;
532         final Boolean QUEUE_STATS = (input & (1 << 6)) != 0;
533         final Boolean PORT_BLOCKED = (input & (1 << 8)) != 0;
534         Capabilities capabilities = new Capabilities(FLOW_STATS, GROUP_STATS, IP_REASM,
535                 PORT_BLOCKED, PORT_STATS, QUEUE_STATS, TABLE_STATS);
536         return capabilities;
537     }
538
539     public class ExperimenterMessageService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
540         @Override
541         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
542             LOG.debug("Received a packet in Experimenter Service");
543             ConnectionConductorImplTest.this.incrExperimenterMessageCounter();
544             return null;
545         }
546     }
547
548     public class PacketInMessageService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
549         @Override
550         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
551             LOG.debug("Received a packet in PacketIn Service");
552             ConnectionConductorImplTest.this.incrPacketinMessageCounter();
553             return null;
554         }
555     }
556
557     public class FlowRemovedMessageService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
558         @Override
559         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
560             LOG.debug("Received a packet in FlowRemoved Service");
561             ConnectionConductorImplTest.this.incrFlowremovedMessageCounter();
562             return null;
563         }
564     }
565
566     public class PortStatusMessageService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
567         @Override
568         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
569             LOG.debug("Received a packet in PortStatus Service");
570             if ((((PortStatusMessage) msg).getReason().equals(PortReason.OFPPRADD))) {
571                 ConnectionConductorImplTest.this.incrPortstatusAddMessageCounter();
572             } else if (((PortStatusMessage) msg).getReason().equals(PortReason.OFPPRDELETE)) {
573                 ConnectionConductorImplTest.this.incrPortstatusDeleteMessageCounter();
574             } else if (((PortStatusMessage) msg).getReason().equals(PortReason.OFPPRMODIFY)) {
575                 ConnectionConductorImplTest.this.incrPortstatusModifyMessageCounter();
576             }
577             return null;
578         }
579     }
580
581     public class ErrorMessageService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
582         @Override
583         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
584             LOG.debug("Received a packet in Experimenter Service");
585             ConnectionConductorImplTest.this.incrErrorMessageCounter();
586             return null;
587         }
588     }
589
590     /**
591      * Test method for
592      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onExperimenterMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage)}
593      * .
594      *
595      * @throws InterruptedException
596      */
597     @Test
598     public void testOnErrorMessage() throws InterruptedException {
599         simulateV13PostHandshakeState(connectionConductor);
600
601         ErrorMessageBuilder builder1 = new ErrorMessageBuilder();
602         builder1.setVersion((short) 4);
603         builder1.setCode(100);
604         connectionConductor.onErrorMessage(builder1.build());
605         synchronized (popListener) {
606             popListener.wait(maxProcessingTimeout);
607         }
608         Assert.assertEquals(1, errorMessageCounter);
609         builder1.setCode(200);
610         connectionConductor.onErrorMessage(builder1.build());
611         synchronized (popListener) {
612             popListener.wait(maxProcessingTimeout);
613         }
614         Assert.assertEquals(2, errorMessageCounter);
615     }
616
617     /**
618      * @return listener mapping for:
619      * <ul>
620      * <li>experimenter</li>
621      * <li>error</li>
622      * </ul>
623      */
624     private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> assembleTranslatorMapping() {
625         Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping = new HashMap<>();
626         TranslatorKey tKey;
627
628         IMDMessageTranslator<OfHeader, List<DataObject>> objEms = new ExperimenterMessageService();
629         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = new ArrayList<>();
630         existingValues.add(objEms);
631         tKey = new TranslatorKey(4, ExperimenterMessage.class.getName());
632         translatorMapping.put(tKey, existingValues);
633         IMDMessageTranslator<OfHeader, List<DataObject>> objErms = new ErrorMessageService();
634         existingValues.add(objErms);
635         tKey = new TranslatorKey(4, ErrorMessage.class.getName());
636         translatorMapping.put(tKey, existingValues);
637         return translatorMapping;
638     }
639
640     /**
641      * Test method for
642      * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#processPortStatusMsg(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage)}
643      * <br><br>
644      * Tests for getting features from port status message by port version
645      * <ul>
646      * <li>features are malformed - one of them is null</li>
647      * <li>mismatch between port version and port features</li>
648      * <li>mismatch between port version and port features</li>
649      * <li>non-existing port version</li>
650      * <li>port version OF 1.0</li>
651      * <li>port version OF 1.3</li>
652      * </ul>
653      */
654     @Test
655     public void testProcessPortStatusMsg() {
656         simulateV13PostHandshakeState(connectionConductor);
657
658         long portNumber = 90L;
659         long portNumberV10 = 91L;
660         PortStatusMessage msg;
661
662         PortStatusMessageBuilder builder = new PortStatusMessageBuilder();
663         PortFeatures features = new PortFeatures(true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);
664         PortFeatures featuresMal = new PortFeatures(true, false, false, false, null, false, false, false, false, false, false, false, false, false, false, false);
665         PortFeaturesV10 featuresV10 = new PortFeaturesV10(true, false, false, false, false, false, false, false, false, false, false, false);
666
667         //Malformed features            
668         builder.setVersion((short) 1).setPortNo(portNumber).setReason(PortReason.OFPPRADD).setCurrentFeatures(featuresMal);
669         connectionConductor.processPortStatusMsg(builder.build());
670         Assert.assertTrue(connectionConductor.getSessionContext().getPortsBandwidth().isEmpty());
671         Assert.assertTrue(connectionConductor.getSessionContext().getPhysicalPorts().isEmpty());
672
673         //Version-features mismatch             
674         builder.setCurrentFeatures(features);
675         connectionConductor.processPortStatusMsg(builder.build());
676         Assert.assertTrue(connectionConductor.getSessionContext().getPortsBandwidth().isEmpty());
677         Assert.assertTrue(connectionConductor.getSessionContext().getPhysicalPorts().isEmpty());
678
679         //Non existing version
680         builder.setVersion((short) 0);
681         connectionConductor.processPortStatusMsg(builder.build());
682         Assert.assertTrue(connectionConductor.getSessionContext().getPortsBandwidth().isEmpty());
683         Assert.assertTrue(connectionConductor.getSessionContext().getPhysicalPorts().isEmpty());
684
685         //Version OF 1.3
686         builder.setVersion((short) 4);
687         msg = builder.build();
688         connectionConductor.processPortStatusMsg(builder.build());
689         Assert.assertTrue(connectionConductor.getSessionContext().getPortBandwidth(portNumber));
690         Assert.assertEquals(connectionConductor.getSessionContext().getPhysicalPort(portNumber), msg);
691
692         //Version OF 1.0
693         builder.setVersion((short) 1).setPortNo(portNumberV10).setCurrentFeatures(null).setCurrentFeaturesV10(featuresV10);
694         msg = builder.build();
695         connectionConductor.processPortStatusMsg(builder.build());
696         Assert.assertTrue(connectionConductor.getSessionContext().getPortBandwidth(portNumberV10));
697         Assert.assertEquals(connectionConductor.getSessionContext().getPhysicalPort(portNumberV10), msg);
698     }
699
700     private void simulateV13PostHandshakeState(ConnectionConductorImpl conductor) {
701         GetFeaturesOutputBuilder featureOutput = getFeatureResponseMsg();
702         conductor.postHandshakeBasic(featureOutput.build(), OFConstants.OFP_VERSION_1_3);
703     }
704 }