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