blob: 8dea4476167c91d7d867419ab508738aa0aee45c [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -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
Sho SHIMIZU12d02682016-02-24 13:55:12 -080018import com.google.common.base.Strings;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020019
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070020import org.apache.commons.lang3.tuple.Pair;
21import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070022import org.apache.felix.scr.annotations.Component;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070023import org.apache.felix.scr.annotations.Deactivate;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070024import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070028import org.onlab.util.Tools;
29import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070032import org.onosproject.net.AnnotationKeys;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020033import org.onosproject.net.CltSignalType;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070034import org.onosproject.net.ConnectPoint;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070035import org.onosproject.net.OchPort;
36import org.onosproject.net.OduCltPort;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020037import org.onosproject.net.OduSignalId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070038import org.onosproject.net.OduSignalType;
39import org.onosproject.net.Port;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020040import org.onosproject.net.TributarySlot;
41import org.onosproject.net.behaviour.TributarySlotQuery;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070042import org.onosproject.net.device.DeviceService;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020043import org.onosproject.net.driver.Driver;
44import org.onosproject.net.driver.DriverService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070045import org.onosproject.net.flow.DefaultFlowRule;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.FlowRule;
49import org.onosproject.net.flow.TrafficSelector;
50import org.onosproject.net.flow.TrafficTreatment;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020051import org.onosproject.net.flow.criteria.Criteria;
52import org.onosproject.net.flow.instructions.Instructions;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070053import org.onosproject.net.intent.FlowRuleIntent;
54import org.onosproject.net.intent.Intent;
55import org.onosproject.net.intent.IntentCompiler;
56import org.onosproject.net.intent.IntentExtensionService;
57import org.onosproject.net.intent.IntentId;
58import org.onosproject.net.intent.IntentService;
59import org.onosproject.net.intent.OpticalCircuitIntent;
60import org.onosproject.net.intent.OpticalConnectivityIntent;
61import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070062import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080063import org.onosproject.net.newresource.Resource;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070064import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080065import org.onosproject.net.newresource.Resources;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080066import org.onosproject.net.intent.IntentSetMultimap;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070067import org.osgi.service.component.ComponentContext;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070068import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020071import com.google.common.collect.ImmutableList;
72import com.google.common.collect.Sets;
73
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074import java.util.Collections;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070075import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076import java.util.LinkedList;
77import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070078import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070079import java.util.Set;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020080import java.util.stream.Collectors;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020081import java.util.stream.Stream;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070082
83import static com.google.common.base.Preconditions.checkArgument;
84
85/**
86 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
87 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070088@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070089public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
90
91 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
92
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070093 private static final int DEFAULT_MAX_CAPACITY = 10;
94
95 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
96 label = "Maximum utilization of an optical connection.")
97
98 private int maxCapacity = DEFAULT_MAX_CAPACITY;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected ComponentConfigService cfgService;
102
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected IntentExtensionService intentManager;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected CoreService coreService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected DeviceService deviceService;
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700113 protected ResourceService resourceService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700116 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
119 protected IntentService intentService;
120
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected DriverService driverService;
123
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700124 private ApplicationId appId;
125
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700126 @Modified
127 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200128 if (context == null) {
129 return;
130 }
131
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700132 Dictionary properties = context.getProperties();
133
134 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
135 String propertyString = Tools.get(properties, "maxCapacity");
136
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800137 //Ignore if propertyString is empty or null
138 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700139 try {
140 int temp = Integer.parseInt(propertyString);
141 //Ensure value is non-negative but allow zero as a way to shutdown the link
142 if (temp >= 0) {
143 maxCapacity = temp;
144 }
145 } catch (NumberFormatException e) {
146 //Malformed arguments lead to no change of value (user should be notified of error)
147 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
148 }
149 } else {
150 //Notify of empty value but do not return (other properties will also go in this function)
151 log.error("The value for maxCapacity was set to an empty value.");
152 }
153
154 }
155
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700156 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700157 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700158 appId = coreService.registerApplication("org.onosproject.net.intent");
159 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700160 cfgService.registerProperties(getClass());
161 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700162 }
163
164 @Deactivate
165 public void deactivate() {
166 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700167 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700168 }
169
170 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800171 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700172 // Check if ports are OduClt ports
173 ConnectPoint src = intent.getSrc();
174 ConnectPoint dst = intent.getDst();
175 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
176 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
177 checkArgument(srcPort instanceof OduCltPort);
178 checkArgument(dstPort instanceof OduCltPort);
179
180 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
181
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200182 // Release of intent resources here is only a temporary solution for handling the
183 // case of recompiling due to intent restoration (when intent state is FAILED).
184 // TODO: try to release intent resources in IntentManager.
185 resourceService.release(intent.id());
186
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200187 // Check OduClt ports availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800188 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
189 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200190 // If ports are not available, compilation fails
191 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
192 throw new IntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700193 }
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200194 List<Resource> ports = ImmutableList.of(srcPortResource, dstPortResource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700195
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200196 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800197 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
198 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200199
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800200 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
201 intent.getSignalType(), multiplexingSupported);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800202
203 if (connIntent != null && !multiplexingSupported) {
204 return compile(intent, src, dst, Optional.of(connIntent), ports, false);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200205 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700206
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200207 // Create optical connectivity intent if needed - no optical intent or not enough slots available
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800208 if (connIntent == null) {
209 return compile(intent, src, dst, Optional.empty(), ports, multiplexingSupported);
210 }
211
212 List<Resource> slots = availableSlotResources(connIntent.getSrc(), connIntent.getDst(),
213 intent.getSignalType());
214 if (slots.isEmpty()) {
215 return compile(intent, src, dst, Optional.empty(), ports, true);
216 }
217
218 return compile(intent, src, dst, Optional.of(connIntent), ImmutableList.<Resource>builder()
219 .addAll(ports).addAll(slots).build(), false);
220
221 }
222
223 private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst,
224 Optional<OpticalConnectivityIntent> existingConnectivity,
225 List<Resource> resources, boolean supportsMultiplexing) {
226 OpticalConnectivityIntent connectivityIntent;
227 List<Resource> required;
228 if (existingConnectivity.isPresent()) {
229 connectivityIntent = existingConnectivity.get();
230 required = resources;
231 } else {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700232 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800233 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700234
235 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200236 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
237 }
238
239 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
240 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
241
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700242 // Create optical connectivity intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800243 connectivityIntent = OpticalConnectivityIntent.builder()
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700244 .appId(appId)
245 .src(srcCP)
246 .dst(dstCP)
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800247 .signalType(ochPorts.getLeft().signalType())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700248 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700249 .build();
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800250
251 if (!supportsMultiplexing) {
252 required = resources;
253 } else {
254 List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
255 if (slots.isEmpty()) {
256 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
257 }
258 required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
259 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700260 }
261
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800262 if (resourceService.allocate(intent.id(), required).isEmpty()) {
263 throw new IntentCompilationException("Unable to allocate resources for intent " + intent
264 + ": resources=" + required);
265 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700266
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800267 intentService.submit(connectivityIntent);
268
269 // Save circuit to connectivity intent mapping
270 intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
271
272 FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().
273 flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class)))
274 .collect(Collectors.toSet()));
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800275 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700276 }
277
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800278 private List<Resource> availableSlotResources(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
279 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(signalType);
280 int requestedTsNum = oduSignalType.tributarySlots();
281 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(src, dst);
282 if (commonTributarySlots.isEmpty()) {
283 return Collections.emptyList();
284 }
285 if (commonTributarySlots.size() < requestedTsNum) {
286 return Collections.emptyList();
287 }
288
289 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
290 .limit(requestedTsNum)
291 .collect(Collectors.toSet());
292
293 final List<ConnectPoint> portsList = ImmutableList.of(src, dst);
294 List<Resource> tributarySlotResources = portsList.stream()
295 .flatMap(cp -> tributarySlots
296 .stream()
297 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
298 .collect(Collectors.toList());
299
300 if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
301 log.debug("Resource allocation for {} on {} and {} failed (resource request: {})",
302 signalType, src, dst, tributarySlotResources);
303 return Collections.emptyList();
304 }
305 return tributarySlotResources;
306 }
307
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800308 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
309 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
310 // Create optical circuit intent
311 List<FlowRule> rules = new LinkedList<>();
312 // at the source: ODUCLT port mapping to OCH port
313 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
314 // at the destination: OCH port mapping to ODUCLT port
315 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
316
317 // Create flow rules for reverse path
318 if (higherIntent.isBidirectional()) {
319 // at the destination: OCH port mapping to ODUCLT port
320 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
321 // at the source: ODUCLT port mapping to OCH port
322 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
323 }
324
325 return new FlowRuleIntent(appId, rules, higherIntent.resources());
326 }
327
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700328 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700329 * Returns existing and available optical connectivity intent that matches the given circuit intent.
330 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800331 * @param src source connect point of optical circuit intent
332 * @param dst destination connect point of optical circuit intent
333 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200334 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700335 * @return existing optical connectivity intent, null otherwise.
336 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800337 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
338 ConnectPoint dst,
339 CltSignalType signalType,
340 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200341
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800342 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200343
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800344 return Tools.stream(intentService.getIntents())
345 .filter(x -> x instanceof OpticalConnectivityIntent)
346 .map(x -> (OpticalConnectivityIntent) x)
347 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
348 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
349 .filter(x -> isAllowed(src, x.getSrc()))
350 .filter(x -> isAllowed(dst, x.getDst()))
351 .filter(x -> isAvailable(x.id()))
352 .filter(x -> !multiplexingSupported ||
353 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
354 .findFirst()
355 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700356 }
357
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800358 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
359 ConnectPoint staticPort = staticPort(circuitCp);
360 return staticPort == null || staticPort.equals(connectivityCp);
361 }
362
363 /**
364 * Checks if current allocations on given resource can satisfy request.
365 * If the resource is null, return true.
366 *
367 * @param resource the resource on which to map the intent
368 * @return true if the resource can accept the request, false otherwise
369 */
370 private boolean isAvailable(IntentId resource) {
371 if (resource == null) {
372 return true;
373 }
374
375 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
376
377 if (mapping == null) {
378 return true;
379 }
380
381 return mapping.size() < maxCapacity;
382 }
383
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800384 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
385 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200386 if (common.isEmpty()) {
387 log.debug("No available TributarySlots");
388 return false;
389 }
390 if (common.size() < requestedTsNum) {
391 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
392 return false;
393 }
394 return true;
395 }
396
Marc De Leenheer723f5532015-06-03 20:16:17 -0700397 private ConnectPoint staticPort(ConnectPoint connectPoint) {
398 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
399
400 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
401
402 // FIXME: need a better way to match the port
403 if (staticPort != null) {
404 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
405 if (staticPort.equals(p.number().name())) {
406 return new ConnectPoint(p.element().id(), p.number());
407 }
408 }
409 }
410
411 return null;
412 }
413
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800414 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
415 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
416 switch (signalType) {
417 case CLT_1GBE:
418 case CLT_10GBE:
419 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
420 return findPorts(src, dst, OduSignalType.ODU2)
421 .orElse(findPorts(src, dst, OduSignalType.ODU4).orElse(null));
422 case CLT_100GBE:
423 return findPorts(src, dst, OduSignalType.ODU4).orElse(null);
424 case CLT_40GBE:
425 default:
426 return null;
427 }
428 }
429
430 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
431 OduSignalType ochPortSignalType) {
432 return findAvailableOchPort(src, ochPortSignalType)
433 .flatMap(srcOch ->
434 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
435 }
436
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800437 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700438 // First see if the port mappings are constrained
439 ConnectPoint ochCP = staticPort(oduPort);
440
441 if (ochCP != null) {
442 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700443 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800444 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800445 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700446 .map(ResourceAllocation::consumer)
447 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800448 .map(x -> (IntentId) x)
449 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700450
451 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800452 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700453 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800454 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700455 }
456
457 // No port constraints, so find any port that works
458 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700459
460 for (Port port : ports) {
461 if (!(port instanceof OchPort)) {
462 continue;
463 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200464 // This should be the first allocation on the OCH port
465 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
466 continue;
467 }
468 // OchPort is required to have the requested oduSignalType
469 if (((OchPort) port).signalType() != ochPortSignalType) {
470 continue;
471 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700472
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700473 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800474 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800475 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700476 .map(ResourceAllocation::consumer)
477 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800478 .map(x -> (IntentId) x)
479 .findAny();
480
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700481 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800482 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700483 }
484 }
485
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800486 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700487 }
488
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700489 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700490 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700491 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700492 * @param src source port
493 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200494 * @param priority
495 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700496 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700497 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200498 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700499 checkArgument(src.deviceId().equals(dst.deviceId()));
500
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700501 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
502 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
503
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700504 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200505 if (!slots.isEmpty()) {
506 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
507 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
508 OduSignalType oduCltPortOduSignalType;
509 OduSignalType ochPortOduSignalType;
510
511 if (srcPort instanceof OduCltPort) {
512 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
513 ochPortOduSignalType = ((OchPort) dstPort).signalType();
514
515 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
516 // use Instruction of OduSignalId only in case of ODU Multiplexing
517 if (oduCltPortOduSignalType != ochPortOduSignalType) {
518 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
519 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
520 }
521 } else { // srcPort is OchPort
522 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
523 ochPortOduSignalType = ((OchPort) srcPort).signalType();
524
525 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
526 // use Criteria of OduSignalId only in case of ODU Multiplexing
527 if (oduCltPortOduSignalType != ochPortOduSignalType) {
528 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
529 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
530 }
531 }
532 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700533 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700534
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700535 FlowRule flowRule = DefaultFlowRule.builder()
536 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700537 .withSelector(selectorBuilder.build())
538 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400539 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700540 .fromApp(appId)
541 .makePermanent()
542 .build();
543
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700544 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700545 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200546
Sho SHIMIZU6953cf12016-02-29 13:58:13 -0800547 OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200548 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
549 int tributarySlotLen = ochPortSignalType.tributarySlots();
550 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
551
552 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
553 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
554 }
555
556 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
557 return (int) tributarySlots.stream().findFirst().get().index();
558 }
559
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800560 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800561 Driver driver = driverService.getDriver(cp.deviceId());
562 return driver != null
563 && driver.hasBehaviour(TributarySlotQuery.class)
564 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200565 }
566
567 /**
568 * Maps from Intent's OduClt SignalType to OduSignalType.
569 *
570 * @param cltSignalType OduClt port signal type
571 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
572 */
573 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200574 switch (cltSignalType) {
575 case CLT_1GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800576 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200577 case CLT_10GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800578 return OduSignalType.ODU2;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200579 case CLT_40GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800580 return OduSignalType.ODU3;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200581 case CLT_100GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800582 return OduSignalType.ODU4;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200583 default:
584 log.error("Unsupported CltSignalType {}", cltSignalType);
Sho SHIMIZU88799912016-02-29 13:34:12 -0800585 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200586 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200587 }
588
589 /**
590 * Finds the common TributarySlots available on the two connect points.
591 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800592 * @param src source connect point
593 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200594 * @return set of common TributarySlots on both connect points
595 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800596 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
597 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
598 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200599 return Sets.intersection(forward, backward);
600 }
601
602 /**
603 * Finds the TributarySlots available on the connect point.
604 *
605 * @param cp connect point
606 * @return set of TributarySlots available on the connect point
607 */
608 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800609 return resourceService.getAvailableResourceValues(
610 Resources.discrete(cp.deviceId(), cp.port()).id(),
611 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200612 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700613}