blob: 40867680acfc4eddbf9e6722571f36c73712b73d [file] [log] [blame]
Pier Ventref8543d82016-09-28 19:49:33 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.net.resource.impl;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.Iterables;
22import com.google.common.collect.Maps;
23import com.google.common.collect.Sets;
24import org.apache.commons.lang.math.RandomUtils;
25import org.onlab.packet.MplsLabel;
26import org.onlab.packet.VlanId;
27import org.onlab.util.Identifier;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.EncapsulationType;
30import org.onosproject.net.Link;
31import org.onosproject.net.LinkKey;
32import org.onosproject.net.intent.IntentId;
33import org.onosproject.net.resource.Resource;
34import org.onosproject.net.resource.ResourceAllocation;
35import org.onosproject.net.resource.ResourceService;
36import org.onosproject.net.resource.Resources;
37
38import java.util.Collections;
39import java.util.List;
40import java.util.Map;
41import java.util.Set;
42import java.util.stream.Collectors;
43import java.util.stream.Stream;
44
45/**
46 * Helper class which interacts with the ResourceService and provides
47 * a unified API to allocate MPLS labels and VLAN Ids.
48 */
49public final class LabelAllocator {
50
51 private enum Behavior {
52 /**
53 * Random selection.
54 */
55 RANDOM,
56 /**
57 * First fit selection.
58 */
59 FIRST_FIT
60 }
61
62 private static final Behavior[] BEHAVIORS = Behavior.values();
63
64 private ResourceService resourceService;
65 private LabelSelection labelSelection;
66
67 /**
68 * Creates a new label allocator. Random is the
69 * default behavior.
70 *
71 * @param rs the resource service
72 */
73 public LabelAllocator(ResourceService rs) {
74 this.resourceService = rs;
75 this.labelSelection = this.getLabelSelection(Behavior.RANDOM);
76 }
77
78 /**
79 * Checks if a given string is a valid Behavior.
80 *
81 * @param value the string to check
82 * @return true if value is a valid Behavior, false otherwise
83 */
84 public static boolean isInEnum(String value) {
85 for (Behavior b : BEHAVIORS) {
86 if (b.name().equals(value)) {
87 return true;
88 }
89 }
90 return false;
91 }
92
93 /**
94 * Changes the selection behavior.
95 *
96 * @param type the behavior type
97 */
98 public void setLabelSelection(String type) {
99 if (isInEnum(type)) {
100 this.labelSelection = this.getLabelSelection(type);
101 }
102 }
103
104 /**
105 * Retrieves the label selection behavior.
106 *
107 * @return the label selection behavior in use
108 */
109 public LabelSelection getLabelSelection() {
110 return this.labelSelection;
111 }
112
113 /**
114 * Returns the label selection behavior, given a behavior type.
115 *
116 * @param type the behavior type
117 * @return the label selection behavior in use
118 */
119 private LabelSelection getLabelSelection(String type) {
120 Behavior behavior = Behavior.valueOf(type);
121 return this.getLabelSelection(behavior);
122 }
123
124 /**
125 * Creates a new LabelSelection. Random is
126 * the default label selection behavior.
127 *
128 * @param type the behavior type
129 * @return the object implementing the behavior
130 */
131 private LabelSelection getLabelSelection(Behavior type) {
132 LabelSelection selection = null;
133 switch (type) {
134 case FIRST_FIT:
135 selection = new FirstFitSelection();
136 break;
137 case RANDOM:
138 default:
139 selection = new RandomSelection();
140 break;
141 }
142 return selection;
143 }
144
145 /**
146 * Looks for available Ids.
147 *
148 * @param links the links where to look for Ids
149 * @param type the encapsulation type
150 * @return the mappings between key and id
151 */
152 private Map<LinkKey, Identifier<?>> findAvailableIDs(Set<LinkKey> links, EncapsulationType type) {
153
154 Map<LinkKey, Identifier<?>> ids = Maps.newHashMap();
155 for (LinkKey link : links) {
156 Set<Identifier<?>> availableIDsatSrc = getAvailableIDs(link.src(), type);
157 Set<Identifier<?>> availableIDsatDst = getAvailableIDs(link.dst(), type);
158 Set<Identifier<?>> common = Sets.intersection(availableIDsatSrc, availableIDsatDst);
159 if (common.isEmpty()) {
160 continue;
161 }
162 Identifier<?> selected = labelSelection.select(common);
163 if (selected == null) {
164 continue;
165 }
166 ids.put(link, selected);
167 }
168 return ids;
169 }
170
171 /**
172 * Looks for available Ids associated to the given connection point.
173 *
174 * @param cp the connection point
175 * @param type the type of Id
176 * @return the set of available Ids
177 */
178 private Set<Identifier<?>> getAvailableIDs(ConnectPoint cp, EncapsulationType type) {
179 return resourceService.getAvailableResourceValues(
180 Resources.discrete(cp.deviceId(), cp.port()).id(), getEncapsulationClass(type)
181 );
182 }
183
184 /**
185 * Method to map the encapsulation type to identifier class.
186 * VLAN is the default encapsulation.
187 *
188 * @param type the type of encapsulation
189 * @return the id class
190 */
191 private Class getEncapsulationClass(EncapsulationType type) {
192 Class idType;
193 switch (type) {
194 case MPLS:
195 idType = MplsLabel.class;
196 break;
197 case VLAN:
198 default:
199 idType = VlanId.class;
200 }
201 return idType;
202 }
203
204 /**
205 * Allocates labels and associates them to links.
206 *
207 * @param links the links where labels will be allocated
208 * @param id the intent Id
209 * @param type the encapsulation type
210 * @return the list of links and associated labels
211 */
212 public Map<LinkKey, Identifier<?>> assignLabelToLinks(Set<Link> links, IntentId id, EncapsulationType type) {
213
214 Set<LinkKey> linkRequest = Sets.newHashSet();
215
216 links.forEach(link -> {
217 linkRequest.add(LinkKey.linkKey(link));
218 });
219
220 Map<LinkKey, Identifier<?>> availableIds = findAvailableIDs(linkRequest, type);
221 if (availableIds.isEmpty()) {
222 return Collections.emptyMap();
223 }
224
225 Set<Resource> resources = availableIds.entrySet().stream()
226 .flatMap(x -> Stream.of(
227 Resources.discrete(
228 x.getKey().src().deviceId(),
229 x.getKey().src().port(),
230 x.getValue()
231 ).resource(),
232 Resources.discrete(
233 x.getKey().dst().deviceId(),
234 x.getKey().dst().port(),
235 x.getValue()
236 ).resource()
237 ))
238 .collect(Collectors.toSet());
239
240 List<ResourceAllocation> allocations = resourceService.allocate(id, ImmutableList.copyOf(resources));
241
242 if (allocations.isEmpty()) {
243 return Collections.emptyMap();
244 }
245
246 return ImmutableMap.copyOf(availableIds);
247 }
248
249 /**
250 * Allocates labels and associates them to source
251 * and destination ports of a link.
252 *
253 * @param links the links on which labels will be reserved
254 * @param id the intent Id
255 * @param type the encapsulation type
256 * @return the list of ports and associated labels
257 */
258 public Map<ConnectPoint, Identifier<?>> assignLabelToPorts(Set<Link> links, IntentId id, EncapsulationType type) {
259 Map<LinkKey, Identifier<?>> allocation = this.assignLabelToLinks(links, id, type);
260 if (allocation.isEmpty()) {
261 return Collections.emptyMap();
262 }
263 Map<ConnectPoint, Identifier<?>> finalAllocation = Maps.newHashMap();
264 allocation.forEach((key, value) -> {
265 finalAllocation.putIfAbsent(key.src(), value);
266 finalAllocation.putIfAbsent(key.dst(), value);
267 });
268 return ImmutableMap.copyOf(finalAllocation);
269 }
270
271 /**
272 * Interface for selection algorithms of the labels.
273 */
274 public interface LabelSelection {
275
276 /**
277 * Picks an element from values using a particular algorithm.
278 *
279 * @param values the values to select from
280 * @return the selected identifier if values are present, null otherwise
281 */
282 Identifier<?> select(Set<Identifier<?>> values);
283
284 }
285
286 /**
287 * Random label selection.
288 */
289 public static class RandomSelection implements LabelSelection {
290
291 /**
292 * Selects an identifier from a given set of values using
293 * the random selection algorithm.
294 *
295 * @param values the values to select from
296 * @return the selected identifier if values are present, null otherwise
297 */
298 @Override
299 public Identifier<?> select(Set<Identifier<?>> values) {
300 if (!values.isEmpty()) {
301 int size = values.size();
302 int index = RandomUtils.nextInt(size);
303 return Iterables.get(values, index);
304 }
305 return null;
306 }
307 }
308
309 /**
310 * First fit label selection.
311 */
312 public static class FirstFitSelection implements LabelSelection {
313
314 /**
315 * Selects an identifier from a given set of values using
316 * the first fir selection algorithm.
317 *
318 * @param values the values to select from
319 * @return the selected identifier if values are present, null otherwise.
320 */
321 @Override
322 public Identifier<?> select(Set<Identifier<?>> values) {
323 if (!values.isEmpty()) {
324 return values.iterator().next();
325 }
326 return null;
327 }
328 }
329
330}