Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
| 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; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 22 | import org.onosproject.net.newresource.ResourceListener; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 23 | import org.onosproject.net.newresource.ResourcePath; |
| 24 | import org.onosproject.net.newresource.ResourceService; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Optional; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
| 33 | class 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 SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame^] | 69 | public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 70 | return Optional.ofNullable(assignment.get(resource)) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame^] | 71 | .map(x -> ImmutableList.of(new ResourceAllocation(resource, x))) |
| 72 | .orElse(ImmutableList.of()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 73 | } |
| 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 SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 94 | ResourcePath resource = parent.child(MplsLabel.mplsLabel(10)); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 95 | return ImmutableList.of(resource); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean isAvailable(ResourcePath resource) { |
| 100 | return true; |
| 101 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | public void addListener(ResourceListener listener) {} |
| 105 | |
| 106 | @Override |
| 107 | public void removeListener(ResourceListener listener) {} |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 108 | } |