Add missing license headers
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / MDControllerTest.java
1 /**
2  * Copyright (c) 2013 IBM Corporation.  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.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;
25
26 public class MDControllerTest {
27     protected static final Logger LOG = LoggerFactory
28             .getLogger(ConnectionConductorImplTest.class);
29
30     protected MDController controller;
31
32
33     /**
34      * @throws java.lang.Exception
35      */
36     @Before
37     public void setUp() throws Exception {
38         controller = new MDController();
39         controller.init();
40     }
41
42     /**
43      * @throws java.lang.Exception
44      */
45     @After
46     public void tearDown() throws Exception {
47         controller = null;
48     }
49
50
51     /**
52      * Test method for
53      * {@link org.opendaylight.openflowplugin.openflow.md.core.MDController#addMessageListeners}
54      * .
55      */
56     @Test
57     public void testAddMessageListeners() {
58         //clean translators
59         controller.getMessageTranslators().clear();
60
61         // Empty map
62         int size = controller.getMessageTranslators().size();
63         Assert.assertEquals(0, size);
64         // Add one
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);
69         // Remove one
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);
96     }
97
98     private class DataPacketService implements IMDMessageTranslator<OfHeader, List<DataObject>> {
99         @Override
100         public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
101             LOG.debug("Received a packet in DataPacket Service");
102             return null;
103         }
104     }
105
106     private class FlowProgrammerService implements IMDMessageTranslator<OfHeader, DataObject> {
107         @Override
108         public DataObject translate(SwitchConnectionDistinguisher cookie, SessionContext sw, OfHeader msg) {
109             LOG.debug("Received a packet in Flow Programmer Service");
110             return null;
111         }
112     }
113
114
115
116 }