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