blob: baa5260392a2812aa56a7ccd4c27ac2742d9b574 [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07003 *
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;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020019
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070020import org.apache.commons.lang3.tuple.Pair;
21import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070022import org.apache.felix.scr.annotations.Component;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070023import org.apache.felix.scr.annotations.Deactivate;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070024import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070028import org.onlab.util.Tools;
29import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070032import org.onosproject.net.AnnotationKeys;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020033import org.onosproject.net.CltSignalType;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070034import org.onosproject.net.ConnectPoint;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020035import org.onosproject.net.OduSignalId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070036import org.onosproject.net.OduSignalType;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020037import org.onosproject.net.OduSignalUtils;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070038import 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;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080061import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070062import org.onosproject.net.optical.OduCltPort;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080063import org.onosproject.net.intent.IntentSetMultimap;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080064import org.onosproject.net.resource.ResourceAllocation;
65import org.onosproject.net.resource.Resource;
66import org.onosproject.net.resource.ResourceService;
67import org.onosproject.net.resource.Resources;
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;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020082import java.util.stream.Stream;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070083
84import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080085import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070086
87/**
88 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
89 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070090@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070091public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
92
93 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
94
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070095 private static final int DEFAULT_MAX_CAPACITY = 10;
96
97 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
98 label = "Maximum utilization of an optical connection.")
99
100 private int maxCapacity = DEFAULT_MAX_CAPACITY;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected ComponentConfigService cfgService;
104
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected IntentExtensionService intentManager;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected CoreService coreService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected DeviceService deviceService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700115 protected ResourceService resourceService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700118 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected IntentService intentService;
122
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected DriverService driverService;
125
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700126 private ApplicationId appId;
127
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700128 @Modified
129 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200130 if (context == null) {
131 return;
132 }
133
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700134 Dictionary properties = context.getProperties();
135
136 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
137 String propertyString = Tools.get(properties, "maxCapacity");
138
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800139 //Ignore if propertyString is empty or null
140 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700141 try {
142 int temp = Integer.parseInt(propertyString);
143 //Ensure value is non-negative but allow zero as a way to shutdown the link
144 if (temp >= 0) {
145 maxCapacity = temp;
146 }
147 } catch (NumberFormatException e) {
148 //Malformed arguments lead to no change of value (user should be notified of error)
149 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
150 }
151 } else {
152 //Notify of empty value but do not return (other properties will also go in this function)
153 log.error("The value for maxCapacity was set to an empty value.");
154 }
155
156 }
157
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700158 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700159 public void activate(ComponentContext context) {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800160 deviceService = opticalView(deviceService);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700161 appId = coreService.registerApplication("org.onosproject.net.intent");
162 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700163 cfgService.registerProperties(getClass());
164 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700165 }
166
167 @Deactivate
168 public void deactivate() {
169 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700170 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700171 }
172
173 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800174 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700175 // Check if ports are OduClt ports
176 ConnectPoint src = intent.getSrc();
177 ConnectPoint dst = intent.getDst();
178 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
179 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
180 checkArgument(srcPort instanceof OduCltPort);
181 checkArgument(dstPort instanceof OduCltPort);
182
183 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
184
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200185 // Release of intent resources here is only a temporary solution for handling the
186 // case of recompiling due to intent restoration (when intent state is FAILED).
187 // TODO: try to release intent resources in IntentManager.
188 resourceService.release(intent.id());
189
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200190 // Check OduClt ports availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800191 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
192 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200193 // If ports are not available, compilation fails
194 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
195 throw new IntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700196 }
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200197 List<Resource> ports = ImmutableList.of(srcPortResource, dstPortResource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700198
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200199 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800200 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
201 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200202
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800203 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
204 intent.getSignalType(), multiplexingSupported);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800205
206 if (connIntent != null && !multiplexingSupported) {
207 return compile(intent, src, dst, Optional.of(connIntent), ports, false);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200208 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700209
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200210 // Create optical connectivity intent if needed - no optical intent or not enough slots available
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800211 if (connIntent == null) {
212 return compile(intent, src, dst, Optional.empty(), ports, multiplexingSupported);
213 }
214
215 List<Resource> slots = availableSlotResources(connIntent.getSrc(), connIntent.getDst(),
216 intent.getSignalType());
217 if (slots.isEmpty()) {
218 return compile(intent, src, dst, Optional.empty(), ports, true);
219 }
220
221 return compile(intent, src, dst, Optional.of(connIntent), ImmutableList.<Resource>builder()
222 .addAll(ports).addAll(slots).build(), false);
223
224 }
225
226 private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst,
227 Optional<OpticalConnectivityIntent> existingConnectivity,
228 List<Resource> resources, boolean supportsMultiplexing) {
229 OpticalConnectivityIntent connectivityIntent;
230 List<Resource> required;
231 if (existingConnectivity.isPresent()) {
232 connectivityIntent = existingConnectivity.get();
233 required = resources;
234 } else {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700235 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800236 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700237
238 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200239 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
240 }
241
242 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
243 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
244
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700245 // Create optical connectivity intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800246 connectivityIntent = OpticalConnectivityIntent.builder()
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700247 .appId(appId)
248 .src(srcCP)
249 .dst(dstCP)
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800250 .signalType(ochPorts.getLeft().signalType())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700251 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700252 .build();
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800253
254 if (!supportsMultiplexing) {
255 required = resources;
256 } else {
257 List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
258 if (slots.isEmpty()) {
259 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
260 }
261 required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
262 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700263 }
264
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800265 if (resourceService.allocate(intent.id(), required).isEmpty()) {
266 throw new IntentCompilationException("Unable to allocate resources for intent " + intent
267 + ": resources=" + required);
268 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700269
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800270 intentService.submit(connectivityIntent);
271
272 // Save circuit to connectivity intent mapping
273 intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
274
275 FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().
276 flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class)))
277 .collect(Collectors.toSet()));
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800278 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700279 }
280
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800281 private List<Resource> availableSlotResources(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200282 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800283 int requestedTsNum = oduSignalType.tributarySlots();
284 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(src, dst);
285 if (commonTributarySlots.isEmpty()) {
286 return Collections.emptyList();
287 }
288 if (commonTributarySlots.size() < requestedTsNum) {
289 return Collections.emptyList();
290 }
291
292 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
293 .limit(requestedTsNum)
294 .collect(Collectors.toSet());
295
296 final List<ConnectPoint> portsList = ImmutableList.of(src, dst);
297 List<Resource> tributarySlotResources = portsList.stream()
298 .flatMap(cp -> tributarySlots
299 .stream()
300 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
301 .collect(Collectors.toList());
302
303 if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
304 log.debug("Resource allocation for {} on {} and {} failed (resource request: {})",
305 signalType, src, dst, tributarySlotResources);
306 return Collections.emptyList();
307 }
308 return tributarySlotResources;
309 }
310
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800311 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
312 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
313 // Create optical circuit intent
314 List<FlowRule> rules = new LinkedList<>();
315 // at the source: ODUCLT port mapping to OCH port
316 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
317 // at the destination: OCH port mapping to ODUCLT port
318 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
319
320 // Create flow rules for reverse path
321 if (higherIntent.isBidirectional()) {
322 // at the destination: OCH port mapping to ODUCLT port
323 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
324 // at the source: ODUCLT port mapping to OCH port
325 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
326 }
327
328 return new FlowRuleIntent(appId, rules, higherIntent.resources());
329 }
330
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700331 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700332 * Returns existing and available optical connectivity intent that matches the given circuit intent.
333 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800334 * @param src source connect point of optical circuit intent
335 * @param dst destination connect point of optical circuit intent
336 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200337 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700338 * @return existing optical connectivity intent, null otherwise.
339 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800340 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
341 ConnectPoint dst,
342 CltSignalType signalType,
343 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200344
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200345 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200346
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800347 return Tools.stream(intentService.getIntents())
348 .filter(x -> x instanceof OpticalConnectivityIntent)
349 .map(x -> (OpticalConnectivityIntent) x)
350 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
351 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
352 .filter(x -> isAllowed(src, x.getSrc()))
353 .filter(x -> isAllowed(dst, x.getDst()))
354 .filter(x -> isAvailable(x.id()))
355 .filter(x -> !multiplexingSupported ||
356 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
357 .findFirst()
358 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700359 }
360
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800361 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
362 ConnectPoint staticPort = staticPort(circuitCp);
363 return staticPort == null || staticPort.equals(connectivityCp);
364 }
365
366 /**
367 * Checks if current allocations on given resource can satisfy request.
368 * If the resource is null, return true.
369 *
370 * @param resource the resource on which to map the intent
371 * @return true if the resource can accept the request, false otherwise
372 */
373 private boolean isAvailable(IntentId resource) {
374 if (resource == null) {
375 return true;
376 }
377
378 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
379
380 if (mapping == null) {
381 return true;
382 }
383
384 return mapping.size() < maxCapacity;
385 }
386
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800387 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
388 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200389 if (common.isEmpty()) {
390 log.debug("No available TributarySlots");
391 return false;
392 }
393 if (common.size() < requestedTsNum) {
394 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
395 return false;
396 }
397 return true;
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
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800417 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
418 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
419 switch (signalType) {
420 case CLT_1GBE:
421 case CLT_10GBE:
422 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
423 return findPorts(src, dst, OduSignalType.ODU2)
424 .orElse(findPorts(src, dst, OduSignalType.ODU4).orElse(null));
425 case CLT_100GBE:
426 return findPorts(src, dst, OduSignalType.ODU4).orElse(null);
427 case CLT_40GBE:
428 default:
429 return null;
430 }
431 }
432
433 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
434 OduSignalType ochPortSignalType) {
435 return findAvailableOchPort(src, ochPortSignalType)
436 .flatMap(srcOch ->
437 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
438 }
439
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800440 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700441 // First see if the port mappings are constrained
442 ConnectPoint ochCP = staticPort(oduPort);
443
444 if (ochCP != null) {
445 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700446 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800447 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800448 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700449 .map(ResourceAllocation::consumer)
450 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800451 .map(x -> (IntentId) x)
452 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700453
454 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800455 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700456 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800457 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700458 }
459
460 // No port constraints, so find any port that works
461 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700462
463 for (Port port : ports) {
464 if (!(port instanceof OchPort)) {
465 continue;
466 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200467 // This should be the first allocation on the OCH port
468 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
469 continue;
470 }
471 // OchPort is required to have the requested oduSignalType
472 if (((OchPort) port).signalType() != ochPortSignalType) {
473 continue;
474 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700475
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700476 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800477 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800478 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700479 .map(ResourceAllocation::consumer)
480 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800481 .map(x -> (IntentId) x)
482 .findAny();
483
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700484 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800485 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700486 }
487 }
488
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800489 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700490 }
491
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700492 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700493 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700494 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700495 * @param src source port
496 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200497 * @param priority
498 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700499 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700500 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200501 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700502 checkArgument(src.deviceId().equals(dst.deviceId()));
503
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700504 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
505 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
506
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700507 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200508 if (!slots.isEmpty()) {
509 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
510 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
511 OduSignalType oduCltPortOduSignalType;
512 OduSignalType ochPortOduSignalType;
513
514 if (srcPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200515 oduCltPortOduSignalType =
516 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200517 ochPortOduSignalType = ((OchPort) dstPort).signalType();
518
519 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
520 // use Instruction of OduSignalId only in case of ODU Multiplexing
521 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200522 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200523 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
524 }
525 } else { // srcPort is OchPort
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200526 oduCltPortOduSignalType =
527 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200528 ochPortOduSignalType = ((OchPort) srcPort).signalType();
529
530 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
531 // use Criteria of OduSignalId only in case of ODU Multiplexing
532 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200533 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200534 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
535 }
536 }
537 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700538 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700539
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700540 FlowRule flowRule = DefaultFlowRule.builder()
541 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700542 .withSelector(selectorBuilder.build())
543 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400544 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700545 .fromApp(appId)
546 .makePermanent()
547 .build();
548
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700549 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700550 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200551
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800552 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800553 Driver driver = driverService.getDriver(cp.deviceId());
554 return driver != null
555 && driver.hasBehaviour(TributarySlotQuery.class)
556 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200557 }
558
559 /**
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200560 * Finds the common TributarySlots available on the two connect points.
561 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800562 * @param src source connect point
563 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200564 * @return set of common TributarySlots on both connect points
565 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800566 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
567 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
568 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200569 return Sets.intersection(forward, backward);
570 }
571
572 /**
573 * Finds the TributarySlots available on the connect point.
574 *
575 * @param cp connect point
576 * @return set of TributarySlots available on the connect point
577 */
578 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800579 return resourceService.getAvailableResourceValues(
580 Resources.discrete(cp.deviceId(), cp.port()).id(),
581 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200582 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700583}