blob: 424a1e1d1437399966ed038712ec10a2ea95f6f8 [file] [log] [blame]
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import com.google.common.collect.ImmutableList;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080019import com.google.common.collect.ImmutableSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070020import org.onlab.packet.MplsLabel;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010021import org.onlab.packet.VlanId;
Sho SHIMIZU7332fe42016-02-15 14:58:33 -080022import org.onlab.util.Tools;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080023import org.onosproject.net.newresource.ContinuousResourceId;
24import org.onosproject.net.newresource.DiscreteResource;
25import org.onosproject.net.newresource.DiscreteResourceId;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070026import org.onosproject.net.newresource.ResourceAllocation;
27import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080028import org.onosproject.net.newresource.ResourceId;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080029import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080030import org.onosproject.net.newresource.Resource;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070031import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080032import org.onosproject.net.newresource.Resources;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070033
34import java.util.Collection;
35import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010036import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070037import java.util.List;
38import java.util.Map;
39import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080040import java.util.Set;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070041import java.util.stream.Collectors;
42
43class MockResourceService implements ResourceService {
44
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080045 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070046
47 @Override
Jonathan Hart56151262016-02-11 09:48:50 -080048 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070049 assignment.putAll(
50 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
51 );
52
53 return resources.stream()
54 .map(x -> new ResourceAllocation(x, consumer))
55 .collect(Collectors.toList());
56 }
57
58 @Override
59 public boolean release(List<ResourceAllocation> allocations) {
60 allocations.forEach(x -> assignment.remove(x.resource()));
61
62 return true;
63 }
64
65 @Override
66 public boolean release(ResourceConsumer consumer) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080067 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070068 .filter(x -> x.getValue().equals(consumer))
69 .map(Map.Entry::getKey)
70 .collect(Collectors.toList());
71 List<ResourceAllocation> allocations = resources.stream()
72 .map(x -> new ResourceAllocation(x, consumer))
73 .collect(Collectors.toList());
74
75 return release(allocations);
76 }
77
78 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080079 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
80 if (id instanceof ContinuousResourceId) {
81 return ImmutableList.of();
82 }
83 DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource();
84 return Optional.ofNullable(assignment.get(discrete))
85 .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x)))
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080086 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070087 }
88
89 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080090 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070091 return assignment.entrySet().stream()
92 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080093 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070094 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
95 .collect(Collectors.toList());
96 }
97
98 @Override
99 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
100 return assignment.entrySet().stream()
101 .filter(x -> x.getValue().equals(consumer))
102 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
103 .collect(Collectors.toList());
104 }
105
106 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800107 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
108 Collection<Resource> resources = new HashSet<>();
109 resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) 10)));
110 resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10)));
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800111 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700112 }
113
114 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800115 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
116 return getAvailableResources(parent).stream()
117 .filter(x -> x.isTypeOf(cls))
118 .collect(Collectors.toSet());
119 }
120
121 @Override
122 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
123 return getAvailableResources(parent).stream()
124 .filter(x -> x.isTypeOf(cls))
125 .flatMap(x -> Tools.stream(x.valueAs(cls)))
126 .collect(Collectors.toSet());
127 }
128
129 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800130 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800131 return getAvailableResources(parent);
132 }
133
134 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800135 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700136 return true;
137 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800138
139 @Override
140 public void addListener(ResourceListener listener) {}
141
142 @Override
143 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700144}