blob: f5f884bb52d257dc7045c360e774cc4ab5388c54 [file] [log] [blame]
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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;
Sho SHIMIZU7332fe42016-02-15 14:58:33 -080023import org.onlab.util.Tools;
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +020024import org.onosproject.net.TributarySlot;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070025
26import java.util.Collection;
27import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010028import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070029import java.util.List;
30import java.util.Map;
31import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080032import java.util.Set;
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070033import java.util.function.Function;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070034import java.util.stream.Collectors;
35
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070036public class MockResourceService implements ResourceService {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070037
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080038 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070039
40 @Override
Jonathan Hart56151262016-02-11 09:48:50 -080041 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070042 assignment.putAll(
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070043 resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070044 );
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080060 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070061 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -080072 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 SHIMIZU6c9e33a2016-01-07 18:45:27 -080079 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070080 }
81
82 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080083 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070084 return assignment.entrySet().stream()
85 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080086 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070087 .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 Ventre51313bd2016-06-02 10:50:30 +020099
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 SHIMIZUf26e6922015-10-29 16:19:20 -0700114 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800115 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
116 Collection<Resource> resources = new HashSet<>();
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200117 resources.addAll(addVlanIds(parent));
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800118 resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10)));
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200119 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 SHIMIZU83258ae2016-01-29 17:39:07 -0800127 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700128 }
129
130 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800131 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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800146 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800147 return getAvailableResources(parent);
148 }
149
150 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800151 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700152 return true;
153 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800154
155 @Override
156 public void addListener(ResourceListener listener) {}
157
158 @Override
159 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700160}