Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 19 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.core.DefaultGroupId; |
| 21 | import org.onosproject.core.GroupId; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.ElementId; |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 24 | import org.onosproject.net.HostId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.Link; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 26 | import org.onosproject.net.NetTestTools; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 27 | import org.onosproject.net.NetworkResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Path; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 29 | import org.onosproject.net.device.DeviceServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.FlowId; |
| 31 | import org.onosproject.net.flow.FlowRule; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.flow.criteria.Criterion; |
| 36 | import org.onosproject.net.flow.criteria.Criterion.Type; |
| 37 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.instructions.Instructions; |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 40 | import org.onosproject.net.resource.DiscreteResourceId; |
| 41 | import org.onosproject.net.resource.Resource; |
| 42 | import org.onosproject.net.resource.ResourceAllocation; |
| 43 | import org.onosproject.net.resource.ResourceConsumer; |
| 44 | import org.onosproject.net.resource.ResourceId; |
| 45 | import org.onosproject.net.resource.ResourceListener; |
| 46 | import org.onosproject.net.resource.ResourceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.topology.DefaultTopologyEdge; |
| 48 | import org.onosproject.net.topology.DefaultTopologyVertex; |
| 49 | import org.onosproject.net.topology.LinkWeight; |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 50 | import org.onosproject.net.topology.PathServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.topology.TopologyVertex; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 52 | import org.onosproject.store.Timestamp; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 53 | |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 54 | import java.util.Arrays; |
| 55 | import java.util.Collection; |
| 56 | import java.util.Collections; |
| 57 | import java.util.HashSet; |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 58 | import java.util.List; |
| 59 | import java.util.Objects; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 60 | import java.util.Optional; |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 61 | import java.util.Set; |
| 62 | import java.util.concurrent.atomic.AtomicLong; |
| 63 | |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 64 | import static org.onosproject.net.NetTestTools.*; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 65 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Common mocks used by the intent framework tests. |
| 68 | */ |
| 69 | public class IntentTestsMocks { |
| 70 | /** |
| 71 | * Mock traffic selector class used for satisfying API requirements. |
| 72 | */ |
| 73 | public static class MockSelector implements TrafficSelector { |
| 74 | @Override |
| 75 | public Set<Criterion> criteria() { |
| 76 | return new HashSet<>(); |
| 77 | } |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | public Criterion getCriterion(Type type) { |
| 81 | return null; |
| 82 | } |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Mock traffic treatment class used for satisfying API requirements. |
| 87 | */ |
| 88 | public static class MockTreatment implements TrafficTreatment { |
| 89 | @Override |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 90 | public List<Instruction> deferred() { |
Jonathan Hart | 4a0ba56 | 2015-03-23 17:23:33 -0700 | [diff] [blame] | 91 | return Collections.emptyList(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public List<Instruction> immediate() { |
Jonathan Hart | 4a0ba56 | 2015-03-23 17:23:33 -0700 | [diff] [blame] | 96 | return Collections.emptyList(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 100 | public List<Instruction> allInstructions() { |
Jonathan Hart | 4a0ba56 | 2015-03-23 17:23:33 -0700 | [diff] [blame] | 101 | return Collections.emptyList(); |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 105 | public Instructions.TableTypeTransition tableTransition() { |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
Jonathan Hart | 4a0ba56 | 2015-03-23 17:23:33 -0700 | [diff] [blame] | 110 | public boolean clearedDeferred() { |
| 111 | return false; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 112 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 113 | |
| 114 | @Override |
| 115 | public MetadataInstruction writeMetadata() { |
| 116 | return null; |
| 117 | } |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public Instructions.MeterInstruction metered() { |
| 121 | return null; |
| 122 | } |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Mock path service for creating paths within the test. |
| 127 | */ |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 128 | public static class MockPathService extends PathServiceAdapter { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 129 | |
| 130 | final String[] pathHops; |
| 131 | final String[] reversePathHops; |
| 132 | |
| 133 | /** |
| 134 | * Constructor that provides a set of hops to mock. |
| 135 | * |
| 136 | * @param pathHops path hops to mock |
| 137 | */ |
| 138 | public MockPathService(String[] pathHops) { |
| 139 | this.pathHops = pathHops; |
| 140 | String[] reversed = pathHops.clone(); |
| 141 | Collections.reverse(Arrays.asList(reversed)); |
| 142 | reversePathHops = reversed; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
| 147 | Set<Path> result = new HashSet<>(); |
| 148 | |
| 149 | String[] allHops = new String[pathHops.length]; |
| 150 | |
| 151 | if (src.toString().endsWith(pathHops[0])) { |
| 152 | System.arraycopy(pathHops, 0, allHops, 0, pathHops.length); |
| 153 | } else { |
| 154 | System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length); |
| 155 | } |
| 156 | |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 157 | result.add(createPath(src instanceof HostId, dst instanceof HostId, allHops)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 158 | return result; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 163 | final Set<Path> paths = getPaths(src, dst); |
| 164 | |
| 165 | for (Path path : paths) { |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 166 | final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null; |
| 167 | final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null; |
| 168 | if (srcDevice != null && dstDevice != null) { |
| 169 | final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice); |
| 170 | final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice); |
| 171 | final Link link = link(src.toString(), 1, dst.toString(), 1); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 172 | |
Thomas Vachuska | dc91b55 | 2016-03-29 14:02:47 -0700 | [diff] [blame] | 173 | final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link)); |
| 174 | if (weightValue < 0) { |
| 175 | return new HashSet<>(); |
| 176 | } |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | return paths; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 182 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Mock path service for creating paths within the test. |
| 185 | * |
| 186 | */ |
| 187 | public static class Mp2MpMockPathService |
| 188 | extends PathServiceAdapter { |
| 189 | |
| 190 | final String[] pathHops; |
| 191 | final String[] reversePathHops; |
| 192 | |
| 193 | /** |
| 194 | * Constructor that provides a set of hops to mock. |
| 195 | * |
| 196 | * @param pathHops path hops to mock |
| 197 | */ |
| 198 | public Mp2MpMockPathService(String[] pathHops) { |
| 199 | this.pathHops = pathHops; |
| 200 | String[] reversed = pathHops.clone(); |
| 201 | Collections.reverse(Arrays.asList(reversed)); |
| 202 | reversePathHops = reversed; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
| 207 | Set<Path> result = new HashSet<>(); |
| 208 | |
| 209 | String[] allHops = new String[pathHops.length + 2]; |
| 210 | allHops[0] = src.toString(); |
| 211 | allHops[allHops.length - 1] = dst.toString(); |
| 212 | |
| 213 | if (pathHops.length != 0) { |
| 214 | System.arraycopy(pathHops, 0, allHops, 1, pathHops.length); |
| 215 | } |
| 216 | |
| 217 | result.add(createPath(allHops)); |
| 218 | |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
| 224 | final Set<Path> paths = getPaths(src, dst); |
| 225 | |
| 226 | for (Path path : paths) { |
| 227 | final DeviceId srcDevice = path.src().elementId() instanceof DeviceId ? path.src().deviceId() : null; |
| 228 | final DeviceId dstDevice = path.dst().elementId() instanceof DeviceId ? path.dst().deviceId() : null; |
| 229 | if (srcDevice != null && dstDevice != null) { |
| 230 | final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice); |
| 231 | final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice); |
| 232 | final Link link = link(src.toString(), 1, dst.toString(), 1); |
| 233 | |
| 234 | final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link)); |
| 235 | if (weightValue < 0) { |
| 236 | return new HashSet<>(); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | return paths; |
| 241 | } |
| 242 | } |
| 243 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 244 | public static final class MockResourceService implements ResourceService { |
| 245 | |
| 246 | private final double bandwidth; |
| 247 | |
| 248 | public static ResourceService makeBandwidthResourceService(double bandwidth) { |
| 249 | return new MockResourceService(bandwidth); |
| 250 | } |
| 251 | |
| 252 | private MockResourceService(double bandwidth) { |
| 253 | this.bandwidth = bandwidth; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 254 | } |
| 255 | |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 256 | @Override |
Sho SHIMIZU | ef835c9 | 2016-08-08 13:51:17 -0700 | [diff] [blame] | 257 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 258 | return null; |
| 259 | } |
| 260 | |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 261 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 262 | public boolean release(List<ResourceAllocation> allocations) { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public boolean release(ResourceConsumer consumer) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 273 | return null; |
| 274 | } |
| 275 | |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 276 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 277 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 278 | return null; |
| 279 | } |
| 280 | |
| 281 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 282 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 283 | return null; |
| 284 | } |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 285 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 286 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 287 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
| 288 | return null; |
| 289 | } |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 290 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 291 | @Override |
| 292 | public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) { |
| 293 | return null; |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) { |
| 298 | return null; |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
| 303 | return null; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public boolean isAvailable(Resource resource) { |
| 308 | if (!resource.isTypeOf(Bandwidth.class)) { |
| 309 | return false; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 312 | Optional<Double> value = resource.valueAs(Double.class); |
| 313 | return value.filter(requested -> requested <= bandwidth).isPresent(); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 317 | public void addListener(ResourceListener listener) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 321 | public void removeListener(ResourceListener listener) { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 322 | } |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 325 | private static final IntentTestsMocks.MockSelector SELECTOR = |
| 326 | new IntentTestsMocks.MockSelector(); |
| 327 | private static final IntentTestsMocks.MockTreatment TREATMENT = |
| 328 | new IntentTestsMocks.MockTreatment(); |
| 329 | |
| 330 | public static class MockFlowRule implements FlowRule { |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 331 | static int nextId = 0; |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 332 | |
| 333 | int priority; |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 334 | int tableId; |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 335 | long timestamp; |
| 336 | int id; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 337 | FlowRuleExtPayLoad payLoad; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 338 | |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 339 | public MockFlowRule(int priority) { |
| 340 | this.priority = priority; |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 341 | this.tableId = 0; |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 342 | this.timestamp = System.currentTimeMillis(); |
| 343 | this.id = nextId++; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 344 | this.payLoad = null; |
| 345 | } |
| 346 | |
| 347 | public MockFlowRule(int priority, FlowRuleExtPayLoad payLoad) { |
| 348 | this.priority = priority; |
| 349 | this.timestamp = System.currentTimeMillis(); |
| 350 | this.id = nextId++; |
| 351 | this.payLoad = payLoad; |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public FlowId id() { |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 356 | return FlowId.valueOf(id); |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public short appId() { |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | @Override |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 365 | public GroupId groupId() { |
| 366 | return new DefaultGroupId(0); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Override |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 370 | public int priority() { |
| 371 | return priority; |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public DeviceId deviceId() { |
| 376 | return did("1"); |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public TrafficSelector selector() { |
| 381 | return SELECTOR; |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public TrafficTreatment treatment() { |
| 386 | return TREATMENT; |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public int timeout() { |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | @Override |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 395 | public int hardTimeout() { |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public FlowRemoveReason reason() { |
| 401 | return FlowRemoveReason.NO_REASON; |
| 402 | } |
| 403 | |
| 404 | @Override |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 405 | public boolean isPermanent() { |
| 406 | return false; |
| 407 | } |
| 408 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 409 | @Override |
| 410 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 411 | return priority; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 412 | } |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 413 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 414 | @Override |
| 415 | public boolean equals(Object obj) { |
| 416 | if (this == obj) { |
| 417 | return true; |
| 418 | } |
| 419 | if (obj == null || getClass() != obj.getClass()) { |
| 420 | return false; |
| 421 | } |
| 422 | final MockFlowRule other = (MockFlowRule) obj; |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 423 | return Objects.equals(this.timestamp, other.timestamp) && |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 424 | this.id == other.id; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 425 | } |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 426 | |
| 427 | @Override |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 428 | public boolean exactMatch(FlowRule rule) { |
| 429 | return this.equals(rule); |
| 430 | } |
| 431 | |
| 432 | @Override |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 433 | public int tableId() { |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 434 | return tableId; |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 435 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 436 | |
| 437 | @Override |
| 438 | public FlowRuleExtPayLoad payLoad() { |
| 439 | return payLoad; |
| 440 | } |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 443 | public static class MockIntent extends Intent { |
| 444 | private static AtomicLong counter = new AtomicLong(0); |
| 445 | |
| 446 | private final Long number; |
| 447 | |
| 448 | public MockIntent(Long number) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 449 | super(NetTestTools.APP_ID, null, Collections.emptyList(), |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 450 | Intent.DEFAULT_INTENT_PRIORITY); |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 451 | this.number = number; |
| 452 | } |
| 453 | |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 454 | public MockIntent(Long number, Collection<NetworkResource> resources) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 455 | super(NetTestTools.APP_ID, null, resources, Intent.DEFAULT_INTENT_PRIORITY); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 456 | this.number = number; |
| 457 | } |
| 458 | |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 459 | public Long number() { |
| 460 | return number; |
| 461 | } |
| 462 | |
| 463 | public static Long nextId() { |
| 464 | return counter.getAndIncrement(); |
| 465 | } |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 466 | |
| 467 | @Override |
| 468 | public String toString() { |
| 469 | return MoreObjects.toStringHelper(getClass()) |
| 470 | .add("id", id()) |
| 471 | .add("appId", appId()) |
| 472 | .toString(); |
| 473 | } |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | public static class MockTimestamp implements Timestamp { |
| 477 | final int value; |
| 478 | |
| 479 | public MockTimestamp(int value) { |
| 480 | this.value = value; |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public int compareTo(Timestamp o) { |
| 485 | if (!(o instanceof MockTimestamp)) { |
| 486 | return -1; |
| 487 | } |
| 488 | MockTimestamp that = (MockTimestamp) o; |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 489 | return this.value - that.value; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 493 | /** |
| 494 | * Mocks the device service so that a device appears available in the test. |
| 495 | */ |
| 496 | public static class MockDeviceService extends DeviceServiceAdapter { |
| 497 | @Override |
| 498 | public boolean isAvailable(DeviceId deviceId) { |
| 499 | return true; |
| 500 | } |
| 501 | } |
| 502 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 503 | } |