blob: c808d91ec172b91328cbeec89b1014804c44af9d [file] [log] [blame]
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUf26e6922015-10-29 16:19:20 -07003 *
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;
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +020020
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070021import org.onlab.packet.MplsLabel;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010022import org.onlab.packet.VlanId;
Sho SHIMIZU7332fe42016-02-15 14:58:33 -080023import org.onlab.util.Tools;
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +020024import org.onosproject.net.TributarySlot;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080025import org.onosproject.net.resource.ContinuousResourceId;
26import org.onosproject.net.resource.DiscreteResource;
27import org.onosproject.net.resource.DiscreteResourceId;
28import org.onosproject.net.resource.ResourceAllocation;
29import org.onosproject.net.resource.ResourceConsumer;
30import org.onosproject.net.resource.ResourceId;
31import org.onosproject.net.resource.ResourceListener;
32import org.onosproject.net.resource.Resource;
33import org.onosproject.net.resource.ResourceService;
34import org.onosproject.net.resource.Resources;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070035
36import java.util.Collection;
37import java.util.HashMap;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010038import java.util.HashSet;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070039import java.util.List;
40import java.util.Map;
41import java.util.Optional;
Sho SHIMIZU83258ae2016-01-29 17:39:07 -080042import java.util.Set;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070043import java.util.stream.Collectors;
44
45class MockResourceService implements ResourceService {
46
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080047 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070048
49 @Override
Jonathan Hart56151262016-02-11 09:48:50 -080050 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070051 assignment.putAll(
52 resources.stream().collect(Collectors.toMap(x -> x, x -> consumer))
53 );
54
55 return resources.stream()
56 .map(x -> new ResourceAllocation(x, consumer))
57 .collect(Collectors.toList());
58 }
59
60 @Override
61 public boolean release(List<ResourceAllocation> allocations) {
62 allocations.forEach(x -> assignment.remove(x.resource()));
63
64 return true;
65 }
66
67 @Override
68 public boolean release(ResourceConsumer consumer) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080069 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070070 .filter(x -> x.getValue().equals(consumer))
71 .map(Map.Entry::getKey)
72 .collect(Collectors.toList());
73 List<ResourceAllocation> allocations = resources.stream()
74 .map(x -> new ResourceAllocation(x, consumer))
75 .collect(Collectors.toList());
76
77 return release(allocations);
78 }
79
80 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080081 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
82 if (id instanceof ContinuousResourceId) {
83 return ImmutableList.of();
84 }
85 DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource();
86 return Optional.ofNullable(assignment.get(discrete))
87 .map(x -> ImmutableList.of(new ResourceAllocation(discrete, x)))
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -080088 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070089 }
90
91 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080092 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070093 return assignment.entrySet().stream()
94 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080095 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070096 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
97 .collect(Collectors.toList());
98 }
99
100 @Override
101 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
102 return assignment.entrySet().stream()
103 .filter(x -> x.getValue().equals(consumer))
104 .map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
105 .collect(Collectors.toList());
106 }
107
108 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800109 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
110 Collection<Resource> resources = new HashSet<>();
111 resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) 10)));
112 resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10)));
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200113 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(1)));
114 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(2)));
115 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(3)));
116 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(4)));
117 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(5)));
118 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(6)));
119 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(7)));
120 resources.add(Resources.discrete(parent).resource().child(TributarySlot.of(8)));
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800121 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700122 }
123
124 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800125 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
126 return getAvailableResources(parent).stream()
127 .filter(x -> x.isTypeOf(cls))
128 .collect(Collectors.toSet());
129 }
130
131 @Override
132 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
133 return getAvailableResources(parent).stream()
134 .filter(x -> x.isTypeOf(cls))
135 .flatMap(x -> Tools.stream(x.valueAs(cls)))
136 .collect(Collectors.toSet());
137 }
138
139 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800140 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800141 return getAvailableResources(parent);
142 }
143
144 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800145 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700146 return true;
147 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800148
149 @Override
150 public void addListener(ResourceListener listener) {}
151
152 @Override
153 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700154}