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