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