blob: 61ac9d63cc38962e2ee46ee9e8fdecfea960df46 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.util;
19
20import static org.easymock.EasyMock.createNiceMock;
21import static org.easymock.EasyMock.expect;
22import static org.easymock.EasyMock.replay;
23import static org.easymock.EasyMock.verify;
24
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070025import java.io.PrintWriter;
26import java.io.StringWriter;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import java.util.ArrayList;
28import java.util.List;
29import java.util.Random;
30
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031import net.floodlightcontroller.core.IOFMessageListener;
32import net.floodlightcontroller.test.FloodlightTestCase;
33
Jonathan Harta88fd242014-04-03 11:24:54 -070034import org.junit.Test;
35import org.openflow.protocol.OFType;
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
Jonathan Harta88fd242014-04-03 11:24:54 -070038
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039public class MessageDispatcherTest extends FloodlightTestCase {
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070040 private static final Logger log = LoggerFactory.getLogger(MessageDispatcherTest.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041
42 IOFMessageListener createLMock(String name) {
43 IOFMessageListener mock = createNiceMock(IOFMessageListener.class);
44 expect(mock.getName()).andReturn(name).anyTimes();
45 return mock;
46 }
Ray Milkey269ffb92014-04-03 14:43:30 -070047
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048 void addPrereqs(IOFMessageListener mock, String... deps) {
49 for (String dep : deps) {
50 expect(mock.isCallbackOrderingPrereq(OFType.PACKET_IN, dep)).andReturn(true).anyTimes();
51 }
52 }
Ray Milkey269ffb92014-04-03 14:43:30 -070053
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 void testOrdering(ArrayList<IOFMessageListener> inputListeners) {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 ListenerDispatcher<OFType, IOFMessageListener> ld =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056 new ListenerDispatcher<OFType, IOFMessageListener>();
Ray Milkey269ffb92014-04-03 14:43:30 -070057
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080058 for (IOFMessageListener l : inputListeners) {
59 ld.addListener(OFType.PACKET_IN, l);
60 }
61 for (IOFMessageListener l : inputListeners) {
62 verify(l);
63 }
Ray Milkey269ffb92014-04-03 14:43:30 -070064
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080065 List<IOFMessageListener> result = ld.getOrderedListeners();
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070066 StringWriter sw = new StringWriter();
67 PrintWriter out = new PrintWriter(sw);
68
69 out.print("Ordering: ");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080070 for (IOFMessageListener l : result) {
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070071 out.print(l.getName());
72 out.print(",");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080073 }
Yuta HIGUCHI258e71f2014-07-25 12:52:25 -070074 out.print("\n");
75 log.debug(sw.toString());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076
77 for (int ind_i = 0; ind_i < result.size(); ind_i++) {
78 IOFMessageListener i = result.get(ind_i);
Ray Milkey269ffb92014-04-03 14:43:30 -070079 for (int ind_j = ind_i + 1; ind_j < result.size(); ind_j++) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080 IOFMessageListener j = result.get(ind_j);
Ray Milkey269ffb92014-04-03 14:43:30 -070081
82 boolean orderwrong =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083 (i.isCallbackOrderingPrereq(OFType.PACKET_IN, j.getName()) ||
Ray Milkey269ffb92014-04-03 14:43:30 -070084 j.isCallbackOrderingPostreq(OFType.PACKET_IN, i.getName()));
85 assertFalse("Invalid order: " +
86 ind_i + " (" + i.getName() + ") " +
87 ind_j + " (" + j.getName() + ") ", orderwrong);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 }
89 }
90 }
Ray Milkey269ffb92014-04-03 14:43:30 -070091
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 void randomTestOrdering(ArrayList<IOFMessageListener> mocks) {
93 Random rand = new Random(0);
Ray Milkey269ffb92014-04-03 14:43:30 -070094 ArrayList<IOFMessageListener> random =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095 new ArrayList<IOFMessageListener>();
96 random.addAll(mocks);
97 for (int i = 0; i < 20; i++) {
98 for (int j = 0; j < random.size(); j++) {
Ray Milkey269ffb92014-04-03 14:43:30 -070099 int ind = rand.nextInt(mocks.size() - 1);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800100 IOFMessageListener tmp = random.get(j);
101 random.set(j, random.get(ind));
102 random.set(ind, tmp);
103 }
104 testOrdering(random);
105 }
106 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700107
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 @Test
109 public void testCallbackOrderingSimple() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800111 new ArrayList<IOFMessageListener>();
112 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 }
115 for (int i = 1; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800117 }
118 for (IOFMessageListener l : mocks) {
119 replay(l);
120 }
121 randomTestOrdering(mocks);
122 }
123
124 @Test
125 public void testCallbackOrderingPartial() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800127 new ArrayList<IOFMessageListener>();
128 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800130 }
131 for (int i = 1; i < 5; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 }
134 for (int i = 6; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 }
137 for (IOFMessageListener l : mocks) {
138 replay(l);
139 }
140 randomTestOrdering(mocks);
141 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700142
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800143
144 @Test
145 public void testCallbackOrderingPartial2() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 ArrayList<IOFMessageListener> mocks =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800147 new ArrayList<IOFMessageListener>();
148 for (int i = 0; i < 10; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 mocks.add(createLMock("" + i));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800150 }
151 for (int i = 2; i < 5; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800153 }
154 for (int i = 6; i < 9; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 addPrereqs(mocks.get(i), "" + (i - 1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800156 }
157 for (IOFMessageListener l : mocks) {
158 replay(l);
159 }
160 randomTestOrdering(mocks);
161 }
162}