blob: 9da412e2500546ce28623eae025bdf30b10f9a98 [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 SHIMIZU0e03f59b2016-06-08 17:03:48 -070043import java.util.function.Function;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070044import java.util.stream.Collectors;
45
46class MockResourceService implements ResourceService {
47
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080048 private final Map<Resource, ResourceConsumer> assignment = new HashMap<>();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070049
50 @Override
Jonathan Hart56151262016-02-11 09:48:50 -080051 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070052 assignment.putAll(
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070053 resources.stream().collect(Collectors.toMap(Function.identity(), x -> consumer))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070054 );
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080070 List<Resource> resources = assignment.entrySet().stream()
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070071 .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 SHIMIZUdd3750c2016-02-01 11:37:04 -080082 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 SHIMIZU6c9e33a2016-01-07 18:45:27 -080089 .orElse(ImmutableList.of());
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070090 }
91
92 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080093 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070094 return assignment.entrySet().stream()
95 .filter(x -> x.getKey().parent().isPresent())
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080096 .filter(x -> x.getKey().parent().get().id().equals(parent))
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070097 .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 Ventre51313bd2016-06-02 10:50:30 +0200109
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 SHIMIZUf26e6922015-10-29 16:19:20 -0700124 @Override
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800125 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
126 Collection<Resource> resources = new HashSet<>();
Pier Luigi Ventre51313bd2016-06-02 10:50:30 +0200127 resources.addAll(addVlanIds(parent));
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800128 resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10)));
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200129 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 SHIMIZU83258ae2016-01-29 17:39:07 -0800137 return ImmutableSet.copyOf(resources);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700138 }
139
140 @Override
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800141 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 SHIMIZUdd3750c2016-02-01 11:37:04 -0800156 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
HIGUCHI Yutadff91af2016-01-21 16:34:45 -0800157 return getAvailableResources(parent);
158 }
159
160 @Override
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800161 public boolean isAvailable(Resource resource) {
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700162 return true;
163 }
Sho SHIMIZUfa62b472015-11-02 17:35:46 -0800164
165 @Override
166 public void addListener(ResourceListener listener) {}
167
168 @Override
169 public void removeListener(ResourceListener listener) {}
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700170}