blob: 9e31234e969b20587c3432f137800b5c72fefba4 [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;
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;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020039import org.onosproject.net.OduSignalUtils;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070040import org.onosproject.net.Port;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020041import org.onosproject.net.TributarySlot;
42import org.onosproject.net.behaviour.TributarySlotQuery;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070043import org.onosproject.net.device.DeviceService;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020044import org.onosproject.net.driver.Driver;
45import org.onosproject.net.driver.DriverService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070046import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.TrafficSelector;
51import org.onosproject.net.flow.TrafficTreatment;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020052import org.onosproject.net.flow.criteria.Criteria;
53import org.onosproject.net.flow.instructions.Instructions;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070054import org.onosproject.net.intent.FlowRuleIntent;
55import org.onosproject.net.intent.Intent;
56import org.onosproject.net.intent.IntentCompiler;
57import org.onosproject.net.intent.IntentExtensionService;
58import org.onosproject.net.intent.IntentId;
59import org.onosproject.net.intent.IntentService;
60import org.onosproject.net.intent.OpticalCircuitIntent;
61import org.onosproject.net.intent.OpticalConnectivityIntent;
62import org.onosproject.net.intent.impl.IntentCompilationException;
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;
85
86/**
87 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
88 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070089@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070090public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
91
92 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
93
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070094 private static final int DEFAULT_MAX_CAPACITY = 10;
95
96 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
97 label = "Maximum utilization of an optical connection.")
98
99 private int maxCapacity = DEFAULT_MAX_CAPACITY;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected ComponentConfigService cfgService;
103
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected IntentExtensionService intentManager;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected CoreService coreService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected DeviceService deviceService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700114 protected ResourceService resourceService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700117 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected IntentService intentService;
121
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected DriverService driverService;
124
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700125 private ApplicationId appId;
126
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700127 @Modified
128 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200129 if (context == null) {
130 return;
131 }
132
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700133 Dictionary properties = context.getProperties();
134
135 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
136 String propertyString = Tools.get(properties, "maxCapacity");
137
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800138 //Ignore if propertyString is empty or null
139 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700140 try {
141 int temp = Integer.parseInt(propertyString);
142 //Ensure value is non-negative but allow zero as a way to shutdown the link
143 if (temp >= 0) {
144 maxCapacity = temp;
145 }
146 } catch (NumberFormatException e) {
147 //Malformed arguments lead to no change of value (user should be notified of error)
148 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
149 }
150 } else {
151 //Notify of empty value but do not return (other properties will also go in this function)
152 log.error("The value for maxCapacity was set to an empty value.");
153 }
154
155 }
156
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700157 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700158 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700159 appId = coreService.registerApplication("org.onosproject.net.intent");
160 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700161 cfgService.registerProperties(getClass());
162 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700163 }
164
165 @Deactivate
166 public void deactivate() {
167 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700168 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700169 }
170
171 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800172 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700173 // 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
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200188 // Check OduClt ports availability
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();
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200191 // If ports are not available, compilation fails
192 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
193 throw new IntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700194 }
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200195 List<Resource> ports = ImmutableList.of(srcPortResource, dstPortResource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700196
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200197 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800198 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
199 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200200
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800201 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
202 intent.getSignalType(), multiplexingSupported);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800203
204 if (connIntent != null && !multiplexingSupported) {
205 return compile(intent, src, dst, Optional.of(connIntent), ports, false);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200206 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700207
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200208 // Create optical connectivity intent if needed - no optical intent or not enough slots available
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800209 if (connIntent == null) {
210 return compile(intent, src, dst, Optional.empty(), ports, multiplexingSupported);
211 }
212
213 List<Resource> slots = availableSlotResources(connIntent.getSrc(), connIntent.getDst(),
214 intent.getSignalType());
215 if (slots.isEmpty()) {
216 return compile(intent, src, dst, Optional.empty(), ports, true);
217 }
218
219 return compile(intent, src, dst, Optional.of(connIntent), ImmutableList.<Resource>builder()
220 .addAll(ports).addAll(slots).build(), false);
221
222 }
223
224 private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst,
225 Optional<OpticalConnectivityIntent> existingConnectivity,
226 List<Resource> resources, boolean supportsMultiplexing) {
227 OpticalConnectivityIntent connectivityIntent;
228 List<Resource> required;
229 if (existingConnectivity.isPresent()) {
230 connectivityIntent = existingConnectivity.get();
231 required = resources;
232 } else {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700233 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800234 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700235
236 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200237 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
238 }
239
240 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
241 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
242
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700243 // Create optical connectivity intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800244 connectivityIntent = OpticalConnectivityIntent.builder()
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700245 .appId(appId)
246 .src(srcCP)
247 .dst(dstCP)
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800248 .signalType(ochPorts.getLeft().signalType())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700249 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700250 .build();
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800251
252 if (!supportsMultiplexing) {
253 required = resources;
254 } else {
255 List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
256 if (slots.isEmpty()) {
257 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
258 }
259 required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
260 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700261 }
262
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800263 if (resourceService.allocate(intent.id(), required).isEmpty()) {
264 throw new IntentCompilationException("Unable to allocate resources for intent " + intent
265 + ": resources=" + required);
266 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700267
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800268 intentService.submit(connectivityIntent);
269
270 // Save circuit to connectivity intent mapping
271 intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
272
273 FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().
274 flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class)))
275 .collect(Collectors.toSet()));
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800276 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700277 }
278
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800279 private List<Resource> availableSlotResources(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200280 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800281 int requestedTsNum = oduSignalType.tributarySlots();
282 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(src, dst);
283 if (commonTributarySlots.isEmpty()) {
284 return Collections.emptyList();
285 }
286 if (commonTributarySlots.size() < requestedTsNum) {
287 return Collections.emptyList();
288 }
289
290 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
291 .limit(requestedTsNum)
292 .collect(Collectors.toSet());
293
294 final List<ConnectPoint> portsList = ImmutableList.of(src, dst);
295 List<Resource> tributarySlotResources = portsList.stream()
296 .flatMap(cp -> tributarySlots
297 .stream()
298 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
299 .collect(Collectors.toList());
300
301 if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
302 log.debug("Resource allocation for {} on {} and {} failed (resource request: {})",
303 signalType, src, dst, tributarySlotResources);
304 return Collections.emptyList();
305 }
306 return tributarySlotResources;
307 }
308
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800309 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
310 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
311 // Create optical circuit intent
312 List<FlowRule> rules = new LinkedList<>();
313 // at the source: ODUCLT port mapping to OCH port
314 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
315 // at the destination: OCH port mapping to ODUCLT port
316 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
317
318 // Create flow rules for reverse path
319 if (higherIntent.isBidirectional()) {
320 // at the destination: OCH port mapping to ODUCLT port
321 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
322 // at the source: ODUCLT port mapping to OCH port
323 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
324 }
325
326 return new FlowRuleIntent(appId, rules, higherIntent.resources());
327 }
328
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700329 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700330 * Returns existing and available optical connectivity intent that matches the given circuit intent.
331 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800332 * @param src source connect point of optical circuit intent
333 * @param dst destination connect point of optical circuit intent
334 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200335 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700336 * @return existing optical connectivity intent, null otherwise.
337 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800338 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
339 ConnectPoint dst,
340 CltSignalType signalType,
341 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200342
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200343 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200344
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800345 return Tools.stream(intentService.getIntents())
346 .filter(x -> x instanceof OpticalConnectivityIntent)
347 .map(x -> (OpticalConnectivityIntent) x)
348 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
349 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
350 .filter(x -> isAllowed(src, x.getSrc()))
351 .filter(x -> isAllowed(dst, x.getDst()))
352 .filter(x -> isAvailable(x.id()))
353 .filter(x -> !multiplexingSupported ||
354 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
355 .findFirst()
356 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700357 }
358
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800359 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
360 ConnectPoint staticPort = staticPort(circuitCp);
361 return staticPort == null || staticPort.equals(connectivityCp);
362 }
363
364 /**
365 * Checks if current allocations on given resource can satisfy request.
366 * If the resource is null, return true.
367 *
368 * @param resource the resource on which to map the intent
369 * @return true if the resource can accept the request, false otherwise
370 */
371 private boolean isAvailable(IntentId resource) {
372 if (resource == null) {
373 return true;
374 }
375
376 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
377
378 if (mapping == null) {
379 return true;
380 }
381
382 return mapping.size() < maxCapacity;
383 }
384
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800385 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
386 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200387 if (common.isEmpty()) {
388 log.debug("No available TributarySlots");
389 return false;
390 }
391 if (common.size() < requestedTsNum) {
392 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
393 return false;
394 }
395 return true;
396 }
397
Marc De Leenheer723f5532015-06-03 20:16:17 -0700398 private ConnectPoint staticPort(ConnectPoint connectPoint) {
399 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
400
401 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
402
403 // FIXME: need a better way to match the port
404 if (staticPort != null) {
405 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
406 if (staticPort.equals(p.number().name())) {
407 return new ConnectPoint(p.element().id(), p.number());
408 }
409 }
410 }
411
412 return null;
413 }
414
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800415 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
416 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
417 switch (signalType) {
418 case CLT_1GBE:
419 case CLT_10GBE:
420 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
421 return findPorts(src, dst, OduSignalType.ODU2)
422 .orElse(findPorts(src, dst, OduSignalType.ODU4).orElse(null));
423 case CLT_100GBE:
424 return findPorts(src, dst, OduSignalType.ODU4).orElse(null);
425 case CLT_40GBE:
426 default:
427 return null;
428 }
429 }
430
431 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
432 OduSignalType ochPortSignalType) {
433 return findAvailableOchPort(src, ochPortSignalType)
434 .flatMap(srcOch ->
435 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
436 }
437
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800438 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700439 // First see if the port mappings are constrained
440 ConnectPoint ochCP = staticPort(oduPort);
441
442 if (ochCP != null) {
443 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700444 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800445 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800446 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700447 .map(ResourceAllocation::consumer)
448 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800449 .map(x -> (IntentId) x)
450 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700451
452 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800453 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700454 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800455 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700456 }
457
458 // No port constraints, so find any port that works
459 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700460
461 for (Port port : ports) {
462 if (!(port instanceof OchPort)) {
463 continue;
464 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200465 // This should be the first allocation on the OCH port
466 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
467 continue;
468 }
469 // OchPort is required to have the requested oduSignalType
470 if (((OchPort) port).signalType() != ochPortSignalType) {
471 continue;
472 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700473
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700474 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800475 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800476 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700477 .map(ResourceAllocation::consumer)
478 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800479 .map(x -> (IntentId) x)
480 .findAny();
481
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700482 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800483 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700484 }
485 }
486
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800487 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700488 }
489
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700490 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700491 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700492 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700493 * @param src source port
494 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200495 * @param priority
496 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700497 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700498 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200499 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700500 checkArgument(src.deviceId().equals(dst.deviceId()));
501
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700502 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
503 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
504
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700505 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200506 if (!slots.isEmpty()) {
507 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
508 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
509 OduSignalType oduCltPortOduSignalType;
510 OduSignalType ochPortOduSignalType;
511
512 if (srcPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200513 oduCltPortOduSignalType =
514 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200515 ochPortOduSignalType = ((OchPort) dstPort).signalType();
516
517 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
518 // use Instruction of OduSignalId only in case of ODU Multiplexing
519 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200520 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200521 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
522 }
523 } else { // srcPort is OchPort
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200524 oduCltPortOduSignalType =
525 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200526 ochPortOduSignalType = ((OchPort) srcPort).signalType();
527
528 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
529 // use Criteria of OduSignalId only in case of ODU Multiplexing
530 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200531 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200532 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
533 }
534 }
535 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700536 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700537
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700538 FlowRule flowRule = DefaultFlowRule.builder()
539 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700540 .withSelector(selectorBuilder.build())
541 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400542 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700543 .fromApp(appId)
544 .makePermanent()
545 .build();
546
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700547 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700548 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200549
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800550 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800551 Driver driver = driverService.getDriver(cp.deviceId());
552 return driver != null
553 && driver.hasBehaviour(TributarySlotQuery.class)
554 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200555 }
556
557 /**
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200558 * Finds the common TributarySlots available on the two connect points.
559 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800560 * @param src source connect point
561 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200562 * @return set of common TributarySlots on both connect points
563 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800564 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
565 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
566 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200567 return Sets.intersection(forward, backward);
568 }
569
570 /**
571 * Finds the TributarySlots available on the connect point.
572 *
573 * @param cp connect point
574 * @return set of TributarySlots available on the connect point
575 */
576 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800577 return resourceService.getAvailableResourceValues(
578 Resources.discrete(cp.deviceId(), cp.port()).id(),
579 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200580 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700581}