blob: 5ae1eb7010b6723f00679fb80973a8bd87893b6a [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) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200127 if (context == null) {
128 return;
129 }
130
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700131 Dictionary properties = context.getProperties();
132
133 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
134 String propertyString = Tools.get(properties, "maxCapacity");
135
136 //Ignore if propertyString is empty
137 if (!propertyString.isEmpty()) {
138 try {
139 int temp = Integer.parseInt(propertyString);
140 //Ensure value is non-negative but allow zero as a way to shutdown the link
141 if (temp >= 0) {
142 maxCapacity = temp;
143 }
144 } catch (NumberFormatException e) {
145 //Malformed arguments lead to no change of value (user should be notified of error)
146 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
147 }
148 } else {
149 //Notify of empty value but do not return (other properties will also go in this function)
150 log.error("The value for maxCapacity was set to an empty value.");
151 }
152
153 }
154
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700155 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700156 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700157 appId = coreService.registerApplication("org.onosproject.net.intent");
158 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700159 cfgService.registerProperties(getClass());
160 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700161 }
162
163 @Deactivate
164 public void deactivate() {
165 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700166 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700167 }
168
169 @Override
170 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
171 Set<LinkResourceAllocations> resources) {
172 // 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
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700187 // Reserve OduClt ports
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();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800190 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700191 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700192 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
193 }
194
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200195 // Check if both devices support multiplexing (usage of TributarySlots)
196 boolean multiplexingSupported = isMultiplexingSupported(intent);
197
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700198 LinkedList<Intent> intents = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200199 // slots are used only for devices supporting multiplexing
200 Set<TributarySlot> slots = Collections.emptySet();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700201
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200202 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent, multiplexingSupported);
203 if ((connIntent != null) && multiplexingSupported) {
204 // Allocate TributarySlots on existing OCH ports
205 slots = assignTributarySlots(intent, Pair.of(connIntent.getSrc(), connIntent.getDst()));
206 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700207
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200208 // Create optical connectivity intent if needed - no optical intent or not enough slots available
209 if (connIntent == null || (multiplexingSupported && slots.isEmpty())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700210 // Find OCh ports with available resources
211 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
212
213 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200214 // Release port allocations if unsuccessful
215 resourceService.release(intent.id());
216 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
217 }
218
219 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
220 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
221
222 if (multiplexingSupported) {
223 // Allocate TributarySlots on OCH ports
224 slots = assignTributarySlots(intent, Pair.of(srcCP, dstCP));
225 if (slots.isEmpty()) {
226 // Release port allocations if unsuccessful
227 resourceService.release(intent.id());
228 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
229 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700230 }
231
232 // Create optical connectivity intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200233 OduSignalType signalType = ochPorts.getLeft().signalType();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700234 connIntent = OpticalConnectivityIntent.builder()
235 .appId(appId)
236 .src(srcCP)
237 .dst(dstCP)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200238 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700239 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700240 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700241 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700242 }
243
244 // Create optical circuit intent
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700245 List<FlowRule> rules = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200246 // at the source: ODUCLT port mapping to OCH port
247 rules.add(connectPorts(src, connIntent.getSrc(), intent.priority(), slots));
248 // at the destination: OCH port mapping to ODUCLT port
249 rules.add(connectPorts(connIntent.getDst(), dst, intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700250
251 // Create flow rules for reverse path
252 if (intent.isBidirectional()) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200253 // at the destination: OCH port mapping to ODUCLT port
254 rules.add(connectPorts(connIntent.getSrc(), src, intent.priority(), slots));
255 // at the source: ODUCLT port mapping to OCH port
256 rules.add(connectPorts(dst, connIntent.getDst(), intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700257 }
258
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200259 FlowRuleIntent circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700260
261 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700262 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700263 intents.add(circuitIntent);
264
265 return intents;
266 }
267
268 /**
269 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700270 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700271 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700272 * @param resource the resource on which to map the intent
273 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700274 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700275 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700276 if (resource == null) {
277 return true;
278 }
279
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700280 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700281
Marc De Leenheer723f5532015-06-03 20:16:17 -0700282 if (mapping == null) {
283 return true;
284 }
285
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700286 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700287 }
288
289 private boolean isAllowed(OpticalCircuitIntent circuitIntent, OpticalConnectivityIntent connIntent) {
290 ConnectPoint srcStaticPort = staticPort(circuitIntent.getSrc());
291 if (srcStaticPort != null) {
292 if (!srcStaticPort.equals(connIntent.getSrc())) {
293 return false;
294 }
295 }
296
297 ConnectPoint dstStaticPort = staticPort(circuitIntent.getDst());
298 if (dstStaticPort != null) {
299 if (!dstStaticPort.equals(connIntent.getDst())) {
300 return false;
301 }
302 }
303
304 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700305 }
306
307 /**
308 * Returns existing and available optical connectivity intent that matches the given circuit intent.
309 *
310 * @param circuitIntent optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200311 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700312 * @return existing optical connectivity intent, null otherwise.
313 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200314 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent,
315 boolean multiplexingSupported) {
316
317 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(circuitIntent.getSignalType());
318
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700319 for (Intent intent : intentService.getIntents()) {
320 if (!(intent instanceof OpticalConnectivityIntent)) {
321 continue;
322 }
323
324 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
325
326 ConnectPoint src = circuitIntent.getSrc();
327 ConnectPoint dst = circuitIntent.getDst();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700328 // Ignore if the intents don't have identical src and dst devices
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200329 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) ||
Marc De Leenheer723f5532015-06-03 20:16:17 -0700330 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
331 continue;
332 }
333
334 if (!isAllowed(circuitIntent, connIntent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700335 continue;
336 }
337
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200338 if (!isAvailable(connIntent.id())) {
339 continue;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700340 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200341
342 if (multiplexingSupported) {
343 if (!isAvailableTributarySlots(connIntent, oduSignalType.tributarySlots())) {
344 continue;
345 }
346 }
347
348 return connIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700349 }
350
351 return null;
352 }
353
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200354 private boolean isAvailableTributarySlots(OpticalConnectivityIntent connIntent, int requestedTsNum) {
355 Set<TributarySlot> common = findCommonTributarySlotsOnCps(connIntent.getSrc(), connIntent.getDst());
356 if (common.isEmpty()) {
357 log.debug("No available TributarySlots");
358 return false;
359 }
360 if (common.size() < requestedTsNum) {
361 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
362 return false;
363 }
364 return true;
365 }
366
367 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
368 Pair<ConnectPoint, ConnectPoint> ports) {
369
370 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
371 int requestedTsNum = oduSignalType.tributarySlots();
372 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
373 if (commonTributarySlots.isEmpty()) {
374 return Collections.emptySet();
375 }
376 if (commonTributarySlots.size() < requestedTsNum) {
377 return Collections.emptySet();
378 }
379
380 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
381 .limit(requestedTsNum)
382 .collect(Collectors.toSet());
383
384 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
385 List<Resource> tributarySlotResources = portsList.stream()
386 .flatMap(cp -> tributarySlots
387 .stream()
388 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
389 .collect(Collectors.toList());
390
391 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
392 if (allocations.isEmpty()) {
393 log.debug("Resource allocation for {} failed (resource request: {})",
394 intent, tributarySlotResources);
395 return Collections.emptySet();
396 }
397 return tributarySlots;
398 }
399
Marc De Leenheer723f5532015-06-03 20:16:17 -0700400 private ConnectPoint staticPort(ConnectPoint connectPoint) {
401 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
402
403 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
404
405 // FIXME: need a better way to match the port
406 if (staticPort != null) {
407 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
408 if (staticPort.equals(p.number().name())) {
409 return new ConnectPoint(p.element().id(), p.number());
410 }
411 }
412 }
413
414 return null;
415 }
416
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200417 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700418 // First see if the port mappings are constrained
419 ConnectPoint ochCP = staticPort(oduPort);
420
421 if (ochCP != null) {
422 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700423 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800424 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800425 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700426 .map(ResourceAllocation::consumer)
427 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800428 .map(x -> (IntentId) x)
429 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700430
431 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700432 return ochPort;
433 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200434 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700435 }
436
437 // No port constraints, so find any port that works
438 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700439
440 for (Port port : ports) {
441 if (!(port instanceof OchPort)) {
442 continue;
443 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200444 // This should be the first allocation on the OCH port
445 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
446 continue;
447 }
448 // OchPort is required to have the requested oduSignalType
449 if (((OchPort) port).signalType() != ochPortSignalType) {
450 continue;
451 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700452
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700453 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800454 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800455 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700456 .map(ResourceAllocation::consumer)
457 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800458 .map(x -> (IntentId) x)
459 .findAny();
460
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700461 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700462 return (OchPort) port;
463 }
464 }
465
466 return null;
467 }
468
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700469 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200470 Pair<OchPort, OchPort> ochPorts = null;
471 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
472 switch (intent.getSignalType()) {
473 case CLT_1GBE:
474 case CLT_10GBE:
475 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
476 ochPorts = findPorts(intent, OduSignalType.ODU2);
477 if (ochPorts == null) {
478 ochPorts = findPorts(intent, OduSignalType.ODU4);
479 }
480 break;
481 case CLT_100GBE:
482 ochPorts = findPorts(intent, OduSignalType.ODU4);
483 break;
484 case CLT_40GBE:
485 default:
486 break;
487 }
488 return ochPorts;
489 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700490
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200491 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent, OduSignalType ochPortSignalType) {
492 OchPort srcPort = findAvailableOchPort(intent.getSrc(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700493 if (srcPort == null) {
494 return null;
495 }
496
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200497 OchPort dstPort = findAvailableOchPort(intent.getDst(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700498 if (dstPort == null) {
499 return null;
500 }
501
502 return Pair.of(srcPort, dstPort);
503 }
504
505 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700506 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700507 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700508 * @param src source port
509 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200510 * @param priority
511 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700512 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700513 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200514 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700515 checkArgument(src.deviceId().equals(dst.deviceId()));
516
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700517 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
518 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
519
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700520 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200521 if (!slots.isEmpty()) {
522 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
523 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
524 OduSignalType oduCltPortOduSignalType;
525 OduSignalType ochPortOduSignalType;
526
527 if (srcPort instanceof OduCltPort) {
528 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
529 ochPortOduSignalType = ((OchPort) dstPort).signalType();
530
531 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
532 // use Instruction of OduSignalId only in case of ODU Multiplexing
533 if (oduCltPortOduSignalType != ochPortOduSignalType) {
534 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
535 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
536 }
537 } else { // srcPort is OchPort
538 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
539 ochPortOduSignalType = ((OchPort) srcPort).signalType();
540
541 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
542 // use Criteria of OduSignalId only in case of ODU Multiplexing
543 if (oduCltPortOduSignalType != ochPortOduSignalType) {
544 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
545 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
546 }
547 }
548 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700549 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700550
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700551 FlowRule flowRule = DefaultFlowRule.builder()
552 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700553 .withSelector(selectorBuilder.build())
554 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400555 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700556 .fromApp(appId)
557 .makePermanent()
558 .build();
559
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700560 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700561 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200562
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200563 protected OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200564 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
565 int tributarySlotLen = ochPortSignalType.tributarySlots();
566 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
567
568 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
569 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
570 }
571
572 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
573 return (int) tributarySlots.stream().findFirst().get().index();
574 }
575
576 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
577 Driver driver = driverService.getDriver(deviceId);
578 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
579 }
580
581 private boolean isMultiplexingSupported(OpticalCircuitIntent intent) {
582 ConnectPoint src = intent.getSrc();
583 ConnectPoint dst = intent.getDst();
584
585 if (!isTributarySlotBehaviourSupported(src.deviceId()) ||
586 !isTributarySlotBehaviourSupported(dst.deviceId())) {
587 return false;
588 }
589
590 ConnectPoint srcStaticPort = staticPort(src);
591 if (srcStaticPort != null) {
592 return false;
593 }
594 ConnectPoint dstStaticPort = staticPort(dst);
595 if (dstStaticPort != null) {
596 return false;
597 }
598
599 return true;
600 }
601
602 /**
603 * Maps from Intent's OduClt SignalType to OduSignalType.
604 *
605 * @param cltSignalType OduClt port signal type
606 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
607 */
608 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
609 OduSignalType oduSignalType = OduSignalType.ODU0;
610 switch (cltSignalType) {
611 case CLT_1GBE:
612 oduSignalType = OduSignalType.ODU0;
613 break;
614 case CLT_10GBE:
615 oduSignalType = OduSignalType.ODU2;
616 break;
617 case CLT_40GBE:
618 oduSignalType = OduSignalType.ODU3;
619 break;
620 case CLT_100GBE:
621 oduSignalType = OduSignalType.ODU4;
622 break;
623 default:
624 log.error("Unsupported CltSignalType {}", cltSignalType);
625 break;
626 }
627 return oduSignalType;
628 }
629
630 /**
631 * Finds the common TributarySlots available on the two connect points.
632 *
633 * @param srcCp source connect point
634 * @param dstCp dest connect point
635 * @return set of common TributarySlots on both connect points
636 */
637 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint srcCp, ConnectPoint dstCp) {
638 Set<TributarySlot> forward = findTributarySlotsOnCp(srcCp);
639 Set<TributarySlot> backward = findTributarySlotsOnCp(dstCp);
640 return Sets.intersection(forward, backward);
641 }
642
643 /**
644 * Finds the TributarySlots available on the connect point.
645 *
646 * @param cp connect point
647 * @return set of TributarySlots available on the connect point
648 */
649 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800650 return resourceService.getAvailableResourceValues(
651 Resources.discrete(cp.deviceId(), cp.port()).id(),
652 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200653 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700654}