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