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