blob: 436a763f6f8114bacad7763e09de9c44dff641a8 [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;
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<>();
Pier Ventref8543d82016-09-28 19:49:33 -070039 public Set<Short> availableVlanLabels = new HashSet<>();
40 public Set<Integer> availableMplsLabels = new HashSet<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070041
42 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -070043 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070044 assignment.putAll(
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070045 resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070046 );
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080062 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070063 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -080074 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 SHIMIZU6c9e33a2016-01-07 18:45:27 -080081 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070082 }
83
84 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080085 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070086 return assignment.entrySet().stream()
87 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080088 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070089 .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 Ventre51313bd2016-06-02 10:50:30 +0200101
102 /**
Pier Ventref8543d82016-09-28 19:49:33 -0700103 * Binds VLAN Ids to a parent resource, given a parent resource.
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200104 *
105 * @param parent the parent resource
Pier Ventref8543d82016-09-28 19:49:33 -0700106 * @return the VLAN Ids allocated
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200107 */
108 private Collection<Resource> addVlanIds(DiscreteResourceId parent) {
109 Collection<Resource> resources = new HashSet<>();
Pier Ventref8543d82016-09-28 19:49:33 -0700110 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 Ventre51313bd2016-06-02 10:50:30 +0200142 }
143 return resources;
144 }
145
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700146 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800147 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
148 Collection<Resource> resources = new HashSet<>();
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200149 resources.addAll(addVlanIds(parent));
Pier Ventref8543d82016-09-28 19:49:33 -0700150 resources.addAll(addMplsLabels(parent));
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200151 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 SHIMIZU83258ae2016-01-29 17:39:07 -0800159 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700160 }
161
162 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800163 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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800178 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800179 return getAvailableResources(parent);
180 }
181
182 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800183 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700184 return true;
185 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800186
187 @Override
188 public void addListener(ResourceListener listener) {}
189
190 @Override
191 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700192}