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; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 20 | import org.onlab.packet.MplsLabel; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 21 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 22 | import org.onosproject.net.newresource.ContinuousResourceId; |
| 23 | import org.onosproject.net.newresource.DiscreteResource; |
| 24 | import org.onosproject.net.newresource.DiscreteResourceId; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 25 | import org.onosproject.net.newresource.ResourceAllocation; |
| 26 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 27 | import org.onosproject.net.newresource.ResourceId; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 28 | import org.onosproject.net.newresource.ResourceListener; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 29 | import org.onosproject.net.newresource.Resource; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 30 | import org.onosproject.net.newresource.ResourceService; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 31 | import org.onosproject.net.newresource.Resources; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.Collection; |
| 34 | import java.util.HashMap; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 35 | import java.util.HashSet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 36 | import java.util.List; |
| 37 | import java.util.Map; |
| 38 | import java.util.Optional; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 39 | import java.util.Set; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | class MockResourceService implements ResourceService { |
| 43 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 44 | private final Map<Resource, ResourceConsumer> assignment = new HashMap<>(); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 47 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 48 | assignment.putAll( |
| 49 | resources.stream().collect(Collectors.toMap(x -> x, x -> consumer)) |
| 50 | ); |
| 51 | |
| 52 | return resources.stream() |
| 53 | .map(x -> new ResourceAllocation(x, consumer)) |
| 54 | .collect(Collectors.toList()); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean release(List<ResourceAllocation> allocations) { |
| 59 | allocations.forEach(x -> assignment.remove(x.resource())); |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean release(ResourceConsumer consumer) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 66 | List<Resource> resources = assignment.entrySet().stream() |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 67 | .filter(x -> x.getValue().equals(consumer)) |
| 68 | .map(Map.Entry::getKey) |
| 69 | .collect(Collectors.toList()); |
| 70 | List<ResourceAllocation> allocations = resources.stream() |
| 71 | .map(x -> new ResourceAllocation(x, consumer)) |
| 72 | .collect(Collectors.toList()); |
| 73 | |
| 74 | return release(allocations); |
| 75 | } |
| 76 | |
| 77 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 78 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 79 | if (id instanceof ContinuousResourceId) { |
| 80 | return ImmutableList.of(); |
| 81 | } |
| 82 | DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource(); |
| 83 | return Optional.ofNullable(assignment.get(discrete)) |
| 84 | .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x))) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 85 | .orElse(ImmutableList.of()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 89 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 90 | return assignment.entrySet().stream() |
| 91 | .filter(x -> x.getKey().parent().isPresent()) |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 92 | .filter(x -> x.getKey().parent().get().id().equals(parent)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 93 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 94 | .collect(Collectors.toList()); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
| 99 | return assignment.entrySet().stream() |
| 100 | .filter(x -> x.getValue().equals(consumer)) |
| 101 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 102 | .collect(Collectors.toList()); |
| 103 | } |
| 104 | |
| 105 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 106 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
| 107 | Collection<Resource> resources = new HashSet<>(); |
| 108 | resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) 10))); |
| 109 | resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10))); |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 110 | return ImmutableSet.copyOf(resources); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame^] | 114 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 115 | return getAvailableResources(parent); |
| 116 | } |
| 117 | |
| 118 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 119 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 122 | |
| 123 | @Override |
| 124 | public void addListener(ResourceListener listener) {} |
| 125 | |
| 126 | @Override |
| 127 | public void removeListener(ResourceListener listener) {} |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 128 | } |