blob: 4083f5b2abfc50a77049a2467ea99a457796e99a [file] [log] [blame]
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07001/*
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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import com.google.common.collect.ImmutableList;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080019import com.google.common.collect.ImmutableSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070020import org.onlab.packet.MplsLabel;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010021import org.onlab.packet.VlanId;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080022import org.onosproject.net.newresource.ContinuousResourceId;
23import org.onosproject.net.newresource.DiscreteResource;
24import org.onosproject.net.newresource.DiscreteResourceId;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070025import org.onosproject.net.newresource.ResourceAllocation;
26import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080027import org.onosproject.net.newresource.ResourceId;
Sho SHIMIZUfa62b472015-11-02 17:35:46 -080028import org.onosproject.net.newresource.ResourceListener;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080029import org.onosproject.net.newresource.Resource;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070030import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080031import org.onosproject.net.newresource.Resources;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070032
33import java.util.Collection;
34import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010035import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070036import java.util.List;
37import java.util.Map;
38import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080039import java.util.Set;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070040import java.util.stream.Collectors;
41
42class MockResourceService implements ResourceService {
43
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080044 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070045
46 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080047 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070048 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 SHIMIZU8fa670a2016-01-14 11:17:18 -080066 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070067 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -080078 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 SHIMIZU6c9e33a2016-01-07 18:45:27 -080085 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070086 }
87
88 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080089 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070090 return assignment.entrySet().stream()
91 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080092 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070093 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800106 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 SHIMIZU83258ae2016-01-29 17:39:07 -0800110 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700111 }
112
113 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800114 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800115 return getAvailableResources(parent);
116 }
117
118 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800119 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700120 return true;
121 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800122
123 @Override
124 public void addListener(ResourceListener listener) {}
125
126 @Override
127 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700128}