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