blob: 148e0bc4f9ed29a75235dc396219f9664bc1be05 [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)
Sho SHIMIZUd8f7dc42016-02-29 11:14:18 -0800197 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc(), intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200198
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
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800202 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
203 intent.getSignalType(), multiplexingSupported);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200204 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
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800212 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700213
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
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700245 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700246 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700247
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800248 FlowRuleIntent circuitIntent = createFlowRule(intent, connIntent, slots);
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800249 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700250 }
251
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800252 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
253 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
254 // Create optical circuit intent
255 List<FlowRule> rules = new LinkedList<>();
256 // at the source: ODUCLT port mapping to OCH port
257 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
258 // at the destination: OCH port mapping to ODUCLT port
259 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
260
261 // Create flow rules for reverse path
262 if (higherIntent.isBidirectional()) {
263 // at the destination: OCH port mapping to ODUCLT port
264 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
265 // at the source: ODUCLT port mapping to OCH port
266 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
267 }
268
269 return new FlowRuleIntent(appId, rules, higherIntent.resources());
270 }
271
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700272 /**
273 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700274 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700275 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700276 * @param resource the resource on which to map the intent
277 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700278 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700279 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700280 if (resource == null) {
281 return true;
282 }
283
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700284 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700285
Marc De Leenheer723f5532015-06-03 20:16:17 -0700286 if (mapping == null) {
287 return true;
288 }
289
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700290 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700291 }
292
Sho SHIMIZU54fa5802016-02-29 11:29:06 -0800293 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
294 ConnectPoint srcStaticPort = staticPort(circuitCp);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700295 if (srcStaticPort != null) {
Sho SHIMIZU54fa5802016-02-29 11:29:06 -0800296 if (!srcStaticPort.equals(connectivityCp)) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700297 return false;
298 }
299 }
Marc De Leenheer723f5532015-06-03 20:16:17 -0700300 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 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800306 * @param src source connect point of optical circuit intent
307 * @param dst destination connect point of optical circuit intent
308 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200309 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700310 * @return existing optical connectivity intent, null otherwise.
311 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800312 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
313 ConnectPoint dst,
314 CltSignalType signalType,
315 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200316
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800317 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200318
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800319 return Tools.stream(intentService.getIntents())
320 .filter(x -> x instanceof OpticalConnectivityIntent)
321 .map(x -> (OpticalConnectivityIntent) x)
322 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
323 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
324 .filter(x -> isAllowed(src, x.getSrc()))
325 .filter(x -> isAllowed(dst, x.getDst()))
326 .filter(x -> isAvailable(x.id()))
327 .filter(x -> !multiplexingSupported ||
328 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
329 .findFirst()
330 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700331 }
332
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800333 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
334 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200335 if (common.isEmpty()) {
336 log.debug("No available TributarySlots");
337 return false;
338 }
339 if (common.size() < requestedTsNum) {
340 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
341 return false;
342 }
343 return true;
344 }
345
346 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
347 Pair<ConnectPoint, ConnectPoint> ports) {
348
349 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
350 int requestedTsNum = oduSignalType.tributarySlots();
351 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
352 if (commonTributarySlots.isEmpty()) {
353 return Collections.emptySet();
354 }
355 if (commonTributarySlots.size() < requestedTsNum) {
356 return Collections.emptySet();
357 }
358
359 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
360 .limit(requestedTsNum)
361 .collect(Collectors.toSet());
362
363 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
364 List<Resource> tributarySlotResources = portsList.stream()
365 .flatMap(cp -> tributarySlots
366 .stream()
367 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
368 .collect(Collectors.toList());
369
370 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
371 if (allocations.isEmpty()) {
372 log.debug("Resource allocation for {} failed (resource request: {})",
373 intent, tributarySlotResources);
374 return Collections.emptySet();
375 }
376 return tributarySlots;
377 }
378
Marc De Leenheer723f5532015-06-03 20:16:17 -0700379 private ConnectPoint staticPort(ConnectPoint connectPoint) {
380 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
381
382 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
383
384 // FIXME: need a better way to match the port
385 if (staticPort != null) {
386 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
387 if (staticPort.equals(p.number().name())) {
388 return new ConnectPoint(p.element().id(), p.number());
389 }
390 }
391 }
392
393 return null;
394 }
395
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200396 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700397 // First see if the port mappings are constrained
398 ConnectPoint ochCP = staticPort(oduPort);
399
400 if (ochCP != null) {
401 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700402 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800403 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800404 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700405 .map(ResourceAllocation::consumer)
406 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800407 .map(x -> (IntentId) x)
408 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700409
410 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700411 return ochPort;
412 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200413 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700414 }
415
416 // No port constraints, so find any port that works
417 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700418
419 for (Port port : ports) {
420 if (!(port instanceof OchPort)) {
421 continue;
422 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200423 // This should be the first allocation on the OCH port
424 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
425 continue;
426 }
427 // OchPort is required to have the requested oduSignalType
428 if (((OchPort) port).signalType() != ochPortSignalType) {
429 continue;
430 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700431
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700432 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800433 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800434 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700435 .map(ResourceAllocation::consumer)
436 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800437 .map(x -> (IntentId) x)
438 .findAny();
439
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700440 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700441 return (OchPort) port;
442 }
443 }
444
445 return null;
446 }
447
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800448 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200449 Pair<OchPort, OchPort> ochPorts = null;
450 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800451 switch (signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200452 case CLT_1GBE:
453 case CLT_10GBE:
454 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800455 ochPorts = findPorts(src, dst, OduSignalType.ODU2);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200456 if (ochPorts == null) {
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800457 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200458 }
459 break;
460 case CLT_100GBE:
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800461 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200462 break;
463 case CLT_40GBE:
464 default:
465 break;
466 }
467 return ochPorts;
468 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700469
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800470 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, OduSignalType ochPortSignalType) {
471 OchPort srcPort = findAvailableOchPort(src, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700472 if (srcPort == null) {
473 return null;
474 }
475
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800476 OchPort dstPort = findAvailableOchPort(dst, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700477 if (dstPort == null) {
478 return null;
479 }
480
481 return Pair.of(srcPort, dstPort);
482 }
483
484 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700485 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700486 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700487 * @param src source port
488 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200489 * @param priority
490 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700491 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700492 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200493 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700494 checkArgument(src.deviceId().equals(dst.deviceId()));
495
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700496 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
497 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
498
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700499 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200500 if (!slots.isEmpty()) {
501 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
502 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
503 OduSignalType oduCltPortOduSignalType;
504 OduSignalType ochPortOduSignalType;
505
506 if (srcPort instanceof OduCltPort) {
507 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
508 ochPortOduSignalType = ((OchPort) dstPort).signalType();
509
510 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
511 // use Instruction of OduSignalId only in case of ODU Multiplexing
512 if (oduCltPortOduSignalType != ochPortOduSignalType) {
513 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
514 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
515 }
516 } else { // srcPort is OchPort
517 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
518 ochPortOduSignalType = ((OchPort) srcPort).signalType();
519
520 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
521 // use Criteria of OduSignalId only in case of ODU Multiplexing
522 if (oduCltPortOduSignalType != ochPortOduSignalType) {
523 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
524 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
525 }
526 }
527 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700528 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700529
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700530 FlowRule flowRule = DefaultFlowRule.builder()
531 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700532 .withSelector(selectorBuilder.build())
533 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400534 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700535 .fromApp(appId)
536 .makePermanent()
537 .build();
538
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700539 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700540 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200541
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200542 protected OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200543 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
544 int tributarySlotLen = ochPortSignalType.tributarySlots();
545 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
546
547 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
548 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
549 }
550
551 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
552 return (int) tributarySlots.stream().findFirst().get().index();
553 }
554
555 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
556 Driver driver = driverService.getDriver(deviceId);
557 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
558 }
559
Sho SHIMIZUd8f7dc42016-02-29 11:14:18 -0800560 private boolean isMultiplexingSupported(ConnectPoint src, ConnectPoint dst) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200561 if (!isTributarySlotBehaviourSupported(src.deviceId()) ||
562 !isTributarySlotBehaviourSupported(dst.deviceId())) {
563 return false;
564 }
565
566 ConnectPoint srcStaticPort = staticPort(src);
567 if (srcStaticPort != null) {
568 return false;
569 }
570 ConnectPoint dstStaticPort = staticPort(dst);
571 if (dstStaticPort != null) {
572 return false;
573 }
574
575 return true;
576 }
577
578 /**
579 * Maps from Intent's OduClt SignalType to OduSignalType.
580 *
581 * @param cltSignalType OduClt port signal type
582 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
583 */
584 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200585 switch (cltSignalType) {
586 case CLT_1GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800587 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200588 case CLT_10GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800589 return OduSignalType.ODU2;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200590 case CLT_40GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800591 return OduSignalType.ODU3;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200592 case CLT_100GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800593 return OduSignalType.ODU4;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200594 default:
595 log.error("Unsupported CltSignalType {}", cltSignalType);
Sho SHIMIZU88799912016-02-29 13:34:12 -0800596 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200597 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200598 }
599
600 /**
601 * Finds the common TributarySlots available on the two connect points.
602 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800603 * @param src source connect point
604 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200605 * @return set of common TributarySlots on both connect points
606 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800607 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
608 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
609 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200610 return Sets.intersection(forward, backward);
611 }
612
613 /**
614 * Finds the TributarySlots available on the connect point.
615 *
616 * @param cp connect point
617 * @return set of TributarySlots available on the connect point
618 */
619 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800620 return resourceService.getAvailableResourceValues(
621 Resources.discrete(cp.deviceId(), cp.port()).id(),
622 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200623 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700624}