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