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