tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.impl; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNotNull; |
| 6 | import static org.junit.Assert.assertTrue; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 7 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED; |
| 8 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 9 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_UPDATED; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.List; |
| 13 | |
| 14 | import org.junit.After; |
| 15 | import org.junit.Before; |
| 16 | import org.junit.Test; |
| 17 | import org.onlab.onos.event.impl.TestEventDispatcher; |
| 18 | import org.onlab.onos.net.DefaultDevice; |
| 19 | import org.onlab.onos.net.Device; |
| 20 | import org.onlab.onos.net.Device.Type; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.net.MastershipRole; |
| 23 | import org.onlab.onos.net.Port; |
| 24 | import org.onlab.onos.net.PortNumber; |
| 25 | import org.onlab.onos.net.device.DeviceListener; |
| 26 | import org.onlab.onos.net.device.DeviceService; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.DefaultFlowRule; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 29 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 31 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 32 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 33 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 34 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 35 | import org.onlab.onos.net.flow.FlowRuleService; |
| 36 | import org.onlab.onos.net.flow.TrafficSelector; |
| 37 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 38 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 39 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 40 | import org.onlab.onos.net.provider.AbstractProvider; |
| 41 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.trivial.impl.SimpleFlowRuleStore; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 43 | |
| 44 | import com.google.common.collect.Lists; |
| 45 | import com.google.common.collect.Sets; |
| 46 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Test codifying the flow rule service & flow rule provider service contracts. |
| 49 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 50 | public class FlowRuleManagerTest { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 51 | |
tom | f6ab215 | 2014-09-18 12:08:29 -0700 | [diff] [blame] | 52 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 53 | private static final DeviceId DID = DeviceId.deviceId("of:001"); |
| 54 | private static final Device DEV = new DefaultDevice( |
| 55 | PID, DID, Type.SWITCH, "", "", "", ""); |
| 56 | |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 57 | private FlowRuleManager mgr; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 58 | |
| 59 | protected FlowRuleService service; |
| 60 | protected FlowRuleProviderRegistry registry; |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 61 | protected FlowRuleProviderService providerService; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 62 | protected TestProvider provider; |
| 63 | protected TestListener listener = new TestListener(); |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 67 | mgr = new FlowRuleManager(); |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 68 | mgr.store = new SimpleFlowRuleStore(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 69 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 70 | mgr.deviceService = new TestDeviceService(); |
| 71 | service = mgr; |
| 72 | registry = mgr; |
| 73 | |
| 74 | mgr.activate(); |
| 75 | mgr.addListener(listener); |
| 76 | provider = new TestProvider(PID); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 77 | providerService = registry.register(provider); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 78 | assertTrue("provider should be registered", |
| 79 | registry.getProviders().contains(provider.id())); |
| 80 | } |
| 81 | |
| 82 | @After |
| 83 | public void tearDown() { |
| 84 | registry.unregister(provider); |
| 85 | assertFalse("provider should not be registered", |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 86 | registry.getProviders().contains(provider.id())); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 87 | service.removeListener(listener); |
| 88 | mgr.deactivate(); |
| 89 | mgr.eventDispatcher = null; |
| 90 | mgr.deviceService = null; |
| 91 | } |
| 92 | |
| 93 | private FlowRule flowRule(int tsval, int trval) { |
| 94 | TestSelector ts = new TestSelector(tsval); |
| 95 | TestTreatment tr = new TestTreatment(trval); |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 96 | return new DefaultFlowRule(DID, ts, tr, 0); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 97 | } |
| 98 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 99 | private FlowRule flowRule(FlowRule rule, FlowRuleState state) { |
| 100 | return new DefaultFlowRule(rule, state); |
| 101 | } |
| 102 | |
| 103 | private FlowRule addFlowRule(int hval) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 104 | FlowRule rule = flowRule(hval, hval); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 105 | providerService.flowAdded(rule); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 106 | assertNotNull("rule should be found", service.getFlowEntries(DID)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 107 | return rule; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private void validateEvents(FlowRuleEvent.Type ... events) { |
| 111 | if (events == null) { |
| 112 | assertTrue("events generated", listener.events.isEmpty()); |
| 113 | } |
| 114 | |
| 115 | int i = 0; |
| 116 | for (FlowRuleEvent e : listener.events) { |
| 117 | assertTrue("unexpected event", e.type().equals(events[i])); |
| 118 | i++; |
| 119 | } |
| 120 | |
| 121 | assertEquals("mispredicted number of events", |
| 122 | events.length, listener.events.size()); |
| 123 | |
| 124 | listener.events.clear(); |
| 125 | } |
| 126 | |
| 127 | private int flowCount() { |
| 128 | return Sets.newHashSet(service.getFlowEntries(DID)).size(); |
| 129 | } |
| 130 | @Test |
| 131 | public void getFlowEntries() { |
| 132 | assertTrue("store should be empty", |
| 133 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
| 134 | addFlowRule(1); |
| 135 | addFlowRule(2); |
| 136 | assertEquals("2 rules should exist", 2, flowCount()); |
| 137 | validateEvents(RULE_ADDED, RULE_ADDED); |
| 138 | |
| 139 | addFlowRule(1); |
| 140 | assertEquals("should still be 2 rules", 2, flowCount()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 141 | validateEvents(RULE_UPDATED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 142 | } |
| 143 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 144 | |
| 145 | //backing store is sensitive to the order of additions/removals |
| 146 | private boolean validateState(FlowRuleState... state) { |
| 147 | Iterable<FlowRule> rules = service.getFlowEntries(DID); |
| 148 | int i = 0; |
| 149 | for (FlowRule f : rules) { |
| 150 | if (f.state() != state[i]) { |
| 151 | return false; |
| 152 | } |
| 153 | i++; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 158 | @Test |
| 159 | public void applyFlowRules() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 160 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 161 | FlowRule r1 = flowRule(1, 1); |
| 162 | FlowRule r2 = flowRule(1, 2); |
| 163 | FlowRule r3 = flowRule(1, 3); |
| 164 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 165 | assertTrue("store should be empty", |
| 166 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 167 | mgr.applyFlowRules(r1, r2, r3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 168 | assertEquals("3 rules should exist", 3, flowCount()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 169 | assertTrue("Entries should be pending add.", |
| 170 | validateState(FlowRuleState.PENDING_ADD, FlowRuleState.PENDING_ADD, |
| 171 | FlowRuleState.PENDING_ADD)); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void removeFlowRules() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 176 | FlowRule f1 = addFlowRule(1); |
| 177 | FlowRule f2 = addFlowRule(2); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 178 | addFlowRule(3); |
| 179 | assertEquals("3 rules should exist", 3, flowCount()); |
| 180 | validateEvents(RULE_ADDED, RULE_ADDED, RULE_ADDED); |
| 181 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 182 | FlowRule rem1 = flowRule(f1, FlowRuleState.REMOVED); |
| 183 | FlowRule rem2 = flowRule(f2, FlowRuleState.REMOVED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 184 | mgr.removeFlowRules(rem1, rem2); |
| 185 | //removing from north, so no events generated |
| 186 | validateEvents(); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 187 | assertEquals("3 rule should exist", 3, flowCount()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 188 | assertTrue("Entries should be pending remove.", |
| 189 | validateState(FlowRuleState.CREATED, FlowRuleState.PENDING_REMOVE, |
| 190 | FlowRuleState.PENDING_REMOVE)); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 191 | |
| 192 | mgr.removeFlowRules(rem1); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 193 | assertEquals("3 rule should still exist", 3, flowCount()); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void flowRemoved() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 198 | FlowRule f1 = addFlowRule(1); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 199 | addFlowRule(2); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 200 | FlowRule rem1 = flowRule(f1, FlowRuleState.REMOVED); |
| 201 | providerService.flowRemoved(rem1); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 202 | validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED); |
| 203 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 204 | providerService.flowRemoved(rem1); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 205 | validateEvents(); |
| 206 | } |
| 207 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 208 | @Test |
| 209 | public void flowMetrics() { |
| 210 | FlowRule f1 = flowRule(1, 1); |
| 211 | FlowRule f2 = flowRule(2, 2); |
| 212 | FlowRule f3 = flowRule(3, 3); |
| 213 | |
| 214 | FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 215 | FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 216 | mgr.applyFlowRules(f1, f2, f3); |
| 217 | |
| 218 | providerService.pushFlowMetrics(DID, Lists.newArrayList(updatedF1, updatedF2)); |
| 219 | |
| 220 | assertTrue("Entries should be added.", |
| 221 | validateState(FlowRuleState.PENDING_ADD, FlowRuleState.ADDED, |
| 222 | FlowRuleState.ADDED)); |
| 223 | //TODO: add tests for flowmissing and extraneous flows |
| 224 | } |
| 225 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 226 | private static class TestListener implements FlowRuleListener { |
| 227 | final List<FlowRuleEvent> events = new ArrayList<>(); |
| 228 | |
| 229 | @Override |
| 230 | public void event(FlowRuleEvent event) { |
| 231 | events.add(event); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | private static class TestDeviceService implements DeviceService { |
| 236 | |
| 237 | @Override |
| 238 | public int getDeviceCount() { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Iterable<Device> getDevices() { |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public Device getDevice(DeviceId deviceId) { |
| 249 | return DEV; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public MastershipRole getRole(DeviceId deviceId) { |
| 254 | return null; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public List<Port> getPorts(DeviceId deviceId) { |
| 259 | return null; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 264 | return null; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public boolean isAvailable(DeviceId deviceId) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void addListener(DeviceListener listener) { |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void removeListener(DeviceListener listener) { |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | |
| 282 | private class TestProvider extends AbstractProvider implements FlowRuleProvider { |
| 283 | |
| 284 | protected TestProvider(ProviderId id) { |
| 285 | super(PID); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void applyFlowRule(FlowRule... flowRules) { |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void removeFlowRule(FlowRule... flowRules) { |
| 294 | } |
| 295 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private class TestSelector implements TrafficSelector { |
| 299 | |
| 300 | //for controlling hashcode uniqueness; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 301 | private final int testval; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 302 | |
| 303 | public TestSelector(int val) { |
| 304 | testval = val; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public List<Criterion> criteria() { |
| 309 | return null; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public int hashCode() { |
| 314 | return testval; |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public boolean equals(Object o) { |
| 319 | if (o instanceof TestSelector) { |
| 320 | return this.testval == ((TestSelector) o).testval; |
| 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | private class TestTreatment implements TrafficTreatment { |
| 327 | |
| 328 | //for controlling hashcode uniqueness; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 329 | private final int testval; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 330 | |
| 331 | public TestTreatment(int val) { |
| 332 | testval = val; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public List<Instruction> instructions() { |
| 337 | return null; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public int hashCode() { |
| 342 | return testval; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public boolean equals(Object o) { |
| 347 | if (o instanceof TestTreatment) { |
| 348 | return this.testval == ((TestTreatment) o).testval; |
| 349 | } |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
| 355 | } |