blob: 3a8b4e4ea36bebcdfd5d495e14d33cae9222a095 [file] [log] [blame]
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07003 *
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 HIGUCHId95d5902016-06-27 00:18:45 -070016package org.onosproject.net.resource;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070017
18import com.google.common.collect.ImmutableList;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080019import com.google.common.collect.ImmutableSet;
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +020020
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070021import org.onlab.packet.MplsLabel;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010022import org.onlab.packet.VlanId;
Luca Pretede10c782017-01-05 17:23:08 -080023import org.onlab.util.Bandwidth;
Sho SHIMIZU7332fe42016-02-15 14:58:33 -080024import org.onlab.util.Tools;
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +020025import org.onosproject.net.TributarySlot;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070026
27import java.util.Collection;
28import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010029import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070030import java.util.List;
31import java.util.Map;
32import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080033import java.util.Set;
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070034import java.util.function.Function;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070035import java.util.stream.Collectors;
36
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070037public class MockResourceService implements ResourceService {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070038
Luca Pretede10c782017-01-05 17:23:08 -080039 private double bandwidth = 1000.0;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080040 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Pier Ventref8543d82016-09-28 19:49:33 -070041 public Set<Short> availableVlanLabels = new HashSet<>();
42 public Set<Integer> availableMplsLabels = new HashSet<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070043
Luca Pretede10c782017-01-05 17:23:08 -080044 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 SHIMIZUf26e6922015-10-29 16:19:20 -070055 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -070056 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070057 assignment.putAll(
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070058 resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070059 );
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080075 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070076 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -080087 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 SHIMIZU6c9e33a2016-01-07 18:45:27 -080094 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070095 }
96
97 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080098 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070099 return assignment.entrySet().stream()
100 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800101 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700102 .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 Ventre51313bd2016-06-02 10:50:30 +0200114
115 /**
Pier Ventref8543d82016-09-28 19:49:33 -0700116 * Binds VLAN Ids to a parent resource, given a parent resource.
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200117 *
118 * @param parent the parent resource
Pier Ventref8543d82016-09-28 19:49:33 -0700119 * @return the VLAN Ids allocated
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200120 */
121 private Collection<Resource> addVlanIds(DiscreteResourceId parent) {
122 Collection<Resource> resources = new HashSet<>();
Pier Ventref8543d82016-09-28 19:49:33 -0700123 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 Ventre51313bd2016-06-02 10:50:30 +0200155 }
156 return resources;
157 }
158
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700159 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800160 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
161 Collection<Resource> resources = new HashSet<>();
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200162 resources.addAll(addVlanIds(parent));
Pier Ventref8543d82016-09-28 19:49:33 -0700163 resources.addAll(addMplsLabels(parent));
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200164 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 SHIMIZU83258ae2016-01-29 17:39:07 -0800172 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700173 }
174
175 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800176 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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800191 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800192 return getAvailableResources(parent);
193 }
194
195 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800196 public boolean isAvailable(Resource resource) {
Luca Pretede10c782017-01-05 17:23:08 -0800197 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 SHIMIZUf26e6922015-10-29 16:19:20 -0700202 return true;
203 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800204
205 @Override
206 public void addListener(ResourceListener listener) {}
207
208 @Override
209 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700210}