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