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.ResourcePath;
23 import org.onosproject.net.newresource.ResourceService;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Optional;
30 import java.util.stream.Collectors;
32 class MockResourceService implements ResourceService {
34 private final Map<ResourcePath, ResourceConsumer> assignment = new HashMap<>();
37 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources) {
39 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
42 return resources.stream()
43 .map(x -> new ResourceAllocation(x, consumer))
44 .collect(Collectors.toList());
48 public boolean release(List<ResourceAllocation> allocations) {
49 allocations.forEach(x -> assignment.remove(x.resource()));
55 public boolean release(ResourceConsumer consumer) {
56 List<ResourcePath> resources = assignment.entrySet().stream()
57 .filter(x -> x.getValue().equals(consumer))
58 .map(Map.Entry::getKey)
59 .collect(Collectors.toList());
60 List<ResourceAllocation> allocations = resources.stream()
61 .map(x -> new ResourceAllocation(x, consumer))
62 .collect(Collectors.toList());
64 return release(allocations);
68 public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
69 return Optional.ofNullable(assignment.get(resource))
70 .map(x -> new ResourceAllocation(resource, x));
74 public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
75 return assignment.entrySet().stream()
76 .filter(x -> x.getKey().parent().isPresent())
77 .filter(x -> x.getKey().parent().get().equals(parent))
78 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
79 .collect(Collectors.toList());
83 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
84 return assignment.entrySet().stream()
85 .filter(x -> x.getValue().equals(consumer))
86 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
87 .collect(Collectors.toList());
91 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
92 ResourcePath resource = ResourcePath.child(parent, MplsLabel.mplsLabel(10));
93 return ImmutableList.of(resource);
97 public boolean isAvailable(ResourcePath resource) {