blob: 7698a74c117237971751f18859518b7ccab9651e [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
Thomas Vachuska762c1bb2016-02-19 12:54:31 -080018import com.google.common.collect.ImmutableSet;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070019import org.apache.commons.lang3.tuple.Pair;
20import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070021import org.apache.felix.scr.annotations.Component;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070022import org.apache.felix.scr.annotations.Deactivate;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070023import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070027import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070031import org.onosproject.net.AnnotationKeys;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020032import org.onosproject.net.CltSignalType;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070033import org.onosproject.net.ConnectPoint;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020034import org.onosproject.net.DeviceId;
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 SHIMIZU7d20af12015-10-01 16:03:51 -070066import org.onosproject.net.resource.device.IntentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070067import org.onosproject.net.resource.link.LinkResourceAllocations;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070068import org.osgi.service.component.ComponentContext;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070069import org.slf4j.Logger;
70import org.slf4j.LoggerFactory;
71
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020072import com.google.common.collect.ImmutableList;
73import com.google.common.collect.Sets;
74
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070075import java.util.Collections;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070076import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070077import java.util.LinkedList;
78import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070079import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070080import java.util.Set;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020081import java.util.stream.Collectors;
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) {
128 Dictionary properties = context.getProperties();
129
130 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
131 String propertyString = Tools.get(properties, "maxCapacity");
132
133 //Ignore if propertyString is empty
134 if (!propertyString.isEmpty()) {
135 try {
136 int temp = Integer.parseInt(propertyString);
137 //Ensure value is non-negative but allow zero as a way to shutdown the link
138 if (temp >= 0) {
139 maxCapacity = temp;
140 }
141 } catch (NumberFormatException e) {
142 //Malformed arguments lead to no change of value (user should be notified of error)
143 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
144 }
145 } else {
146 //Notify of empty value but do not return (other properties will also go in this function)
147 log.error("The value for maxCapacity was set to an empty value.");
148 }
149
150 }
151
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700152 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700153 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700154 appId = coreService.registerApplication("org.onosproject.net.intent");
155 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700156 cfgService.registerProperties(getClass());
157 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700158 }
159
160 @Deactivate
161 public void deactivate() {
162 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700163 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700164 }
165
166 @Override
167 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
168 Set<LinkResourceAllocations> resources) {
169 // Check if ports are OduClt ports
170 ConnectPoint src = intent.getSrc();
171 ConnectPoint dst = intent.getDst();
172 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
173 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
174 checkArgument(srcPort instanceof OduCltPort);
175 checkArgument(dstPort instanceof OduCltPort);
176
177 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
178
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200179 // Release of intent resources here is only a temporary solution for handling the
180 // case of recompiling due to intent restoration (when intent state is FAILED).
181 // TODO: try to release intent resources in IntentManager.
182 resourceService.release(intent.id());
183
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700184 // Reserve OduClt ports
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800185 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
186 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800187 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700188 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700189 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
190 }
191
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200192 // Check if both devices support multiplexing (usage of TributarySlots)
193 boolean multiplexingSupported = isMultiplexingSupported(intent);
194
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700195 LinkedList<Intent> intents = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200196 // slots are used only for devices supporting multiplexing
197 Set<TributarySlot> slots = Collections.emptySet();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700198
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200199 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent, multiplexingSupported);
200 if ((connIntent != null) && multiplexingSupported) {
201 // Allocate TributarySlots on existing OCH ports
202 slots = assignTributarySlots(intent, Pair.of(connIntent.getSrc(), connIntent.getDst()));
203 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700204
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200205 // Create optical connectivity intent if needed - no optical intent or not enough slots available
206 if (connIntent == null || (multiplexingSupported && slots.isEmpty())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700207 // Find OCh ports with available resources
208 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
209
210 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200211 // Release port allocations if unsuccessful
212 resourceService.release(intent.id());
213 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
214 }
215
216 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
217 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
218
219 if (multiplexingSupported) {
220 // Allocate TributarySlots on OCH ports
221 slots = assignTributarySlots(intent, Pair.of(srcCP, dstCP));
222 if (slots.isEmpty()) {
223 // Release port allocations if unsuccessful
224 resourceService.release(intent.id());
225 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
226 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700227 }
228
229 // Create optical connectivity intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200230 OduSignalType signalType = ochPorts.getLeft().signalType();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700231 connIntent = OpticalConnectivityIntent.builder()
232 .appId(appId)
233 .src(srcCP)
234 .dst(dstCP)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200235 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700236 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700237 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700238 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700239 }
240
241 // Create optical circuit intent
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700242 List<FlowRule> rules = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200243 // at the source: ODUCLT port mapping to OCH port
244 rules.add(connectPorts(src, connIntent.getSrc(), intent.priority(), slots));
245 // at the destination: OCH port mapping to ODUCLT port
246 rules.add(connectPorts(connIntent.getDst(), dst, intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700247
248 // Create flow rules for reverse path
249 if (intent.isBidirectional()) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200250 // at the destination: OCH port mapping to ODUCLT port
251 rules.add(connectPorts(connIntent.getSrc(), src, intent.priority(), slots));
252 // at the source: ODUCLT port mapping to OCH port
253 rules.add(connectPorts(dst, connIntent.getDst(), intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700254 }
255
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200256 FlowRuleIntent circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700257
258 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700259 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700260 intents.add(circuitIntent);
261
262 return intents;
263 }
264
265 /**
266 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700267 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700268 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700269 * @param resource the resource on which to map the intent
270 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700271 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700272 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700273 if (resource == null) {
274 return true;
275 }
276
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700277 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700278
Marc De Leenheer723f5532015-06-03 20:16:17 -0700279 if (mapping == null) {
280 return true;
281 }
282
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700283 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700284 }
285
286 private boolean isAllowed(OpticalCircuitIntent circuitIntent, OpticalConnectivityIntent connIntent) {
287 ConnectPoint srcStaticPort = staticPort(circuitIntent.getSrc());
288 if (srcStaticPort != null) {
289 if (!srcStaticPort.equals(connIntent.getSrc())) {
290 return false;
291 }
292 }
293
294 ConnectPoint dstStaticPort = staticPort(circuitIntent.getDst());
295 if (dstStaticPort != null) {
296 if (!dstStaticPort.equals(connIntent.getDst())) {
297 return false;
298 }
299 }
300
301 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700302 }
303
304 /**
305 * Returns existing and available optical connectivity intent that matches the given circuit intent.
306 *
307 * @param circuitIntent optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200308 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700309 * @return existing optical connectivity intent, null otherwise.
310 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200311 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent,
312 boolean multiplexingSupported) {
313
314 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(circuitIntent.getSignalType());
315
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700316 for (Intent intent : intentService.getIntents()) {
317 if (!(intent instanceof OpticalConnectivityIntent)) {
318 continue;
319 }
320
321 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
322
323 ConnectPoint src = circuitIntent.getSrc();
324 ConnectPoint dst = circuitIntent.getDst();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700325 // Ignore if the intents don't have identical src and dst devices
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200326 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) ||
Marc De Leenheer723f5532015-06-03 20:16:17 -0700327 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
328 continue;
329 }
330
331 if (!isAllowed(circuitIntent, connIntent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700332 continue;
333 }
334
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200335 if (!isAvailable(connIntent.id())) {
336 continue;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700337 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200338
339 if (multiplexingSupported) {
340 if (!isAvailableTributarySlots(connIntent, oduSignalType.tributarySlots())) {
341 continue;
342 }
343 }
344
345 return connIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700346 }
347
348 return null;
349 }
350
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200351 private boolean isAvailableTributarySlots(OpticalConnectivityIntent connIntent, int requestedTsNum) {
352 Set<TributarySlot> common = findCommonTributarySlotsOnCps(connIntent.getSrc(), connIntent.getDst());
353 if (common.isEmpty()) {
354 log.debug("No available TributarySlots");
355 return false;
356 }
357 if (common.size() < requestedTsNum) {
358 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
359 return false;
360 }
361 return true;
362 }
363
364 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
365 Pair<ConnectPoint, ConnectPoint> ports) {
366
367 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
368 int requestedTsNum = oduSignalType.tributarySlots();
369 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
370 if (commonTributarySlots.isEmpty()) {
371 return Collections.emptySet();
372 }
373 if (commonTributarySlots.size() < requestedTsNum) {
374 return Collections.emptySet();
375 }
376
377 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
378 .limit(requestedTsNum)
379 .collect(Collectors.toSet());
380
381 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
382 List<Resource> tributarySlotResources = portsList.stream()
383 .flatMap(cp -> tributarySlots
384 .stream()
385 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
386 .collect(Collectors.toList());
387
388 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
389 if (allocations.isEmpty()) {
390 log.debug("Resource allocation for {} failed (resource request: {})",
391 intent, tributarySlotResources);
392 return Collections.emptySet();
393 }
394 return tributarySlots;
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
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200414 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700415 // First see if the port mappings are constrained
416 ConnectPoint ochCP = staticPort(oduPort);
417
418 if (ochCP != null) {
419 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700420 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800421 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800422 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700423 .map(ResourceAllocation::consumer)
424 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800425 .map(x -> (IntentId) x)
426 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700427
428 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700429 return ochPort;
430 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200431 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700432 }
433
434 // No port constraints, so find any port that works
435 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700436
437 for (Port port : ports) {
438 if (!(port instanceof OchPort)) {
439 continue;
440 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200441 // This should be the first allocation on the OCH port
442 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
443 continue;
444 }
445 // OchPort is required to have the requested oduSignalType
446 if (((OchPort) port).signalType() != ochPortSignalType) {
447 continue;
448 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700449
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700450 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800451 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800452 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700453 .map(ResourceAllocation::consumer)
454 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800455 .map(x -> (IntentId) x)
456 .findAny();
457
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700458 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700459 return (OchPort) port;
460 }
461 }
462
463 return null;
464 }
465
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700466 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200467 Pair<OchPort, OchPort> ochPorts = null;
468 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
469 switch (intent.getSignalType()) {
470 case CLT_1GBE:
471 case CLT_10GBE:
472 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
473 ochPorts = findPorts(intent, OduSignalType.ODU2);
474 if (ochPorts == null) {
475 ochPorts = findPorts(intent, OduSignalType.ODU4);
476 }
477 break;
478 case CLT_100GBE:
479 ochPorts = findPorts(intent, OduSignalType.ODU4);
480 break;
481 case CLT_40GBE:
482 default:
483 break;
484 }
485 return ochPorts;
486 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700487
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200488 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent, OduSignalType ochPortSignalType) {
489 OchPort srcPort = findAvailableOchPort(intent.getSrc(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700490 if (srcPort == null) {
491 return null;
492 }
493
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200494 OchPort dstPort = findAvailableOchPort(intent.getDst(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700495 if (dstPort == null) {
496 return null;
497 }
498
499 return Pair.of(srcPort, dstPort);
500 }
501
502 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700503 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700504 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700505 * @param src source port
506 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200507 * @param priority
508 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700509 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700510 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200511 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700512 checkArgument(src.deviceId().equals(dst.deviceId()));
513
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700514 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
515 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
516
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700517 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200518 if (!slots.isEmpty()) {
519 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
520 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
521 OduSignalType oduCltPortOduSignalType;
522 OduSignalType ochPortOduSignalType;
523
524 if (srcPort instanceof OduCltPort) {
525 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
526 ochPortOduSignalType = ((OchPort) dstPort).signalType();
527
528 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
529 // use Instruction of OduSignalId only in case of ODU Multiplexing
530 if (oduCltPortOduSignalType != ochPortOduSignalType) {
531 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
532 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
533 }
534 } else { // srcPort is OchPort
535 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
536 ochPortOduSignalType = ((OchPort) srcPort).signalType();
537
538 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
539 // use Criteria of OduSignalId only in case of ODU Multiplexing
540 if (oduCltPortOduSignalType != ochPortOduSignalType) {
541 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
542 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
543 }
544 }
545 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700546 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700547
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700548 FlowRule flowRule = DefaultFlowRule.builder()
549 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700550 .withSelector(selectorBuilder.build())
551 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400552 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700553 .fromApp(appId)
554 .makePermanent()
555 .build();
556
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700557 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700558 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200559
560 private OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
561 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
562 int tributarySlotLen = ochPortSignalType.tributarySlots();
563 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
564
565 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
566 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
567 }
568
569 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
570 return (int) tributarySlots.stream().findFirst().get().index();
571 }
572
573 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
574 Driver driver = driverService.getDriver(deviceId);
575 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
576 }
577
578 private boolean isMultiplexingSupported(OpticalCircuitIntent intent) {
579 ConnectPoint src = intent.getSrc();
580 ConnectPoint dst = intent.getDst();
581
582 if (!isTributarySlotBehaviourSupported(src.deviceId()) ||
583 !isTributarySlotBehaviourSupported(dst.deviceId())) {
584 return false;
585 }
586
587 ConnectPoint srcStaticPort = staticPort(src);
588 if (srcStaticPort != null) {
589 return false;
590 }
591 ConnectPoint dstStaticPort = staticPort(dst);
592 if (dstStaticPort != null) {
593 return false;
594 }
595
596 return true;
597 }
598
599 /**
600 * Maps from Intent's OduClt SignalType to OduSignalType.
601 *
602 * @param cltSignalType OduClt port signal type
603 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
604 */
605 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
606 OduSignalType oduSignalType = OduSignalType.ODU0;
607 switch (cltSignalType) {
608 case CLT_1GBE:
609 oduSignalType = OduSignalType.ODU0;
610 break;
611 case CLT_10GBE:
612 oduSignalType = OduSignalType.ODU2;
613 break;
614 case CLT_40GBE:
615 oduSignalType = OduSignalType.ODU3;
616 break;
617 case CLT_100GBE:
618 oduSignalType = OduSignalType.ODU4;
619 break;
620 default:
621 log.error("Unsupported CltSignalType {}", cltSignalType);
622 break;
623 }
624 return oduSignalType;
625 }
626
627 /**
628 * Finds the common TributarySlots available on the two connect points.
629 *
630 * @param srcCp source connect point
631 * @param dstCp dest connect point
632 * @return set of common TributarySlots on both connect points
633 */
634 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint srcCp, ConnectPoint dstCp) {
635 Set<TributarySlot> forward = findTributarySlotsOnCp(srcCp);
636 Set<TributarySlot> backward = findTributarySlotsOnCp(dstCp);
637 return Sets.intersection(forward, backward);
638 }
639
640 /**
641 * Finds the TributarySlots available on the connect point.
642 *
643 * @param cp connect point
644 * @return set of TributarySlots available on the connect point
645 */
646 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Thomas Vachuska762c1bb2016-02-19 12:54:31 -0800647 return ImmutableSet.of(); // temporary fix for build
648 // FIXME: below changes break the build due to conflicting changes on Resource, which obsoleted Resource#last()
649// return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id())
650// .stream()
651// .filter(x -> x.last() instanceof TributarySlot)
652// .map(x -> (TributarySlot) x.last())
653// .collect(Collectors.toSet());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200654 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700655}