blob: 15ff1dfa14c72a8e10323bcd32108f260a2f3f7f [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.intent.impl.compiler;
17
Sho SHIMIZU12d02682016-02-24 13:55:12 -080018import com.google.common.base.Strings;
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 SHIMIZU739873b2016-02-23 17:02:12 -080066import org.onosproject.net.intent.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) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200128 if (context == null) {
129 return;
130 }
131
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700132 Dictionary properties = context.getProperties();
133
134 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
135 String propertyString = Tools.get(properties, "maxCapacity");
136
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800137 //Ignore if propertyString is empty or null
138 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700139 try {
140 int temp = Integer.parseInt(propertyString);
141 //Ensure value is non-negative but allow zero as a way to shutdown the link
142 if (temp >= 0) {
143 maxCapacity = temp;
144 }
145 } catch (NumberFormatException e) {
146 //Malformed arguments lead to no change of value (user should be notified of error)
147 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
148 }
149 } else {
150 //Notify of empty value but do not return (other properties will also go in this function)
151 log.error("The value for maxCapacity was set to an empty value.");
152 }
153
154 }
155
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700156 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700157 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700158 appId = coreService.registerApplication("org.onosproject.net.intent");
159 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700160 cfgService.registerProperties(getClass());
161 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700162 }
163
164 @Deactivate
165 public void deactivate() {
166 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700167 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700168 }
169
170 @Override
171 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
172 Set<LinkResourceAllocations> resources) {
173 // Check if ports are OduClt ports
174 ConnectPoint src = intent.getSrc();
175 ConnectPoint dst = intent.getDst();
176 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
177 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
178 checkArgument(srcPort instanceof OduCltPort);
179 checkArgument(dstPort instanceof OduCltPort);
180
181 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
182
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200183 // Release of intent resources here is only a temporary solution for handling the
184 // case of recompiling due to intent restoration (when intent state is FAILED).
185 // TODO: try to release intent resources in IntentManager.
186 resourceService.release(intent.id());
187
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700188 // Reserve OduClt ports
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800189 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
190 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800191 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700192 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700193 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
194 }
195
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200196 // Check if both devices support multiplexing (usage of TributarySlots)
197 boolean multiplexingSupported = isMultiplexingSupported(intent);
198
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700199 LinkedList<Intent> intents = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200200 // slots are used only for devices supporting multiplexing
201 Set<TributarySlot> slots = Collections.emptySet();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700202
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200203 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent, multiplexingSupported);
204 if ((connIntent != null) && multiplexingSupported) {
205 // Allocate TributarySlots on existing OCH ports
206 slots = assignTributarySlots(intent, Pair.of(connIntent.getSrc(), connIntent.getDst()));
207 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700208
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200209 // Create optical connectivity intent if needed - no optical intent or not enough slots available
210 if (connIntent == null || (multiplexingSupported && slots.isEmpty())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700211 // Find OCh ports with available resources
212 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
213
214 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200215 // Release port allocations if unsuccessful
216 resourceService.release(intent.id());
217 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
218 }
219
220 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
221 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
222
223 if (multiplexingSupported) {
224 // Allocate TributarySlots on OCH ports
225 slots = assignTributarySlots(intent, Pair.of(srcCP, dstCP));
226 if (slots.isEmpty()) {
227 // Release port allocations if unsuccessful
228 resourceService.release(intent.id());
229 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
230 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700231 }
232
233 // Create optical connectivity intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200234 OduSignalType signalType = ochPorts.getLeft().signalType();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700235 connIntent = OpticalConnectivityIntent.builder()
236 .appId(appId)
237 .src(srcCP)
238 .dst(dstCP)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200239 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700240 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700241 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700242 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700243 }
244
245 // Create optical circuit intent
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700246 List<FlowRule> rules = new LinkedList<>();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200247 // at the source: ODUCLT port mapping to OCH port
248 rules.add(connectPorts(src, connIntent.getSrc(), intent.priority(), slots));
249 // at the destination: OCH port mapping to ODUCLT port
250 rules.add(connectPorts(connIntent.getDst(), dst, intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700251
252 // Create flow rules for reverse path
253 if (intent.isBidirectional()) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200254 // at the destination: OCH port mapping to ODUCLT port
255 rules.add(connectPorts(connIntent.getSrc(), src, intent.priority(), slots));
256 // at the source: ODUCLT port mapping to OCH port
257 rules.add(connectPorts(dst, connIntent.getDst(), intent.priority(), slots));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700258 }
259
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200260 FlowRuleIntent circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700261
262 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700263 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700264 intents.add(circuitIntent);
265
266 return intents;
267 }
268
269 /**
270 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700271 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700272 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700273 * @param resource the resource on which to map the intent
274 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700275 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700276 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700277 if (resource == null) {
278 return true;
279 }
280
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700281 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700282
Marc De Leenheer723f5532015-06-03 20:16:17 -0700283 if (mapping == null) {
284 return true;
285 }
286
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700287 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700288 }
289
290 private boolean isAllowed(OpticalCircuitIntent circuitIntent, OpticalConnectivityIntent connIntent) {
291 ConnectPoint srcStaticPort = staticPort(circuitIntent.getSrc());
292 if (srcStaticPort != null) {
293 if (!srcStaticPort.equals(connIntent.getSrc())) {
294 return false;
295 }
296 }
297
298 ConnectPoint dstStaticPort = staticPort(circuitIntent.getDst());
299 if (dstStaticPort != null) {
300 if (!dstStaticPort.equals(connIntent.getDst())) {
301 return false;
302 }
303 }
304
305 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700306 }
307
308 /**
309 * Returns existing and available optical connectivity intent that matches the given circuit intent.
310 *
311 * @param circuitIntent optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200312 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700313 * @return existing optical connectivity intent, null otherwise.
314 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200315 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent,
316 boolean multiplexingSupported) {
317
318 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(circuitIntent.getSignalType());
319
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700320 for (Intent intent : intentService.getIntents()) {
321 if (!(intent instanceof OpticalConnectivityIntent)) {
322 continue;
323 }
324
325 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
326
327 ConnectPoint src = circuitIntent.getSrc();
328 ConnectPoint dst = circuitIntent.getDst();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700329 // Ignore if the intents don't have identical src and dst devices
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200330 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) ||
Marc De Leenheer723f5532015-06-03 20:16:17 -0700331 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
332 continue;
333 }
334
335 if (!isAllowed(circuitIntent, connIntent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700336 continue;
337 }
338
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200339 if (!isAvailable(connIntent.id())) {
340 continue;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700341 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200342
343 if (multiplexingSupported) {
344 if (!isAvailableTributarySlots(connIntent, oduSignalType.tributarySlots())) {
345 continue;
346 }
347 }
348
349 return connIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700350 }
351
352 return null;
353 }
354
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200355 private boolean isAvailableTributarySlots(OpticalConnectivityIntent connIntent, int requestedTsNum) {
356 Set<TributarySlot> common = findCommonTributarySlotsOnCps(connIntent.getSrc(), connIntent.getDst());
357 if (common.isEmpty()) {
358 log.debug("No available TributarySlots");
359 return false;
360 }
361 if (common.size() < requestedTsNum) {
362 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
363 return false;
364 }
365 return true;
366 }
367
368 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
369 Pair<ConnectPoint, ConnectPoint> ports) {
370
371 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
372 int requestedTsNum = oduSignalType.tributarySlots();
373 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
374 if (commonTributarySlots.isEmpty()) {
375 return Collections.emptySet();
376 }
377 if (commonTributarySlots.size() < requestedTsNum) {
378 return Collections.emptySet();
379 }
380
381 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
382 .limit(requestedTsNum)
383 .collect(Collectors.toSet());
384
385 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
386 List<Resource> tributarySlotResources = portsList.stream()
387 .flatMap(cp -> tributarySlots
388 .stream()
389 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
390 .collect(Collectors.toList());
391
392 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
393 if (allocations.isEmpty()) {
394 log.debug("Resource allocation for {} failed (resource request: {})",
395 intent, tributarySlotResources);
396 return Collections.emptySet();
397 }
398 return tributarySlots;
399 }
400
Marc De Leenheer723f5532015-06-03 20:16:17 -0700401 private ConnectPoint staticPort(ConnectPoint connectPoint) {
402 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
403
404 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
405
406 // FIXME: need a better way to match the port
407 if (staticPort != null) {
408 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
409 if (staticPort.equals(p.number().name())) {
410 return new ConnectPoint(p.element().id(), p.number());
411 }
412 }
413 }
414
415 return null;
416 }
417
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200418 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700419 // First see if the port mappings are constrained
420 ConnectPoint ochCP = staticPort(oduPort);
421
422 if (ochCP != null) {
423 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700424 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800425 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800426 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700427 .map(ResourceAllocation::consumer)
428 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800429 .map(x -> (IntentId) x)
430 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700431
432 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700433 return ochPort;
434 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200435 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700436 }
437
438 // No port constraints, so find any port that works
439 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700440
441 for (Port port : ports) {
442 if (!(port instanceof OchPort)) {
443 continue;
444 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200445 // This should be the first allocation on the OCH port
446 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
447 continue;
448 }
449 // OchPort is required to have the requested oduSignalType
450 if (((OchPort) port).signalType() != ochPortSignalType) {
451 continue;
452 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700453
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700454 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800455 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800456 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700457 .map(ResourceAllocation::consumer)
458 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800459 .map(x -> (IntentId) x)
460 .findAny();
461
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700462 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700463 return (OchPort) port;
464 }
465 }
466
467 return null;
468 }
469
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700470 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200471 Pair<OchPort, OchPort> ochPorts = null;
472 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
473 switch (intent.getSignalType()) {
474 case CLT_1GBE:
475 case CLT_10GBE:
476 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
477 ochPorts = findPorts(intent, OduSignalType.ODU2);
478 if (ochPorts == null) {
479 ochPorts = findPorts(intent, OduSignalType.ODU4);
480 }
481 break;
482 case CLT_100GBE:
483 ochPorts = findPorts(intent, OduSignalType.ODU4);
484 break;
485 case CLT_40GBE:
486 default:
487 break;
488 }
489 return ochPorts;
490 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700491
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200492 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent, OduSignalType ochPortSignalType) {
493 OchPort srcPort = findAvailableOchPort(intent.getSrc(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700494 if (srcPort == null) {
495 return null;
496 }
497
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200498 OchPort dstPort = findAvailableOchPort(intent.getDst(), ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700499 if (dstPort == null) {
500 return null;
501 }
502
503 return Pair.of(srcPort, dstPort);
504 }
505
506 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700507 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700508 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700509 * @param src source port
510 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200511 * @param priority
512 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700513 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700514 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200515 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700516 checkArgument(src.deviceId().equals(dst.deviceId()));
517
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700518 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
519 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
520
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700521 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200522 if (!slots.isEmpty()) {
523 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
524 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
525 OduSignalType oduCltPortOduSignalType;
526 OduSignalType ochPortOduSignalType;
527
528 if (srcPort instanceof OduCltPort) {
529 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
530 ochPortOduSignalType = ((OchPort) dstPort).signalType();
531
532 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
533 // use Instruction of OduSignalId only in case of ODU Multiplexing
534 if (oduCltPortOduSignalType != ochPortOduSignalType) {
535 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
536 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
537 }
538 } else { // srcPort is OchPort
539 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
540 ochPortOduSignalType = ((OchPort) srcPort).signalType();
541
542 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
543 // use Criteria of OduSignalId only in case of ODU Multiplexing
544 if (oduCltPortOduSignalType != ochPortOduSignalType) {
545 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
546 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
547 }
548 }
549 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700550 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700551
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700552 FlowRule flowRule = DefaultFlowRule.builder()
553 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700554 .withSelector(selectorBuilder.build())
555 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400556 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700557 .fromApp(appId)
558 .makePermanent()
559 .build();
560
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700561 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700562 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200563
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200564 protected OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200565 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
566 int tributarySlotLen = ochPortSignalType.tributarySlots();
567 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
568
569 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
570 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
571 }
572
573 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
574 return (int) tributarySlots.stream().findFirst().get().index();
575 }
576
577 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
578 Driver driver = driverService.getDriver(deviceId);
579 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
580 }
581
582 private boolean isMultiplexingSupported(OpticalCircuitIntent intent) {
583 ConnectPoint src = intent.getSrc();
584 ConnectPoint dst = intent.getDst();
585
586 if (!isTributarySlotBehaviourSupported(src.deviceId()) ||
587 !isTributarySlotBehaviourSupported(dst.deviceId())) {
588 return false;
589 }
590
591 ConnectPoint srcStaticPort = staticPort(src);
592 if (srcStaticPort != null) {
593 return false;
594 }
595 ConnectPoint dstStaticPort = staticPort(dst);
596 if (dstStaticPort != null) {
597 return false;
598 }
599
600 return true;
601 }
602
603 /**
604 * Maps from Intent's OduClt SignalType to OduSignalType.
605 *
606 * @param cltSignalType OduClt port signal type
607 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
608 */
609 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
610 OduSignalType oduSignalType = OduSignalType.ODU0;
611 switch (cltSignalType) {
612 case CLT_1GBE:
613 oduSignalType = OduSignalType.ODU0;
614 break;
615 case CLT_10GBE:
616 oduSignalType = OduSignalType.ODU2;
617 break;
618 case CLT_40GBE:
619 oduSignalType = OduSignalType.ODU3;
620 break;
621 case CLT_100GBE:
622 oduSignalType = OduSignalType.ODU4;
623 break;
624 default:
625 log.error("Unsupported CltSignalType {}", cltSignalType);
626 break;
627 }
628 return oduSignalType;
629 }
630
631 /**
632 * Finds the common TributarySlots available on the two connect points.
633 *
634 * @param srcCp source connect point
635 * @param dstCp dest connect point
636 * @return set of common TributarySlots on both connect points
637 */
638 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint srcCp, ConnectPoint dstCp) {
639 Set<TributarySlot> forward = findTributarySlotsOnCp(srcCp);
640 Set<TributarySlot> backward = findTributarySlotsOnCp(dstCp);
641 return Sets.intersection(forward, backward);
642 }
643
644 /**
645 * Finds the TributarySlots available on the connect point.
646 *
647 * @param cp connect point
648 * @return set of TributarySlots available on the connect point
649 */
650 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800651 return resourceService.getAvailableResourceValues(
652 Resources.discrete(cp.deviceId(), cp.port()).id(),
653 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200654 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700655}