Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 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; |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 20 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MplsLabel; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 24 | import org.onosproject.net.TributarySlot; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 25 | import org.onosproject.net.resource.ContinuousResourceId; |
| 26 | import org.onosproject.net.resource.DiscreteResource; |
| 27 | import org.onosproject.net.resource.DiscreteResourceId; |
| 28 | import org.onosproject.net.resource.ResourceAllocation; |
| 29 | import org.onosproject.net.resource.ResourceConsumer; |
| 30 | import org.onosproject.net.resource.ResourceId; |
| 31 | import org.onosproject.net.resource.ResourceListener; |
| 32 | import org.onosproject.net.resource.Resource; |
| 33 | import org.onosproject.net.resource.ResourceService; |
| 34 | import org.onosproject.net.resource.Resources; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.HashMap; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 38 | import java.util.HashSet; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | import java.util.Map; |
| 41 | import java.util.Optional; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 42 | import java.util.Set; |
Sho SHIMIZU | 0e03f59b | 2016-06-08 17:03:48 -0700 | [diff] [blame] | 43 | import java.util.function.Function; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 44 | import java.util.stream.Collectors; |
| 45 | |
| 46 | class MockResourceService implements ResourceService { |
| 47 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 48 | private final Map<Resource, ResourceConsumer> assignment = new HashMap<>(); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 49 | |
| 50 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 51 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 52 | assignment.putAll( |
Sho SHIMIZU | 0e03f59b | 2016-06-08 17:03:48 -0700 | [diff] [blame] | 53 | resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 54 | ); |
| 55 | |
| 56 | return resources.stream() |
| 57 | .map(x -> new ResourceAllocation(x, consumer)) |
| 58 | .collect(Collectors.toList()); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean release(List<ResourceAllocation> allocations) { |
| 63 | allocations.forEach(x -> assignment.remove(x.resource())); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean release(ResourceConsumer consumer) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 70 | List<Resource> resources = assignment.entrySet().stream() |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 71 | .filter(x -> x.getValue().equals(consumer)) |
| 72 | .map(Map.Entry::getKey) |
| 73 | .collect(Collectors.toList()); |
| 74 | List<ResourceAllocation> allocations = resources.stream() |
| 75 | .map(x -> new ResourceAllocation(x, consumer)) |
| 76 | .collect(Collectors.toList()); |
| 77 | |
| 78 | return release(allocations); |
| 79 | } |
| 80 | |
| 81 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 82 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 83 | if (id instanceof ContinuousResourceId) { |
| 84 | return ImmutableList.of(); |
| 85 | } |
| 86 | DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource(); |
| 87 | return Optional.ofNullable(assignment.get(discrete)) |
| 88 | .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x))) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 89 | .orElse(ImmutableList.of()); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 93 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 94 | return assignment.entrySet().stream() |
| 95 | .filter(x -> x.getKey().parent().isPresent()) |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 96 | .filter(x -> x.getKey().parent().get().id().equals(parent)) |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 97 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 98 | .collect(Collectors.toList()); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
| 103 | return assignment.entrySet().stream() |
| 104 | .filter(x -> x.getValue().equals(consumer)) |
| 105 | .map(x -> new ResourceAllocation(x.getKey(), x.getValue())) |
| 106 | .collect(Collectors.toList()); |
| 107 | } |
| 108 | |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame^] | 109 | |
| 110 | /** |
| 111 | * It adds a number of VLAN ids in order to test the random behavior. |
| 112 | * |
| 113 | * @param parent the parent resource |
| 114 | * @return a set of VLAN ids |
| 115 | */ |
| 116 | private Collection<Resource> addVlanIds(DiscreteResourceId parent) { |
| 117 | Collection<Resource> resources = new HashSet<>(); |
| 118 | for (int i = VlanId.NO_VID + 1; i < VlanId.MAX_VLAN; i++) { |
| 119 | resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) i))); |
| 120 | } |
| 121 | return resources; |
| 122 | } |
| 123 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 124 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 125 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
| 126 | Collection<Resource> resources = new HashSet<>(); |
Pier Luigi Ventre | 51313bd | 2016-06-02 10:50:30 +0200 | [diff] [blame^] | 127 | resources.addAll(addVlanIds(parent)); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 128 | resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10))); |
Rimon Ashkenazy | a4e3bd3 | 2016-02-22 16:12:20 +0200 | [diff] [blame] | 129 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(1))); |
| 130 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(2))); |
| 131 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(3))); |
| 132 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(4))); |
| 133 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(5))); |
| 134 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(6))); |
| 135 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(7))); |
| 136 | resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(8))); |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 137 | return ImmutableSet.copyOf(resources); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 141 | public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) { |
| 142 | return getAvailableResources(parent).stream() |
| 143 | .filter(x -> x.isTypeOf(cls)) |
| 144 | .collect(Collectors.toSet()); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) { |
| 149 | return getAvailableResources(parent).stream() |
| 150 | .filter(x -> x.isTypeOf(cls)) |
| 151 | .flatMap(x -> Tools.stream(x.valueAs(cls))) |
| 152 | .collect(Collectors.toSet()); |
| 153 | } |
| 154 | |
| 155 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 156 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
HIGUCHI Yuta | dff91af | 2016-01-21 16:34:45 -0800 | [diff] [blame] | 157 | return getAvailableResources(parent); |
| 158 | } |
| 159 | |
| 160 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 161 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 162 | return true; |
| 163 | } |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 164 | |
| 165 | @Override |
| 166 | public void addListener(ResourceListener listener) {} |
| 167 | |
| 168 | @Override |
| 169 | public void removeListener(ResourceListener listener) {} |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 170 | } |