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; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame^] | 20 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 21 | import org.onosproject.net.newresource.ResourceAllocation; |
| 22 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 23 | import org.onosproject.net.newresource.ResourceListener; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 24 | import org.onosproject.net.newresource.ResourcePath; |
| 25 | import org.onosproject.net.newresource.ResourceService; |
| 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.HashMap; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame^] | 29 | import java.util.HashSet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Optional; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | class 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 SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 71 | public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 72 | return Optional.ofNullable(assignment.get(resource)) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 73 | .map(x -> ImmutableList.of(new ResourceAllocation(resource, x))) |
| 74 | .orElse(ImmutableList.of()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 75 | } |
| 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 Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame^] | 96 | |
| 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 SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean isAvailable(ResourcePath resource) { |
| 105 | return true; |
| 106 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public void addListener(ResourceListener listener) {} |
| 110 | |
| 111 | @Override |
| 112 | public void removeListener(ResourceListener listener) {} |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 113 | } |