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