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