Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-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; |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 24 | import org.onosproject.net.TributarySlot; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.HashMap; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 28 | import java.util.HashSet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | import java.util.Optional; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 32 | import java.util.Set; |
Sho SHIMIZU | 0e03f59b | 2016-06-08 17:03:48 -0700 | [diff] [blame] | 33 | import java.util.function.Function; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 34 | import java.util.stream.Collectors; |
| 35 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 36 | public class MockResourceService implements ResourceService { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 37 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 38 | private final Map<Resource, ResourceConsumer> assignment = new HashMap<>(); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 39 | |
| 40 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 41 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 42 | assignment.putAll( |
Sho SHIMIZU | 0e03f59b | 2016-06-08 17:03:48 -0700 | [diff] [blame] | 43 | resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 44 | ); |
| 45 | |
| 46 | return resources.stream() |
| 47 | .map(x -> new ResourceAllocation(x, consumer)) |
| 48 | .collect(Collectors.toList()); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean release(List<ResourceAllocation> allocations) { |
| 53 | allocations.forEach(x -> assignment.remove(x.resource())); |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean release(ResourceConsumer consumer) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 60 | List<Resource> resources = assignment.entrySet().stream() |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 61 | .filter(x -> x.getValue().equals(consumer)) |
| 62 | .map(Map.Entry::getKey) |
| 63 | .collect(Collectors.toList()); |
| 64 | List<ResourceAllocation> allocations = resources.stream() |
| 65 | .map(x -> new ResourceAllocation(x, consumer)) |
| 66 | .collect(Collectors.toList()); |
| 67 | |
| 68 | return release(allocations); |
| 69 | } |
| 70 | |
| 71 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 72 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 73 | if (id instanceof ContinuousResourceId) { |
| 74 | return ImmutableList.of(); |
| 75 | } |
| 76 | DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource(); |
| 77 | return Optional.ofNullable(assignment.get(discrete)) |
| 78 | .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x))) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 79 | .orElse(ImmutableList.of()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 83 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 84 | return assignment.entrySet().stream() |
| 85 | .filter(x -> x.getKey().parent().isPresent()) |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 86 | .filter(x -> x.getKey().parent().get().id().equals(parent)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 87 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 88 | .collect(Collectors.toList()); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
| 93 | return assignment.entrySet().stream() |
| 94 | .filter(x -> x.getValue().equals(consumer)) |
| 95 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 96 | .collect(Collectors.toList()); |
| 97 | } |
| 98 | |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * It adds a number of VLAN ids in order to test the random behavior. |
| 102 | * |
| 103 | * @param parent the parent resource |
| 104 | * @return a set of VLAN ids |
| 105 | */ |
| 106 | private Collection<Resource> addVlanIds(DiscreteResourceId parent) { |
| 107 | Collection<Resource> resources = new HashSet<>(); |
| 108 | for (int i = VlanId.NO_VID + 1; i < VlanId.MAX_VLAN; i++) { |
| 109 | resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) i))); |
| 110 | } |
| 111 | return resources; |
| 112 | } |
| 113 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 114 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 115 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
| 116 | Collection<Resource> resources = new HashSet<>(); |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame] | 117 | resources.addAll(addVlanIds(parent)); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 118 | resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10))); |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 119 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(1))); |
| 120 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(2))); |
| 121 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(3))); |
| 122 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(4))); |
| 123 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(5))); |
| 124 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(6))); |
| 125 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(7))); |
| 126 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(8))); |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 127 | return ImmutableSet.copyOf(resources); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 131 | public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) { |
| 132 | return getAvailableResources(parent).stream() |
| 133 | .filter(x -> x.isTypeOf(cls)) |
| 134 | .collect(Collectors.toSet()); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) { |
| 139 | return getAvailableResources(parent).stream() |
| 140 | .filter(x -> x.isTypeOf(cls)) |
| 141 | .flatMap(x -> Tools.stream(x.valueAs(cls))) |
| 142 | .collect(Collectors.toSet()); |
| 143 | } |
| 144 | |
| 145 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 146 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 147 | return getAvailableResources(parent); |
| 148 | } |
| 149 | |
| 150 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 151 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 152 | return true; |
| 153 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public void addListener(ResourceListener listener) {} |
| 157 | |
| 158 | @Override |
| 159 | public void removeListener(ResourceListener listener) {} |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 160 | } |