blob: 62613e0de6fd0e216b737afa3273ca2c30eb1346 [file] [log] [blame]
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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<>();
Pier Luigi0b4222e2017-07-21 09:47:14 +020043 public boolean filterAssignment = false;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070044
Luca Pretede10c782017-01-05 17:23:08 -080045 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 SHIMIZUf26e6922015-10-29 16:19:20 -070056 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -070057 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070058 assignment.putAll(
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070059 resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070060 );
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080076 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070077 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -080088 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 SHIMIZU6c9e33a2016-01-07 18:45:27 -080095 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070096 }
97
98 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080099 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700100 return assignment.entrySet().stream()
101 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800102 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700103 .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 Ventre51313bd2016-06-02 10:50:30 +0200115
116 /**
Pier Ventref8543d82016-09-28 19:49:33 -0700117 * Binds VLAN Ids to a parent resource, given a parent resource.
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200118 *
119 * @param parent the parent resource
Pier Ventref8543d82016-09-28 19:49:33 -0700120 * @return the VLAN Ids allocated
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200121 */
122 private Collection<Resource> addVlanIds(DiscreteResourceId parent) {
123 Collection<Resource> resources = new HashSet<>();
Pier Ventref8543d82016-09-28 19:49:33 -0700124 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 Ventre51313bd2016-06-02 10:50:30 +0200156 }
157 return resources;
158 }
159
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700160 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800161 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
162 Collection<Resource> resources = new HashSet<>();
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200163 resources.addAll(addVlanIds(parent));
Pier Ventref8543d82016-09-28 19:49:33 -0700164 resources.addAll(addMplsLabels(parent));
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200165 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 Luigi0b4222e2017-07-21 09:47:14 +0200173 return filterAssignment ? ImmutableSet.copyOf(
174 resources.stream().filter(
175 resource -> assignment.get(resource) == null
176 ).collect(Collectors.toSet())
177 ) : ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700178 }
179
180 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800181 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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800196 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800197 return getAvailableResources(parent);
198 }
199
200 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800201 public boolean isAvailable(Resource resource) {
Luca Pretede10c782017-01-05 17:23:08 -0800202 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 SHIMIZUf26e6922015-10-29 16:19:20 -0700207 return true;
208 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800209
210 @Override
211 public void addListener(ResourceListener listener) {}
212
213 @Override
214 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700215}