blob: 2926346117259abe2fbd65b61c9dfb5993cbf954 [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;
19import org.onlab.packet.MplsLabel;
20import org.onosproject.net.newresource.ResourceAllocation;
21import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080022import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070023import org.onosproject.net.newresource.ResourcePath;
24import org.onosproject.net.newresource.ResourceService;
25
26import java.util.Collection;
27import java.util.HashMap;
28import java.util.List;
29import java.util.Map;
30import java.util.Optional;
31import java.util.stream.Collectors;
32
33class MockResourceService implements ResourceService {
34
35 private final Map<ResourcePath, ResourceConsumer> assignment = new HashMap<>();
36
37 @Override
38 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources) {
39 assignment.putAll(
40 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
41 );
42
43 return resources.stream()
44 .map(x -> new ResourceAllocation(x, consumer))
45 .collect(Collectors.toList());
46 }
47
48 @Override
49 public boolean release(List<ResourceAllocation> allocations) {
50 allocations.forEach(x -> assignment.remove(x.resource()));
51
52 return true;
53 }
54
55 @Override
56 public boolean release(ResourceConsumer consumer) {
57 List<ResourcePath> resources = assignment.entrySet().stream()
58 .filter(x -> x.getValue().equals(consumer))
59 .map(Map.Entry::getKey)
60 .collect(Collectors.toList());
61 List<ResourceAllocation> allocations = resources.stream()
62 .map(x -> new ResourceAllocation(x, consumer))
63 .collect(Collectors.toList());
64
65 return release(allocations);
66 }
67
68 @Override
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080069 public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070070 return Optional.ofNullable(assignment.get(resource))
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080071 .map(x -> ImmutableList.of(new ResourceAllocation(resource, x)))
72 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070073 }
74
75 @Override
76 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
77 return assignment.entrySet().stream()
78 .filter(x -> x.getKey().parent().isPresent())
79 .filter(x -> x.getKey().parent().get().equals(parent))
80 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
81 .collect(Collectors.toList());
82 }
83
84 @Override
85 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
86 return assignment.entrySet().stream()
87 .filter(x -> x.getValue().equals(consumer))
88 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
89 .collect(Collectors.toList());
90 }
91
92 @Override
93 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
Sho SHIMIZUc9546a32015-11-10 11:22:28 -080094 ResourcePath resource = parent.child(MplsLabel.mplsLabel(10));
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070095 return ImmutableList.of(resource);
96 }
97
98 @Override
99 public boolean isAvailable(ResourcePath resource) {
100 return true;
101 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800102
103 @Override
104 public void addListener(ResourceListener listener) {}
105
106 @Override
107 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700108}