blob: c985882257ba1f5b42f0295547234395b55c6df3 [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 SHIMIZUf26e6922015-10-29 16:19:20 -070022import org.onosproject.net.newresource.ResourceAllocation;
23import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080024import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080025import org.onosproject.net.newresource.Resource;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070026import org.onosproject.net.newresource.ResourceService;
27
28import java.util.Collection;
29import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010030import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070031import java.util.List;
32import java.util.Map;
33import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080034import java.util.Set;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070035import java.util.stream.Collectors;
36
37class MockResourceService implements ResourceService {
38
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080039 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070040
41 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080042 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070043 assignment.putAll(
44 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
45 );
46
47 return resources.stream()
48 .map(x -> new ResourceAllocation(x, consumer))
49 .collect(Collectors.toList());
50 }
51
52 @Override
53 public boolean release(List<ResourceAllocation> allocations) {
54 allocations.forEach(x -> assignment.remove(x.resource()));
55
56 return true;
57 }
58
59 @Override
60 public boolean release(ResourceConsumer consumer) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080061 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070062 .filter(x -> x.getValue().equals(consumer))
63 .map(Map.Entry::getKey)
64 .collect(Collectors.toList());
65 List<ResourceAllocation> allocations = resources.stream()
66 .map(x -> new ResourceAllocation(x, consumer))
67 .collect(Collectors.toList());
68
69 return release(allocations);
70 }
71
72 @Override
Sho SHIMIZU4a1e59f2016-01-26 15:27:13 -080073 public List<ResourceAllocation> getResourceAllocations(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070074 return Optional.ofNullable(assignment.get(resource))
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080075 .map(x -> ImmutableList.of(new ResourceAllocation(resource, x)))
76 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070077 }
78
79 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080080 public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070081 return assignment.entrySet().stream()
82 .filter(x -> x.getKey().parent().isPresent())
83 .filter(x -> x.getKey().parent().get().equals(parent))
84 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
85 .collect(Collectors.toList());
86 }
87
88 @Override
89 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
90 return assignment.entrySet().stream()
91 .filter(x -> x.getValue().equals(consumer))
92 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
93 .collect(Collectors.toList());
94 }
95
96 @Override
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080097 public Set<Resource> getAvailableResources(Resource parent) {
Michele Santuari69fc2ff2015-12-03 17:05:35 +010098
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080099 Collection<Resource> resources = new HashSet<Resource>();
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100100 resources.add(parent.child(VlanId.vlanId((short) 10)));
101 resources.add(parent.child(MplsLabel.mplsLabel(10)));
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800102 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700103 }
104
105 @Override
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800106 public Set<Resource> getRegisteredResources(Resource parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800107 return getAvailableResources(parent);
108 }
109
110 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800111 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700112 return true;
113 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800114
115 @Override
116 public void addListener(ResourceListener listener) {}
117
118 @Override
119 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700120}