2 * Copyright IBM Corporation, 2013. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.openflow.md.core;
10 import java.util.List;
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
17 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class MDControllerTest {
27 protected static final Logger LOG = LoggerFactory
28 .getLogger(ConnectionConductorImplTest.class);
30 protected MDController controller;
34 * @throws java.lang.Exception
37 public void setUp() throws Exception {
38 controller = new MDController();
43 * @throws java.lang.Exception
46 public void tearDown() throws Exception {
53 * {@link org.opendaylight.openflowplugin.openflow.md.core.MDController#addMessageListeners}
57 public void testAddMessageListeners() {
59 controller.getMessageTranslators().clear();
62 int size = controller.getMessageTranslators().size();
63 Assert.assertEquals(0, size);
65 IMDMessageTranslator<OfHeader, List<DataObject>> objDps = new DataPacketService() ;
66 controller.addMessageTranslator(PacketIn.class, 4, objDps);
67 size = controller.getMessageTranslators().size();
68 Assert.assertEquals(1, size);
70 controller.removeMessageTranslator(PacketIn.class, 4, objDps);
71 size = controller.getMessageTranslators().size();
72 Assert.assertEquals(0, size);
73 // Add two and remove One
74 IMDMessageTranslator objFps = new FlowProgrammerService();
75 controller.addMessageTranslator(PacketIn.class, 4, objDps);
76 controller.addMessageTranslator(FlowRemoved.class, 4, objFps);
77 controller.removeMessageTranslator(FlowRemoved.class, 4, objFps);
78 size = controller.getMessageTranslators().size();
79 Assert.assertEquals(1, size);
80 // Add one more and remove both
81 controller.addMessageTranslator(FlowRemoved.class, 4, objFps);
82 controller.removeMessageTranslator(PacketIn.class, 4, objDps);
83 controller.removeMessageTranslator(FlowRemoved.class, 4, objFps);
84 size = controller.getMessageTranslators().size();
85 Assert.assertEquals(0, size);
86 // Add multiple listeners to messageTypes
87 controller.addMessageTranslator(PacketIn.class, 4, objDps);
88 controller.addMessageTranslator(PacketIn.class, 4, objFps); // Duplicate value entry
89 controller.addMessageTranslator(FlowRemoved.class, 4, objFps);
90 size = controller.getMessageTranslators().size();
91 Assert.assertEquals(2, size);
92 // Remove one of the multiple listener, still size remains same
93 controller.removeMessageTranslator(PacketIn.class, 4, objFps);
94 size = controller.getMessageTranslators().size();
95 Assert.assertEquals(2, size);
98 private class DataPacketService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
100 public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
101 LOG.debug("Received a packet in DataPacket Service");
106 private class FlowProgrammerService implements IMDMessageTranslator<OfHeader, DataObject> {
108 public DataObject translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
109 LOG.debug("Received a packet in Flow Programmer Service");