blob: 603cda54439d813d89f54b1ad4fd2666b754d2f1 [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
18import org.apache.commons.lang3.tuple.Pair;
19import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070020import org.apache.felix.scr.annotations.Component;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070021import org.apache.felix.scr.annotations.Deactivate;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070026import org.onlab.util.Tools;
27import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070030import org.onosproject.net.AnnotationKeys;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020031import org.onosproject.net.CltSignalType;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070032import org.onosproject.net.ConnectPoint;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020033import org.onosproject.net.DeviceId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070034import org.onosproject.net.OchPort;
35import org.onosproject.net.OduCltPort;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020036import org.onosproject.net.OduSignalId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070037import org.onosproject.net.OduSignalType;
38import org.onosproject.net.Port;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020039import org.onosproject.net.TributarySlot;
40import org.onosproject.net.behaviour.TributarySlotQuery;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070041import org.onosproject.net.device.DeviceService;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020042import org.onosproject.net.driver.Driver;
43import org.onosproject.net.driver.DriverService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070044import org.onosproject.net.flow.DefaultFlowRule;
45import org.onosproject.net.flow.DefaultTrafficSelector;
46import org.onosproject.net.flow.DefaultTrafficTreatment;
47import org.onosproject.net.flow.FlowRule;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020050import org.onosproject.net.flow.criteria.Criteria;
51import org.onosproject.net.flow.instructions.Instructions;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070052import org.onosproject.net.intent.FlowRuleIntent;
53import org.onosproject.net.intent.Intent;
54import org.onosproject.net.intent.IntentCompiler;
55import org.onosproject.net.intent.IntentExtensionService;
56import org.onosproject.net.intent.IntentId;
57import org.onosproject.net.intent.IntentService;
58import org.onosproject.net.intent.OpticalCircuitIntent;
59import org.onosproject.net.intent.OpticalConnectivityIntent;
60import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070061import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080062import org.onosproject.net.newresource.Resource;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070063import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080064import org.onosproject.net.newresource.Resources;
Sho SHIMIZU7d20af12015-10-01 16:03:51 -070065import org.onosproject.net.resource.device.IntentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070066import org.onosproject.net.resource.link.LinkResourceAllocations;
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;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070081
82import static com.google.common.base.Preconditions.checkArgument;
83
84/**
85 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
86 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070087@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070088public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
89
90 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
91
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070092 private static final int DEFAULT_MAX_CAPACITY = 10;
93
94 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
95 label = "Maximum utilization of an optical connection.")
96
97 private int maxCapacity = DEFAULT_MAX_CAPACITY;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected ComponentConfigService cfgService;
101
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected IntentExtensionService intentManager;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected CoreService coreService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected DeviceService deviceService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700112 protected ResourceService resourceService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700115 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected IntentService intentService;
119
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected DriverService driverService;
122
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700123 private ApplicationId appId;
124
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700125 @Modified
126 public void modified(ComponentContext context) {
127 Dictionary properties = context.getProperties();
128
129 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
130 String propertyString = Tools.get(properties, "maxCapacity");
131
132 //Ignore if propertyString is empty
133 if (!propertyString.isEmpty()) {
134 try {
135 int temp = Integer.parseInt(propertyString);
136 //Ensure value is non-negative but allow zero as a way to shutdown the link
137 if (temp >= 0) {
138 maxCapacity = temp;
139 }
140 } catch (NumberFormatException e) {
141 //Malformed arguments lead to no change of value (user should be notified of error)
142 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
143 }
144 } else {
145 //Notify of empty value but do not return (other properties will also go in this function)
146 log.error("The value for maxCapacity was set to an empty value.");
147 }
148
149 }
150
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700151 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700152 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700153 appId = coreService.registerApplication("org.onosproject.net.intent");
154 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700155 cfgService.registerProperties(getClass());
156 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700157 }
158
159 @Deactivate
160 public void deactivate() {
161 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700162 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700163 }
164
165 @Override
166 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
167 Set<LinkResourceAllocations> resources) {
168 // Check if ports are OduClt ports
169 ConnectPoint src = intent.getSrc();
170 ConnectPoint dst = intent.getDst();
171 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
172 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
173 checkArgument(srcPort instanceof OduCltPort);
174 checkArgument(dstPort instanceof OduCltPort);
175
176 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
177
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200178 // Release of intent resources here is only a temporary solution for handling the
179 // case of recompiling due to intent restoration (when intent state is FAILED).
180 // TODO: try to release intent resources in IntentManager.
181 resourceService.release(intent.id());
182
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700183 // Reserve OduClt ports
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800184 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
185 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800186 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700187 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700188 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
189 }
190
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200191 // Check if both devices support multiplexing (usage of TributarySlots)
192 boolean multiplexingSupported = isMultiplexingSupported(intent);
193
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700194 LinkedList<Intent> intents = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200195 // slots are used only for devices supporting multiplexing
196 Set<TributarySlot> slots = Collections.emptySet();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700197
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200198 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent, multiplexingSupported);
199 if ((connIntent != null) && multiplexingSupported) {
200 // Allocate TributarySlots on existing OCH ports
201 slots = assignTributarySlots(intent, Pair.of(connIntent.getSrc(), connIntent.getDst()));
202 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700203
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200204 // Create optical connectivity intent if needed - no optical intent or not enough slots available
205 if (connIntent == null || (multiplexingSupported && slots.isEmpty())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700206 // Find OCh ports with available resources
207 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
208
209 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200210 // Release port allocations if unsuccessful
211 resourceService.release(intent.id());
212 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
213 }
214
215 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
216 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
217
218 if (multiplexingSupported) {
219 // Allocate TributarySlots on OCH ports
220 slots = assignTributarySlots(intent, Pair.of(srcCP, dstCP));
221 if (slots.isEmpty()) {
222 // Release port allocations if unsuccessful
223 resourceService.release(intent.id());
224 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
225 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700226 }
227
228 // Create optical connectivity intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200229 OduSignalType signalType = ochPorts.getLeft().signalType();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700230 connIntent = OpticalConnectivityIntent.builder()
231 .appId(appId)
232 .src(srcCP)
233 .dst(dstCP)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200234 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700235 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700236 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700237 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700238 }
239
240 // Create optical circuit intent
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700241 List<FlowRule> rules = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200242 // at the source: ODUCLT port mapping to OCH port
243 rules.add(connectPorts(src, connIntent.getSrc(), intent.priority(), slots));
244 // at the destination: OCH port mapping to ODUCLT port
245 rules.add(connectPorts(connIntent.getDst(), dst, intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700246
247 // Create flow rules for reverse path
248 if (intent.isBidirectional()) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200249 // at the destination: OCH port mapping to ODUCLT port
250 rules.add(connectPorts(connIntent.getSrc(), src, intent.priority(), slots));
251 // at the source: ODUCLT port mapping to OCH port
252 rules.add(connectPorts(dst, connIntent.getDst(), intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700253 }
254
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200255 FlowRuleIntent circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700256
257 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700258 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700259 intents.add(circuitIntent);
260
261 return intents;
262 }
263
264 /**
265 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700266 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700267 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700268 * @param resource the resource on which to map the intent
269 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700270 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700271 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700272 if (resource == null) {
273 return true;
274 }
275
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700276 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700277
Marc De Leenheer723f5532015-06-03 20:16:17 -0700278 if (mapping == null) {
279 return true;
280 }
281
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700282 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700283 }
284
285 private boolean isAllowed(OpticalCircuitIntent circuitIntent, OpticalConnectivityIntent connIntent) {
286 ConnectPoint srcStaticPort = staticPort(circuitIntent.getSrc());
287 if (srcStaticPort != null) {
288 if (!srcStaticPort.equals(connIntent.getSrc())) {
289 return false;
290 }
291 }
292
293 ConnectPoint dstStaticPort = staticPort(circuitIntent.getDst());
294 if (dstStaticPort != null) {
295 if (!dstStaticPort.equals(connIntent.getDst())) {
296 return false;
297 }
298 }
299
300 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700301 }
302
303 /**
304 * Returns existing and available optical connectivity intent that matches the given circuit intent.
305 *
306 * @param circuitIntent optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200307 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700308 * @return existing optical connectivity intent, null otherwise.
309 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200310 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent,
311 boolean multiplexingSupported) {
312
313 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(circuitIntent.getSignalType());
314
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700315 for (Intent intent : intentService.getIntents()) {
316 if (!(intent instanceof OpticalConnectivityIntent)) {
317 continue;
318 }
319
320 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
321
322 ConnectPoint src = circuitIntent.getSrc();
323 ConnectPoint dst = circuitIntent.getDst();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700324 // Ignore if the intents don't have identical src and dst devices
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200325 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) ||
Marc De Leenheer723f5532015-06-03 20:16:17 -0700326 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
327 continue;
328 }
329
330 if (!isAllowed(circuitIntent, connIntent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700331 continue;
332 }
333
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200334 if (!isAvailable(connIntent.id())) {
335 continue;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700336 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200337
338 if (multiplexingSupported) {
339 if (!isAvailableTributarySlots(connIntent, oduSignalType.tributarySlots())) {
340 continue;
341 }
342 }
343
344 return connIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700345 }
346
347 return null;
348 }
349
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200350 private boolean isAvailableTributarySlots(OpticalConnectivityIntent connIntent, int requestedTsNum) {
351 Set<TributarySlot> common = findCommonTributarySlotsOnCps(connIntent.getSrc(), connIntent.getDst());
352 if (common.isEmpty()) {
353 log.debug("No available TributarySlots");
354 return false;
355 }
356 if (common.size() < requestedTsNum) {
357 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
358 return false;
359 }
360 return true;
361 }
362
363 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
364 Pair<ConnectPoint, ConnectPoint> ports) {
365
366 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
367 int requestedTsNum = oduSignalType.tributarySlots();
368 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
369 if (commonTributarySlots.isEmpty()) {
370 return Collections.emptySet();
371 }
372 if (commonTributarySlots.size() < requestedTsNum) {
373 return Collections.emptySet();
374 }
375
376 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
377 .limit(requestedTsNum)
378 .collect(Collectors.toSet());
379
380 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
381 List<Resource> tributarySlotResources = portsList.stream()
382 .flatMap(cp -> tributarySlots
383 .stream()
384 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
385 .collect(Collectors.toList());
386
387 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
388 if (allocations.isEmpty()) {
389 log.debug("Resource allocation for {} failed (resource request: {})",
390 intent, tributarySlotResources);
391 return Collections.emptySet();
392 }
393 return tributarySlots;
394 }
395
Marc De Leenheer723f5532015-06-03 20:16:17 -0700396 private ConnectPoint staticPort(ConnectPoint connectPoint) {
397 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
398
399 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
400
401 // FIXME: need a better way to match the port
402 if (staticPort != null) {
403 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
404 if (staticPort.equals(p.number().name())) {
405 return new ConnectPoint(p.element().id(), p.number());
406 }
407 }
408 }
409
410 return null;
411 }
412
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200413 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700414 // First see if the port mappings are constrained
415 ConnectPoint ochCP = staticPort(oduPort);
416
417 if (ochCP != null) {
418 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700419 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800420 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800421 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700422 .map(ResourceAllocation::consumer)
423 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800424 .map(x -> (IntentId) x)
425 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700426
427 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700428 return ochPort;
429 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200430 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700431 }
432
433 // No port constraints, so find any port that works
434 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700435
436 for (Port port : ports) {
437 if (!(port instanceof OchPort)) {
438 continue;
439 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200440 // This should be the first allocation on the OCH port
441 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
442 continue;
443 }
444 // OchPort is required to have the requested oduSignalType
445 if (((OchPort) port).signalType() != ochPortSignalType) {
446 continue;
447 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700448
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700449 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800450 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800451 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700452 .map(ResourceAllocation::consumer)
453 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800454 .map(x -> (IntentId) x)
455 .findAny();
456
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700457 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700458 return (OchPort) port;
459 }
460 }
461
462 return null;
463 }
464
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700465 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200466 Pair<OchPort, OchPort> ochPorts = null;
467 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
468 switch (intent.getSignalType()) {
469 case CLT_1GBE:
470 case CLT_10GBE:
471 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
472 ochPorts = findPorts(intent, OduSignalType.ODU2);
473 if (ochPorts == null) {
474 ochPorts = findPorts(intent, OduSignalType.ODU4);
475 }
476 break;
477 case CLT_100GBE:
478 ochPorts = findPorts(intent, OduSignalType.ODU4);
479 break;
480 case CLT_40GBE:
481 default:
482 break;
483 }
484 return ochPorts;
485 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700486
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200487 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent, OduSignalType ochPortSignalType) {
488 OchPort srcPort = findAvailableOchPort(intent.getSrc(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700489 if (srcPort == null) {
490 return null;
491 }
492
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200493 OchPort dstPort = findAvailableOchPort(intent.getDst(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700494 if (dstPort == null) {
495 return null;
496 }
497
498 return Pair.of(srcPort, dstPort);
499 }
500
501 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700502 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700503 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700504 * @param src source port
505 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200506 * @param priority
507 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700508 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700509 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200510 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700511 checkArgument(src.deviceId().equals(dst.deviceId()));
512
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700513 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
514 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
515
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700516 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200517 if (!slots.isEmpty()) {
518 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
519 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
520 OduSignalType oduCltPortOduSignalType;
521 OduSignalType ochPortOduSignalType;
522
523 if (srcPort instanceof OduCltPort) {
524 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
525 ochPortOduSignalType = ((OchPort) dstPort).signalType();
526
527 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
528 // use Instruction of OduSignalId only in case of ODU Multiplexing
529 if (oduCltPortOduSignalType != ochPortOduSignalType) {
530 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
531 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
532 }
533 } else { // srcPort is OchPort
534 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
535 ochPortOduSignalType = ((OchPort) srcPort).signalType();
536
537 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
538 // use Criteria of OduSignalId only in case of ODU Multiplexing
539 if (oduCltPortOduSignalType != ochPortOduSignalType) {
540 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
541 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
542 }
543 }
544 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700545 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700546
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700547 FlowRule flowRule = DefaultFlowRule.builder()
548 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700549 .withSelector(selectorBuilder.build())
550 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400551 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700552 .fromApp(appId)
553 .makePermanent()
554 .build();
555
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700556 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700557 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200558
559 private OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
560 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
561 int tributarySlotLen = ochPortSignalType.tributarySlots();
562 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
563
564 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
565 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
566 }
567
568 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
569 return (int) tributarySlots.stream().findFirst().get().index();
570 }
571
572 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
573 Driver driver = driverService.getDriver(deviceId);
574 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
575 }
576
577 private boolean isMultiplexingSupported(OpticalCircuitIntent intent) {
578 ConnectPoint src = intent.getSrc();
579 ConnectPoint dst = intent.getDst();
580
581 if (!isTributarySlotBehaviourSupported(src.deviceId()) ||
582 !isTributarySlotBehaviourSupported(dst.deviceId())) {
583 return false;
584 }
585
586 ConnectPoint srcStaticPort = staticPort(src);
587 if (srcStaticPort != null) {
588 return false;
589 }
590 ConnectPoint dstStaticPort = staticPort(dst);
591 if (dstStaticPort != null) {
592 return false;
593 }
594
595 return true;
596 }
597
598 /**
599 * Maps from Intent's OduClt SignalType to OduSignalType.
600 *
601 * @param cltSignalType OduClt port signal type
602 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
603 */
604 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
605 OduSignalType oduSignalType = OduSignalType.ODU0;
606 switch (cltSignalType) {
607 case CLT_1GBE:
608 oduSignalType = OduSignalType.ODU0;
609 break;
610 case CLT_10GBE:
611 oduSignalType = OduSignalType.ODU2;
612 break;
613 case CLT_40GBE:
614 oduSignalType = OduSignalType.ODU3;
615 break;
616 case CLT_100GBE:
617 oduSignalType = OduSignalType.ODU4;
618 break;
619 default:
620 log.error("Unsupported CltSignalType {}", cltSignalType);
621 break;
622 }
623 return oduSignalType;
624 }
625
626 /**
627 * Finds the common TributarySlots available on the two connect points.
628 *
629 * @param srcCp source connect point
630 * @param dstCp dest connect point
631 * @return set of common TributarySlots on both connect points
632 */
633 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint srcCp, ConnectPoint dstCp) {
634 Set<TributarySlot> forward = findTributarySlotsOnCp(srcCp);
635 Set<TributarySlot> backward = findTributarySlotsOnCp(dstCp);
636 return Sets.intersection(forward, backward);
637 }
638
639 /**
640 * Finds the TributarySlots available on the connect point.
641 *
642 * @param cp connect point
643 * @return set of TributarySlots available on the connect point
644 */
645 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
646 return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id())
647 .stream()
648 .filter(x -> x.last() instanceof TributarySlot)
649 .map(x -> (TributarySlot) x.last())
650 .collect(Collectors.toSet());
651 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700652}