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