Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import static org.onosproject.net.NetTestTools.createPath; |
| 19 | import static org.onosproject.net.NetTestTools.did; |
| 20 | import static org.onosproject.net.NetTestTools.link; |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 21 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
| 23 | import java.util.Arrays; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 24 | import java.util.Collection; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 25 | import java.util.Collections; |
| 26 | import java.util.HashSet; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 27 | import java.util.LinkedList; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 28 | import java.util.List; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 29 | import java.util.Objects; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 30 | import java.util.Set; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 31 | import java.util.concurrent.atomic.AtomicLong; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 32 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.core.DefaultGroupId; |
| 34 | import org.onosproject.core.GroupId; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.ElementId; |
| 37 | import org.onosproject.net.Link; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 38 | import org.onosproject.net.NetTestTools; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 39 | import org.onosproject.net.NetworkResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.Path; |
| 41 | import org.onosproject.net.flow.FlowId; |
| 42 | import org.onosproject.net.flow.FlowRule; |
| 43 | import org.onosproject.net.flow.TrafficSelector; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.flow.criteria.Criterion; |
| 46 | import org.onosproject.net.flow.criteria.Criterion.Type; |
| 47 | import org.onosproject.net.flow.instructions.Instruction; |
Ray Milkey | cf590df | 2015-02-23 17:43:24 -0800 | [diff] [blame] | 48 | import org.onosproject.net.resource.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.resource.BandwidthResourceRequest; |
Ray Milkey | b1b66d3 | 2015-03-02 10:27:11 -0800 | [diff] [blame] | 50 | import org.onosproject.net.resource.Lambda; |
| 51 | import org.onosproject.net.resource.LambdaResourceAllocation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.resource.LambdaResourceRequest; |
| 53 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 54 | import org.onosproject.net.resource.LinkResourceListener; |
| 55 | import org.onosproject.net.resource.LinkResourceRequest; |
| 56 | import org.onosproject.net.resource.LinkResourceService; |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 57 | import org.onosproject.net.resource.MplsLabel; |
| 58 | import org.onosproject.net.resource.MplsLabelResourceAllocation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.resource.ResourceAllocation; |
| 60 | import org.onosproject.net.resource.ResourceRequest; |
| 61 | import org.onosproject.net.resource.ResourceType; |
| 62 | import org.onosproject.net.topology.DefaultTopologyEdge; |
| 63 | import org.onosproject.net.topology.DefaultTopologyVertex; |
| 64 | import org.onosproject.net.topology.LinkWeight; |
| 65 | import org.onosproject.net.topology.PathService; |
| 66 | import org.onosproject.net.topology.TopologyVertex; |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 67 | import org.onosproject.store.Timestamp; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 68 | |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 69 | import com.google.common.base.MoreObjects; |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 70 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 71 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Common mocks used by the intent framework tests. |
| 74 | */ |
| 75 | public class IntentTestsMocks { |
| 76 | /** |
| 77 | * Mock traffic selector class used for satisfying API requirements. |
| 78 | */ |
| 79 | public static class MockSelector implements TrafficSelector { |
| 80 | @Override |
| 81 | public Set<Criterion> criteria() { |
| 82 | return new HashSet<>(); |
| 83 | } |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | public Criterion getCriterion(Type type) { |
| 87 | return null; |
| 88 | } |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Mock traffic treatment class used for satisfying API requirements. |
| 93 | */ |
| 94 | public static class MockTreatment implements TrafficTreatment { |
| 95 | @Override |
| 96 | public List<Instruction> instructions() { |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 97 | return new ArrayList<>(); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Mock path service for creating paths within the test. |
| 103 | */ |
| 104 | public static class MockPathService implements PathService { |
| 105 | |
| 106 | final String[] pathHops; |
| 107 | final String[] reversePathHops; |
| 108 | |
| 109 | /** |
| 110 | * Constructor that provides a set of hops to mock. |
| 111 | * |
| 112 | * @param pathHops path hops to mock |
| 113 | */ |
| 114 | public MockPathService(String[] pathHops) { |
| 115 | this.pathHops = pathHops; |
| 116 | String[] reversed = pathHops.clone(); |
| 117 | Collections.reverse(Arrays.asList(reversed)); |
| 118 | reversePathHops = reversed; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
| 123 | Set<Path> result = new HashSet<>(); |
| 124 | |
| 125 | String[] allHops = new String[pathHops.length]; |
| 126 | |
| 127 | if (src.toString().endsWith(pathHops[0])) { |
| 128 | System.arraycopy(pathHops, 0, allHops, 0, pathHops.length); |
| 129 | } else { |
| 130 | System.arraycopy(reversePathHops, 0, allHops, 0, pathHops.length); |
| 131 | } |
| 132 | |
| 133 | result.add(createPath(allHops)); |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 139 | final Set<Path> paths = getPaths(src, dst); |
| 140 | |
| 141 | for (Path path : paths) { |
| 142 | final DeviceId srcDevice = path.src().deviceId(); |
| 143 | final DeviceId dstDevice = path.dst().deviceId(); |
| 144 | final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice); |
| 145 | final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice); |
| 146 | final Link link = link(src.toString(), 1, dst.toString(), 1); |
| 147 | |
| 148 | final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link)); |
| 149 | if (weightValue < 0) { |
| 150 | return new HashSet<>(); |
| 151 | } |
| 152 | } |
| 153 | return paths; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 156 | |
| 157 | public static class MockLinkResourceAllocations implements LinkResourceAllocations { |
| 158 | @Override |
| 159 | public Set<ResourceAllocation> getResourceAllocation(Link link) { |
Ray Milkey | b1b66d3 | 2015-03-02 10:27:11 -0800 | [diff] [blame] | 160 | return ImmutableSet.of( |
| 161 | new LambdaResourceAllocation(Lambda.valueOf(77)), |
| 162 | new MplsLabelResourceAllocation(MplsLabel.valueOf(10))); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public IntentId intendId() { |
| 167 | return null; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Collection<Link> links() { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Set<ResourceRequest> resources() { |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public ResourceType type() { |
| 182 | return null; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | public static class MockedAllocationFailure extends RuntimeException { } |
| 187 | |
| 188 | public static class MockResourceService implements LinkResourceService { |
| 189 | |
| 190 | double availableBandwidth = -1.0; |
| 191 | int availableLambda = -1; |
| 192 | |
| 193 | /** |
| 194 | * Allocates a resource service that will allow bandwidth allocations |
| 195 | * up to a limit. |
| 196 | * |
| 197 | * @param bandwidth available bandwidth limit |
| 198 | * @return resource manager for bandwidth requests |
| 199 | */ |
| 200 | public static MockResourceService makeBandwidthResourceService(double bandwidth) { |
| 201 | final MockResourceService result = new MockResourceService(); |
| 202 | result.availableBandwidth = bandwidth; |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Allocates a resource service that will allow lambda allocations. |
| 208 | * |
| 209 | * @param lambda Lambda to return for allocation requests. Currently unused |
| 210 | * @return resource manager for lambda requests |
| 211 | */ |
| 212 | public static MockResourceService makeLambdaResourceService(int lambda) { |
| 213 | final MockResourceService result = new MockResourceService(); |
| 214 | result.availableLambda = lambda; |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | public void setAvailableBandwidth(double availableBandwidth) { |
| 219 | this.availableBandwidth = availableBandwidth; |
| 220 | } |
| 221 | |
| 222 | public void setAvailableLambda(int availableLambda) { |
| 223 | this.availableLambda = availableLambda; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | @Override |
| 228 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
| 229 | int lambda = -1; |
| 230 | double bandwidth = -1.0; |
| 231 | |
| 232 | for (ResourceRequest resourceRequest : req.resources()) { |
| 233 | if (resourceRequest.type() == ResourceType.BANDWIDTH) { |
| 234 | final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest; |
| 235 | bandwidth = brr.bandwidth().toDouble(); |
| 236 | } else if (resourceRequest.type() == ResourceType.LAMBDA) { |
| 237 | lambda = 1; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (availableBandwidth < bandwidth) { |
| 242 | throw new MockedAllocationFailure(); |
| 243 | } |
| 244 | if (lambda > 0 && availableLambda == 0) { |
| 245 | throw new MockedAllocationFailure(); |
| 246 | } |
| 247 | |
| 248 | return new IntentTestsMocks.MockLinkResourceAllocations(); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void releaseResources(LinkResourceAllocations allocations) { |
| 253 | // Mock |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
| 258 | LinkResourceAllocations oldAllocations) { |
| 259 | return null; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Iterable<LinkResourceAllocations> getAllocations() { |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 264 | return ImmutableSet.of( |
| 265 | new IntentTestsMocks.MockLinkResourceAllocations()); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 270 | return ImmutableSet.of( |
| 271 | new IntentTestsMocks.MockLinkResourceAllocations()); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Ray Milkey | bb32048 | 2015-02-25 15:16:46 -0800 | [diff] [blame] | 276 | return new IntentTestsMocks.MockLinkResourceAllocations(); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
| 281 | final List<ResourceRequest> result = new LinkedList<>(); |
| 282 | if (availableBandwidth > 0.0) { |
Ray Milkey | cf590df | 2015-02-23 17:43:24 -0800 | [diff] [blame] | 283 | result.add(new BandwidthResourceRequest( |
| 284 | Bandwidth.bps(availableBandwidth))); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 285 | } |
| 286 | if (availableLambda > 0) { |
| 287 | result.add(new LambdaResourceRequest()); |
| 288 | } |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | @Override |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 293 | public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 294 | return null; |
| 295 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 296 | |
| 297 | @Override |
| 298 | public void addListener(LinkResourceListener listener) { |
| 299 | |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void removeListener(LinkResourceListener listener) { |
| 304 | |
| 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 { |
| 314 | |
| 315 | int priority; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 316 | Type type; |
| 317 | |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 318 | public MockFlowRule(int priority) { |
| 319 | this.priority = priority; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 320 | this.type = Type.DEFAULT; |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public FlowId id() { |
| 325 | return FlowId.valueOf(1); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public short appId() { |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | @Override |
Sho SHIMIZU | 75a5bd9 | 2014-11-25 11:22:56 -0800 | [diff] [blame] | 334 | public GroupId groupId() { |
| 335 | return new DefaultGroupId(0); |
alshabib | 28204e5 | 2014-11-12 18:29:45 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 339 | public int priority() { |
| 340 | return priority; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public DeviceId deviceId() { |
| 345 | return did("1"); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public TrafficSelector selector() { |
| 350 | return SELECTOR; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public TrafficTreatment treatment() { |
| 355 | return TREATMENT; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public int timeout() { |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public boolean isPermanent() { |
| 365 | return false; |
| 366 | } |
| 367 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 368 | @Override |
| 369 | public int hashCode() { |
| 370 | return Objects.hash(priority); |
| 371 | } |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 372 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 373 | @Override |
| 374 | public boolean equals(Object obj) { |
| 375 | if (this == obj) { |
| 376 | return true; |
| 377 | } |
| 378 | if (obj == null || getClass() != obj.getClass()) { |
| 379 | return false; |
| 380 | } |
| 381 | final MockFlowRule other = (MockFlowRule) obj; |
| 382 | return Objects.equals(this.priority, other.priority); |
| 383 | } |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 384 | |
| 385 | @Override |
| 386 | public Type type() { |
| 387 | return type; |
| 388 | } |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 391 | public static class MockIntent extends Intent { |
| 392 | private static AtomicLong counter = new AtomicLong(0); |
| 393 | |
| 394 | private final Long number; |
| 395 | |
| 396 | public MockIntent(Long number) { |
| 397 | super(NetTestTools.APP_ID, Collections.emptyList()); |
| 398 | this.number = number; |
| 399 | } |
| 400 | |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 401 | public MockIntent(Long number, Collection<NetworkResource> resources) { |
| 402 | super(NetTestTools.APP_ID, resources); |
| 403 | this.number = number; |
| 404 | } |
| 405 | |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 406 | public Long number() { |
| 407 | return number; |
| 408 | } |
| 409 | |
| 410 | public static Long nextId() { |
| 411 | return counter.getAndIncrement(); |
| 412 | } |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 413 | |
| 414 | @Override |
| 415 | public String toString() { |
| 416 | return MoreObjects.toStringHelper(getClass()) |
| 417 | .add("id", id()) |
| 418 | .add("appId", appId()) |
| 419 | .toString(); |
| 420 | } |
Ray Milkey | 5a7787a | 2015-02-23 11:44:30 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | public static class MockTimestamp implements Timestamp { |
| 424 | final int value; |
| 425 | |
| 426 | public MockTimestamp(int value) { |
| 427 | this.value = value; |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public int compareTo(Timestamp o) { |
| 432 | if (!(o instanceof MockTimestamp)) { |
| 433 | return -1; |
| 434 | } |
| 435 | MockTimestamp that = (MockTimestamp) o; |
| 436 | return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1)); |
| 437 | } |
| 438 | } |
| 439 | |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 440 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 441 | } |