Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 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 | */ |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 20 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MplsLabel; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 23 | import org.onlab.util.Bandwidth; |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 24 | import org.onlab.util.Tools; |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 25 | import org.onosproject.net.TributarySlot; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.HashMap; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 29 | import java.util.HashSet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Optional; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 33 | import java.util.Set; |
Sho SHIMIZU | 0e03f59b | 2016-06-08 17:03:48 -0700 | [diff] [blame] | 34 | import java.util.function.Function; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 35 | import java.util.stream.Collectors; |
| 36 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 37 | public class MockResourceService implements ResourceService { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 38 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 39 | private double bandwidth = 1000.0; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 40 | private final Map<Resource, ResourceConsumer> assignment = new HashMap<>(); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 41 | public Set<Short> availableVlanLabels = new HashSet<>(); |
| 42 | public Set<Integer> availableMplsLabels = new HashSet<>(); |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 43 | public boolean filterAssignment = false; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 44 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 45 | public MockResourceService(){} |
| 46 | |
| 47 | // To express a custom bandwidth available (in bps) |
| 48 | public static ResourceService makeCustomBandwidthResourceService(double bandwidth) { |
| 49 | return new MockResourceService(bandwidth); |
| 50 | } |
| 51 | |
| 52 | private MockResourceService(double bandwidth) { |
| 53 | this.bandwidth = bandwidth; |
| 54 | } |
| 55 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 56 | @Override |
Sho SHIMIZU | ef835c9 | 2016-08-08 13:51:17 -0700 | [diff] [blame] | 57 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 58 | assignment.putAll( |
Sho SHIMIZU | 0e03f59b | 2016-06-08 17:03:48 -0700 | [diff] [blame] | 59 | resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 60 | ); |
| 61 | |
| 62 | return resources.stream() |
| 63 | .map(x -> new ResourceAllocation(x, consumer)) |
| 64 | .collect(Collectors.toList()); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean release(List<ResourceAllocation> allocations) { |
| 69 | allocations.forEach(x -> assignment.remove(x.resource())); |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean release(ResourceConsumer consumer) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 76 | List<Resource> resources = assignment.entrySet().stream() |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 77 | .filter(x -> x.getValue().equals(consumer)) |
| 78 | .map(Map.Entry::getKey) |
| 79 | .collect(Collectors.toList()); |
| 80 | List<ResourceAllocation> allocations = resources.stream() |
| 81 | .map(x -> new ResourceAllocation(x, consumer)) |
| 82 | .collect(Collectors.toList()); |
| 83 | |
| 84 | return release(allocations); |
| 85 | } |
| 86 | |
| 87 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 88 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 89 | if (id instanceof ContinuousResourceId) { |
| 90 | return ImmutableList.of(); |
| 91 | } |
| 92 | DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource(); |
| 93 | return Optional.ofNullable(assignment.get(discrete)) |
| 94 | .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x))) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 95 | .orElse(ImmutableList.of()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 99 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 100 | return assignment.entrySet().stream() |
| 101 | .filter(x -> x.getKey().parent().isPresent()) |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 102 | .filter(x -> x.getKey().parent().get().id().equals(parent)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 103 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 104 | .collect(Collectors.toList()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
| 109 | return assignment.entrySet().stream() |
| 110 | .filter(x -> x.getValue().equals(consumer)) |
| 111 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 112 | .collect(Collectors.toList()); |
| 113 | } |
| 114 | |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 115 | |
| 116 | /** |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 117 | * Binds VLAN Ids to a parent resource, given a parent resource. |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 118 | * |
| 119 | * @param parent the parent resource |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 120 | * @return the VLAN Ids allocated |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 121 | */ |
| 122 | private Collection<Resource> addVlanIds(DiscreteResourceId parent) { |
| 123 | Collection<Resource> resources = new HashSet<>(); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 124 | if (!this.availableVlanLabels.isEmpty()) { |
| 125 | this.availableVlanLabels.forEach(label -> { |
| 126 | if (label > VlanId.NO_VID && label < VlanId.MAX_VLAN) { |
| 127 | resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId(label))); |
| 128 | } |
| 129 | }); |
| 130 | } else { |
| 131 | for (int i = VlanId.NO_VID + 1; i < 1000; i++) { |
| 132 | resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) i))); |
| 133 | } |
| 134 | } |
| 135 | return resources; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Binds MPLS labels to a parent resource, given a parent resource. |
| 140 | * |
| 141 | * @param parent the parent resource |
| 142 | * @return the MPLS labels allocated |
| 143 | */ |
| 144 | private Collection<Resource> addMplsLabels(DiscreteResourceId parent) { |
| 145 | Collection<Resource> resources = new HashSet<>(); |
| 146 | if (!this.availableMplsLabels.isEmpty()) { |
| 147 | this.availableMplsLabels.forEach(label -> { |
| 148 | if (label < MplsLabel.MAX_MPLS) { |
| 149 | resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(label))); |
| 150 | } |
| 151 | }); |
| 152 | } else { |
| 153 | for (int i = 1; i < 1000; i++) { |
| 154 | resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(i))); |
| 155 | } |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 156 | } |
| 157 | return resources; |
| 158 | } |
| 159 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 160 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 161 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
| 162 | Collection<Resource> resources = new HashSet<>(); |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 163 | resources.addAll(addVlanIds(parent)); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 164 | resources.addAll(addMplsLabels(parent)); |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 165 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(1))); |
| 166 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(2))); |
| 167 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(3))); |
| 168 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(4))); |
| 169 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(5))); |
| 170 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(6))); |
| 171 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(7))); |
| 172 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(8))); |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 173 | return filterAssignment ? ImmutableSet.copyOf( |
| 174 | resources.stream().filter( |
| 175 | resource -> assignment.get(resource) == null |
| 176 | ).collect(Collectors.toSet()) |
| 177 | ) : ImmutableSet.copyOf(resources); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 181 | public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) { |
| 182 | return getAvailableResources(parent).stream() |
| 183 | .filter(x -> x.isTypeOf(cls)) |
| 184 | .collect(Collectors.toSet()); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) { |
| 189 | return getAvailableResources(parent).stream() |
| 190 | .filter(x -> x.isTypeOf(cls)) |
| 191 | .flatMap(x -> Tools.stream(x.valueAs(cls))) |
| 192 | .collect(Collectors.toSet()); |
| 193 | } |
| 194 | |
| 195 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 196 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 197 | return getAvailableResources(parent); |
| 198 | } |
| 199 | |
| 200 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 201 | public boolean isAvailable(Resource resource) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 202 | if (resource.isTypeOf(Bandwidth.class)) { |
| 203 | // If there's is enough bandwidth available return true; false otherwise |
| 204 | Optional<Double> value = resource.valueAs(Double.class); |
| 205 | return value.filter(requested -> requested <= bandwidth).isPresent(); |
| 206 | } |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 207 | return true; |
| 208 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 209 | |
| 210 | @Override |
| 211 | public void addListener(ResourceListener listener) {} |
| 212 | |
| 213 | @Override |
| 214 | public void removeListener(ResourceListener listener) {} |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 215 | } |