2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.intent.impl.compiler;
18 import com.google.common.collect.ImmutableList;
19 import org.onlab.packet.MplsLabel;
20 import org.onosproject.net.newresource.ResourceAllocation;
21 import org.onosproject.net.newresource.ResourceConsumer;
22 import org.onosproject.net.newresource.ResourceListener;
23 import org.onosproject.net.newresource.ResourcePath;
24 import org.onosproject.net.newresource.ResourceService;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
33 class MockResourceService implements ResourceService {
35 private final Map<ResourcePath, ResourceConsumer> assignment = new HashMap<>();
38 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources) {
40 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
43 return resources.stream()
44 .map(x -> new ResourceAllocation(x, consumer))
45 .collect(Collectors.toList());
49 public boolean release(List<ResourceAllocation> allocations) {
50 allocations.forEach(x -> assignment.remove(x.resource()));
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());
65 return release(allocations);
69 public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
70 return Optional.ofNullable(assignment.get(resource))
71 .map(x -> new ResourceAllocation(resource, x));
75 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
76 return assignment.entrySet().stream()
77 .filter(x -> x.getKey().parent().isPresent())
78 .filter(x -> x.getKey().parent().get().equals(parent))
79 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
80 .collect(Collectors.toList());
84 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
85 return assignment.entrySet().stream()
86 .filter(x -> x.getValue().equals(consumer))
87 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
88 .collect(Collectors.toList());
92 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
93 ResourcePath resource = ResourcePath.child(parent, MplsLabel.mplsLabel(10));
94 return ImmutableList.of(resource);
98 public boolean isAvailable(ResourcePath resource) {
103 public void addListener(ResourceListener listener) {}
106 public void removeListener(ResourceListener listener) {}