Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.net.flowobjective.impl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Ignore; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.MplsLabel; |
| 28 | import org.onlab.packet.VlanId; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.DefaultApplicationId; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.behaviour.NextGroup; |
| 35 | import org.onosproject.net.behaviour.Pipeliner; |
| 36 | import org.onosproject.net.behaviour.PipelinerAdapter; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceService; |
| 38 | import org.onosproject.net.driver.DriverService; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.flow.criteria.Criteria; |
| 44 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 45 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 46 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 47 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 48 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 49 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 50 | import org.onosproject.net.flowobjective.NextObjective; |
| 51 | import org.onosproject.net.flowobjective.Objective; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 54 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
| 55 | |
| 56 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 57 | import static org.easymock.EasyMock.createMock; |
| 58 | import static org.easymock.EasyMock.expect; |
| 59 | import static org.easymock.EasyMock.replay; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 60 | import static org.easymock.EasyMock.reset; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 61 | import static org.easymock.EasyMock.verify; |
| 62 | import static org.junit.Assert.assertTrue; |
| 63 | import static org.junit.Assert.assertEquals; |
| 64 | import static org.onlab.junit.TestTools.assertAfter; |
| 65 | import static org.onlab.util.Tools.groupedThreads; |
| 66 | |
| 67 | import java.util.Collection; |
| 68 | import java.util.List; |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 69 | import java.util.Objects; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 70 | import java.util.Random; |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 71 | import java.util.concurrent.atomic.AtomicInteger; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 72 | |
| 73 | public class InOrderFlowObjectiveManagerTest { |
| 74 | private InOrderFlowObjectiveManager mgr; |
| 75 | |
| 76 | private static final int PRIORITY = 1000; |
| 77 | private static final ApplicationId APP_ID = new DefaultApplicationId(1, "org.onosproject.test"); |
| 78 | private static final DeviceId DEV1 = DeviceId.deviceId("of:1"); |
| 79 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 80 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 81 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 82 | private static final PortNumber P4 = PortNumber.portNumber(4); |
| 83 | private static final MacAddress M1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 84 | private static final MacAddress M2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 85 | private static final MacAddress M3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 86 | private static final VlanId V1 = VlanId.vlanId((short) 10); |
| 87 | private static final VlanId V2 = VlanId.vlanId((short) 20); |
| 88 | private static final VlanId V3 = VlanId.vlanId((short) 30); |
| 89 | private static final TrafficSelector S1 = DefaultTrafficSelector.builder() |
| 90 | .matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf("10.0.0.1/32")).build(); |
| 91 | private static final TrafficSelector S2 = DefaultTrafficSelector.builder() |
| 92 | .matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf("10.0.0.2/32")).build(); |
| 93 | private static final int NID1 = 1; |
| 94 | private static final int NID2 = 2; |
| 95 | private static final NextGroup NGRP1 = () -> new byte[] {0x00, 0x01}; |
| 96 | private static final NextGroup NGRP2 = () -> new byte[] {0x02, 0x03}; |
| 97 | |
| 98 | // Delay flow objectives OFFSET + rand(0, BOUND) millis |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 99 | private static final int DEFAULT_OFFSET = 10; // ms |
| 100 | private static final int DEFAULT_BOUND = 40; // ms |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 101 | private static final int TIMEOUT_THRESH = 100; // ms |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 102 | private static int offset = DEFAULT_OFFSET; |
| 103 | private static int bound = DEFAULT_BOUND; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 104 | |
| 105 | private static final FilteringObjective FILT1 = buildFilteringObjective(P2, V3, M3, 1).add(); |
| 106 | private static final FilteringObjective FILT2 = buildFilteringObjective(P2, V2, M2, 2).add(); |
| 107 | private static final FilteringObjective FILT3 = buildFilteringObjective(P2, V3, M3, 3).remove(); |
| 108 | private static final FilteringObjective FILT4 = buildFilteringObjective(P1, V1, M1, 4).add(); |
| 109 | private static final FilteringObjective FILT5 = buildFilteringObjective(P2, V2, M2, 5).remove(); |
| 110 | private static final FilteringObjective FILT6 = buildFilteringObjective(P1, V1, M1, 6).remove(); |
| 111 | private static final FilteringObjective FILT7 = buildFilteringObjective(P2, V3, M3, 7).add(); |
| 112 | private List<FilteringObjective> expectFiltObjs = Lists.newCopyOnWriteArrayList( |
| 113 | Lists.newArrayList(FILT1, FILT2, FILT3, FILT4, FILT5, FILT6, FILT7)); |
| 114 | |
| 115 | private static final NextObjective NEXT1 = buildNextObjective(NID1, V1, Sets.newHashSet(P1)).add(); |
| 116 | private static final NextObjective NEXT2 = buildNextObjective(NID2, V2, Sets.newHashSet(P3)).add(); |
| 117 | private static final NextObjective NEXT3 = buildNextObjective(NID1, V1, Sets.newHashSet(P1, P2)).addToExisting(); |
| 118 | private static final NextObjective NEXT4 = buildNextObjective(NID2, V2, Sets.newHashSet(P3, P4)).addToExisting(); |
| 119 | private static final NextObjective NEXT5 = buildNextObjective(NID1, V1, Sets.newHashSet(P1)).removeFromExisting(); |
| 120 | private static final NextObjective NEXT6 = buildNextObjective(NID2, V2, Sets.newHashSet(P3)).removeFromExisting(); |
| 121 | private static final NextObjective NEXT7 = buildNextObjective(NID1, V1, Sets.newHashSet()).remove(); |
| 122 | private static final NextObjective NEXT8 = buildNextObjective(NID2, V2, Sets.newHashSet()).remove(); |
| 123 | private List<NextObjective> expectNextObjs = Lists.newCopyOnWriteArrayList( |
| 124 | Lists.newArrayList(NEXT1, NEXT2, NEXT3, NEXT4, NEXT5, NEXT6, NEXT7, NEXT8)); |
| 125 | private List<NextObjective> expectNextObjsPending = Lists.newCopyOnWriteArrayList( |
| 126 | Lists.newArrayList(NEXT5, NEXT6, NEXT1, NEXT2, NEXT3, NEXT4, NEXT7, NEXT8)); |
| 127 | |
| 128 | private static final ForwardingObjective FWD1 = buildFwdObjective(S1, NID1).add(); |
| 129 | private static final ForwardingObjective FWD2 = buildFwdObjective(S2, NID2).add(); |
| 130 | private static final ForwardingObjective FWD3 = buildFwdObjective(S1, NID2).add(); |
| 131 | private static final ForwardingObjective FWD4 = buildFwdObjective(S2, NID1).add(); |
| 132 | private static final ForwardingObjective FWD5 = buildFwdObjective(S1, NID2).remove(); |
| 133 | private static final ForwardingObjective FWD6 = buildFwdObjective(S2, NID1).remove(); |
| 134 | private List<ForwardingObjective> expectFwdObjs = Lists.newCopyOnWriteArrayList( |
| 135 | Lists.newArrayList(FWD1, FWD2, FWD3, FWD4, FWD5, FWD6)); |
| 136 | |
| 137 | private List<Objective> actualObjs = Lists.newCopyOnWriteArrayList(); |
| 138 | |
| 139 | private Pipeliner pipeliner = new PipelinerAdapter() { |
| 140 | @Override |
| 141 | public void filter(FilteringObjective filterObjective) { |
| 142 | recordObjective(filterObjective); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void forward(ForwardingObjective forwardObjective) { |
| 147 | recordObjective(forwardObjective); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void next(NextObjective nextObjective) { |
| 152 | recordObjective(nextObjective); |
| 153 | |
| 154 | // Notify delegate when the next obj is completed |
| 155 | ObjectiveEvent.Type type; |
| 156 | if (nextObjective.op() == Objective.Operation.ADD || |
| 157 | nextObjective.op() == Objective.Operation.ADD_TO_EXISTING) { |
| 158 | type = ObjectiveEvent.Type.ADD; |
| 159 | } else if (nextObjective.op() == Objective.Operation.REMOVE || |
| 160 | nextObjective.op() == Objective.Operation.REMOVE_FROM_EXISTING) { |
| 161 | type = ObjectiveEvent.Type.REMOVE; |
| 162 | } else { |
| 163 | return; |
| 164 | } |
| 165 | mgr.delegate.notify(new ObjectiveEvent(type, nextObjective.id())); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Record the objectives. |
| 170 | * The random delay is introduced in order to mimic pipeline and flow operation behavior. |
| 171 | * |
| 172 | * @param obj Flow objective |
| 173 | */ |
| 174 | private void recordObjective(Objective obj) { |
| 175 | try { |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 176 | Thread.sleep(new Random().nextInt(bound) + offset); |
Ray Milkey | 95c9e0f | 2018-05-30 14:16:37 -0700 | [diff] [blame] | 177 | if (!actualObjs.contains(obj)) { |
| 178 | actualObjs.add(obj); |
| 179 | } |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 180 | obj.context().ifPresent(c -> c.onSuccess(obj)); |
| 181 | } catch (Exception e) { |
| 182 | obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.UNKNOWN)); |
| 183 | } |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | @Before |
| 188 | public void setUp() { |
| 189 | mgr = new InOrderFlowObjectiveManager(); |
| 190 | mgr.pipeliners.put(DEV1, pipeliner); |
| 191 | mgr.executorService = newFixedThreadPool(4, groupedThreads("foo", "bar")); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 192 | mgr.cfgService = createMock(ComponentConfigService.class); |
| 193 | mgr.deviceService = createMock(DeviceService.class); |
| 194 | mgr.driverService = createMock(DriverService.class); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 195 | mgr.flowObjectiveStore = createMock(FlowObjectiveStore.class); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 196 | mgr.activate(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 197 | |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 198 | reset(mgr.flowObjectiveStore); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 199 | offset = DEFAULT_OFFSET; |
| 200 | bound = DEFAULT_BOUND; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 201 | actualObjs.clear(); |
| 202 | } |
| 203 | |
| 204 | @Test |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 205 | public void filter() { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 206 | expectFiltObjs.forEach(filtObj -> mgr.filter(DEV1, filtObj)); |
| 207 | |
| 208 | // Wait for the pipeline operation to complete |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 209 | int expectedTime = (bound + offset) * 7; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 210 | assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFiltObjs.size(), actualObjs.size())); |
| 211 | |
| 212 | assertTrue(actualObjs.indexOf(FILT1) < actualObjs.indexOf(FILT2)); |
| 213 | assertTrue(actualObjs.indexOf(FILT2) < actualObjs.indexOf(FILT3)); |
| 214 | assertTrue(actualObjs.indexOf(FILT3) < actualObjs.indexOf(FILT5)); |
| 215 | assertTrue(actualObjs.indexOf(FILT5) < actualObjs.indexOf(FILT7)); |
| 216 | assertTrue(actualObjs.indexOf(FILT4) < actualObjs.indexOf(FILT6)); |
| 217 | } |
| 218 | |
| 219 | @Test |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 220 | public void forward() { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 221 | expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3); |
| 222 | expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3); |
| 223 | replay(mgr.flowObjectiveStore); |
| 224 | |
| 225 | expectFwdObjs.forEach(fwdObj -> mgr.forward(DEV1, fwdObj)); |
| 226 | |
| 227 | // Wait for the pipeline operation to complete |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 228 | int expectedTime = (bound + offset) * 6; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 229 | assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjs.size(), actualObjs.size())); |
| 230 | |
| 231 | assertTrue(actualObjs.indexOf(FWD1) < actualObjs.indexOf(FWD3)); |
| 232 | assertTrue(actualObjs.indexOf(FWD3) < actualObjs.indexOf(FWD5)); |
| 233 | assertTrue(actualObjs.indexOf(FWD2) < actualObjs.indexOf(FWD4)); |
| 234 | assertTrue(actualObjs.indexOf(FWD4) < actualObjs.indexOf(FWD6)); |
| 235 | |
| 236 | verify(mgr.flowObjectiveStore); |
| 237 | } |
| 238 | |
| 239 | @Test |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 240 | public void forwardTimeout() { |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 241 | final AtomicInteger counter = new AtomicInteger(0); |
| 242 | ForwardingObjective fwdTimeout = buildFwdObjective(S1, NID2).add(new ObjectiveContext() { |
| 243 | @Override |
| 244 | public void onError(Objective objective, ObjectiveError error) { |
| 245 | if (Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) { |
| 246 | counter.incrementAndGet(); |
| 247 | } |
| 248 | } |
| 249 | }); |
| 250 | List<ForwardingObjective> expectFwdObjsTimeout = Lists.newCopyOnWriteArrayList( |
| 251 | Lists.newArrayList(fwdTimeout, FWD1, FWD2)); |
| 252 | |
| 253 | // Reduce timeout so the unit test doesn't have to wait many seconds |
| 254 | InOrderFlowObjectiveManager.objTimeoutMs = TIMEOUT_THRESH; |
| 255 | setUp(); |
| 256 | |
Ray Milkey | 95c9e0f | 2018-05-30 14:16:37 -0700 | [diff] [blame] | 257 | expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(2); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 258 | expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(2); |
| 259 | replay(mgr.flowObjectiveStore); |
| 260 | |
| 261 | // Force this objective to time out |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 262 | offset = InOrderFlowObjectiveManager.objTimeoutMs * 2; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 263 | |
| 264 | expectFwdObjsTimeout.forEach(fwdObj -> mgr.forward(DEV1, fwdObj)); |
| 265 | |
| 266 | // Wait for the pipeline operation to complete |
| 267 | int expectedTime = (bound + offset) * 3; |
| 268 | assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjsTimeout.size(), actualObjs.size())); |
| 269 | |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 270 | assertTrue(counter.get() != 0); |
| 271 | assertTrue(actualObjs.indexOf(fwdTimeout) < actualObjs.indexOf(FWD1)); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 272 | |
| 273 | verify(mgr.flowObjectiveStore); |
| 274 | } |
| 275 | |
| 276 | @Test |
| 277 | public void forwardPending() { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 278 | // Note: current logic will double check if the next obj need to be queued |
| 279 | // it does not check when resubmitting pending next back to the queue |
| 280 | expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(null).times(2); |
| 281 | expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(null).times(2); |
| 282 | expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3); |
| 283 | expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3); |
| 284 | replay(mgr.flowObjectiveStore); |
| 285 | |
| 286 | expectFwdObjs.forEach(fwdObj -> mgr.forward(DEV1, fwdObj)); |
| 287 | |
| 288 | // Trigger the next objectives |
| 289 | mgr.next(DEV1, NEXT1); |
| 290 | mgr.next(DEV1, NEXT2); |
| 291 | |
| 292 | // Wait for the pipeline operation to complete |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 293 | int expectedTime = (bound + offset) * 8; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 294 | assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjs.size() + 2, actualObjs.size())); |
| 295 | |
| 296 | assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(FWD1)); |
| 297 | assertTrue(actualObjs.indexOf(FWD1) < actualObjs.indexOf(FWD3)); |
| 298 | assertTrue(actualObjs.indexOf(FWD3) < actualObjs.indexOf(FWD5)); |
| 299 | assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(FWD2)); |
| 300 | assertTrue(actualObjs.indexOf(FWD2) < actualObjs.indexOf(FWD4)); |
| 301 | assertTrue(actualObjs.indexOf(FWD4) < actualObjs.indexOf(FWD6)); |
| 302 | |
| 303 | verify(mgr.flowObjectiveStore); |
| 304 | } |
| 305 | |
| 306 | @Test |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 307 | public void next() { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 308 | // Note: ADD operation won't query this |
| 309 | expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3); |
| 310 | expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3); |
| 311 | replay(mgr.flowObjectiveStore); |
| 312 | |
| 313 | expectNextObjs.forEach(nextObj -> mgr.next(DEV1, nextObj)); |
| 314 | |
| 315 | // Wait for the pipeline operation to complete |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 316 | int expectedTime = (bound + offset) * 8; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 317 | assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectNextObjs.size(), actualObjs.size())); |
| 318 | |
| 319 | assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(NEXT3)); |
| 320 | assertTrue(actualObjs.indexOf(NEXT3) < actualObjs.indexOf(NEXT5)); |
| 321 | assertTrue(actualObjs.indexOf(NEXT5) < actualObjs.indexOf(NEXT7)); |
| 322 | assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(NEXT4)); |
| 323 | assertTrue(actualObjs.indexOf(NEXT4) < actualObjs.indexOf(NEXT6)); |
| 324 | assertTrue(actualObjs.indexOf(NEXT6) < actualObjs.indexOf(NEXT8)); |
| 325 | |
| 326 | verify(mgr.flowObjectiveStore); |
| 327 | } |
| 328 | |
| 329 | // FIXME We currently do not handle the case when an app sends edit/remove of a next id before add. |
| 330 | // The edit/remove operation will be queued by pendingNext, and the add operation will be |
| 331 | // queued by the ordering queue forever due to the deadlock. This can be improved by making |
| 332 | // pendingForwards, pendingNexts and ordering queue caches. |
| 333 | @Test |
| 334 | @Ignore("Not supported") |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 335 | public void nextPending() { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 336 | // Note: current logic will double check if the next obj need to be queued |
| 337 | // it does not check when resubmitting pending next back to the queue |
| 338 | expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(null).times(6); |
| 339 | expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(null).times(6); |
| 340 | replay(mgr.flowObjectiveStore); |
| 341 | |
| 342 | expectNextObjsPending.forEach(nextObj -> mgr.next(DEV1, nextObj)); |
| 343 | |
| 344 | // Wait for the pipeline operation to complete |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 345 | int expectedTime = (bound + offset) * 8; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 346 | assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectNextObjs.size(), actualObjs.size())); |
| 347 | |
| 348 | assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(NEXT5)); |
| 349 | assertTrue(actualObjs.indexOf(NEXT5) < actualObjs.indexOf(NEXT3)); |
| 350 | assertTrue(actualObjs.indexOf(NEXT3) < actualObjs.indexOf(NEXT7)); |
| 351 | assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(NEXT6)); |
| 352 | assertTrue(actualObjs.indexOf(NEXT6) < actualObjs.indexOf(NEXT4)); |
| 353 | assertTrue(actualObjs.indexOf(NEXT4) < actualObjs.indexOf(NEXT8)); |
| 354 | |
| 355 | verify(mgr.flowObjectiveStore); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Creates filtering objective builder with a serial number encoded in MPLS label. |
| 360 | * The serial number is used to identify same objective that occurs multiple times. |
| 361 | * |
| 362 | * @param portnum Port number |
| 363 | * @param vlanId VLAN Id |
| 364 | * @param mac MAC address |
| 365 | * @param serial Serial number |
| 366 | * @return Filtering objective builder |
| 367 | */ |
| 368 | private static FilteringObjective.Builder buildFilteringObjective(PortNumber portnum, VlanId vlanId, |
| 369 | MacAddress mac, int serial) { |
| 370 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 371 | fob.withKey(Criteria.matchInPort(portnum)) |
| 372 | .addCondition(Criteria.matchEthDst(mac)) |
| 373 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
| 374 | .addCondition(Criteria.matchMplsLabel(MplsLabel.mplsLabel(serial))) |
| 375 | .withPriority(PRIORITY); |
| 376 | |
| 377 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 378 | tBuilder.pushVlan().setVlanId(vlanId); |
| 379 | fob.withMeta(tBuilder.build()); |
| 380 | |
| 381 | fob.permit().fromApp(APP_ID); |
| 382 | return fob; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Creates next objective builder. |
| 387 | * |
| 388 | * @param nextId next ID |
| 389 | * @param vlanId VLAN ID |
| 390 | * @param ports Set of ports that is in the given VLAN ID |
| 391 | * |
| 392 | * @return Next objective builder |
| 393 | */ |
| 394 | private static NextObjective.Builder buildNextObjective(int nextId, VlanId vlanId, Collection<PortNumber> ports) { |
| 395 | TrafficSelector metadata = |
| 396 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
| 397 | |
| 398 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 399 | .builder().withId(nextId) |
| 400 | .withType(NextObjective.Type.BROADCAST).fromApp(APP_ID) |
| 401 | .withMeta(metadata); |
| 402 | |
| 403 | ports.forEach(port -> { |
| 404 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 405 | tBuilder.popVlan(); |
| 406 | tBuilder.setOutput(port); |
| 407 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 408 | }); |
| 409 | |
| 410 | return nextObjBuilder; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Creates forwarding objective builder. |
| 415 | * |
| 416 | * @param selector Traffic selector |
| 417 | * @param nextId next ID |
| 418 | * @return Forwarding objective builder |
| 419 | */ |
| 420 | private static ForwardingObjective.Builder buildFwdObjective(TrafficSelector selector, int nextId) { |
| 421 | return DefaultForwardingObjective.builder() |
| 422 | .makePermanent() |
| 423 | .withSelector(selector) |
| 424 | .nextStep(nextId) |
| 425 | .fromApp(APP_ID) |
| 426 | .withPriority(PRIORITY) |
| 427 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 428 | } |
| 429 | } |