copyright header added
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / MDControllerTest.java
1 /**
2  * Copyright IBM Corporation, 2013.  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.List;
11
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.openflow.md.core.session.SessionContext;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class MDControllerTest {
25     protected static final Logger LOG = LoggerFactory
26             .getLogger(ConnectionConductorImplTest.class);
27
28     protected MDController controller;
29
30
31     /**
32      * @throws java.lang.Exception
33      */
34     @Before
35     public void setUp() throws Exception {
36         controller = new MDController();
37         controller.init();
38     }
39
40     /**
41      * @throws java.lang.Exception
42      */
43     @After
44     public void tearDown() throws Exception {
45         controller = null;
46     }
47
48
49     /**
50      * Test method for
51      * {@link org.opendaylight.openflowplugin.openflow.md.core.MDController#addMessageListeners}
52      * .
53      */
54     @Test
55     public void testAddMessageListeners() {
56         //clean translators
57         controller.getMessageTranslators().clear();
58
59         // Empty map
60         int size = controller.getMessageTranslators().size();
61         Assert.assertEquals(0, size);
62         // Add one
63         IMDMessageTranslator<OfHeader, List<DataObject>> objDps = new DataPacketService() ;
64         controller.addMessageTranslator(PacketIn.class, 4, objDps);
65         size = controller.getMessageTranslators().size();
66         Assert.assertEquals(1, size);
67         // Remove one
68         controller.removeMessageTranslator(PacketIn.class, 4, objDps);
69         size = controller.getMessageTranslators().size();
70         Assert.assertEquals(0, size);
71         // Add two and remove One
72         IMDMessageTranslator objFps = new FlowProgrammerService();
73         controller.addMessageTranslator(PacketIn.class, 4, objDps);
74         controller.addMessageTranslator(FlowRemoved.class, 4, objFps);
75         controller.removeMessageTranslator(FlowRemoved.class, 4, objFps);
76         size = controller.getMessageTranslators().size();
77         Assert.assertEquals(1, size);
78         // Add one more and remove both
79         controller.addMessageTranslator(FlowRemoved.class, 4, objFps);
80         controller.removeMessageTranslator(PacketIn.class, 4, objDps);
81         controller.removeMessageTranslator(FlowRemoved.class, 4, objFps);
82         size = controller.getMessageTranslators().size();
83         Assert.assertEquals(0, size);
84         // Add multiple listeners to messageTypes
85         controller.addMessageTranslator(PacketIn.class, 4, objDps);
86         controller.addMessageTranslator(PacketIn.class, 4, objFps); // Duplicate value entry
87         controller.addMessageTranslator(FlowRemoved.class, 4, objFps);
88         size = controller.getMessageTranslators().size();
89         Assert.assertEquals(2, size);
90         // Remove one of the multiple listener, still size remains same
91         controller.removeMessageTranslator(PacketIn.class, 4, objFps);
92         size = controller.getMessageTranslators().size();
93         Assert.assertEquals(2, size);
94     }
95
96     private class DataPacketService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
97         @Override
98         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
99             LOG.debug("Received a packet in DataPacket Service");
100             return null;
101         }
102     }
103
104     private class FlowProgrammerService implements IMDMessageTranslator<OfHeader, DataObject> {
105         @Override
106         public DataObject translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
107             LOG.debug("Received a packet in Flow Programmer Service");
108             return null;
109         }
110     }
111
112
113
114 }