blob: 866c513c5b5b8f5a48936ad19964f112e53c2a30 [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;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010020import org.onlab.packet.VlanId;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070021import org.onosproject.net.newresource.ResourceAllocation;
22import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080023import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070024import org.onosproject.net.newresource.ResourcePath;
25import org.onosproject.net.newresource.ResourceService;
26
27import java.util.Collection;
28import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010029import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070030import java.util.List;
31import java.util.Map;
32import java.util.Optional;
33import java.util.stream.Collectors;
34
35class MockResourceService implements ResourceService {
36
37 private final Map<ResourcePath, ResourceConsumer> assignment = new HashMap<>();
38
39 @Override
40 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources) {
41 assignment.putAll(
42 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
43 );
44
45 return resources.stream()
46 .map(x -> new ResourceAllocation(x, consumer))
47 .collect(Collectors.toList());
48 }
49
50 @Override
51 public boolean release(List<ResourceAllocation> allocations) {
52 allocations.forEach(x -> assignment.remove(x.resource()));
53
54 return true;
55 }
56
57 @Override
58 public boolean release(ResourceConsumer consumer) {
59 List<ResourcePath> resources = assignment.entrySet().stream()
60 .filter(x -> x.getValue().equals(consumer))
61 .map(Map.Entry::getKey)
62 .collect(Collectors.toList());
63 List<ResourceAllocation> allocations = resources.stream()
64 .map(x -> new ResourceAllocation(x, consumer))
65 .collect(Collectors.toList());
66
67 return release(allocations);
68 }
69
70 @Override
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080071 public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070072 return Optional.ofNullable(assignment.get(resource))
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080073 .map(x -> ImmutableList.of(new ResourceAllocation(resource, x)))
74 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070075 }
76
77 @Override
78 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
79 return assignment.entrySet().stream()
80 .filter(x -> x.getKey().parent().isPresent())
81 .filter(x -> x.getKey().parent().get().equals(parent))
82 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
83 .collect(Collectors.toList());
84 }
85
86 @Override
87 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
88 return assignment.entrySet().stream()
89 .filter(x -> x.getValue().equals(consumer))
90 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
91 .collect(Collectors.toList());
92 }
93
94 @Override
95 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
Michele Santuari69fc2ff2015-12-03 17:05:35 +010096
97 Collection<ResourcePath> resources = new HashSet<ResourcePath>();
98 resources.add(parent.child(VlanId.vlanId((short) 10)));
99 resources.add(parent.child(MplsLabel.mplsLabel(10)));
100 return ImmutableList.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700101 }
102
103 @Override
104 public boolean isAvailable(ResourcePath resource) {
105 return true;
106 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800107
108 @Override
109 public void addListener(ResourceListener listener) {}
110
111 @Override
112 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700113}