blob: 9b131aef968d5662ae9d31772a5c707f317aa40a [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-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 */
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070016package org.onosproject.net.optical.intent.impl.compiler;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070017
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;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080060import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070061import org.onosproject.net.optical.OduCltPort;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080062import org.onosproject.net.intent.IntentSetMultimap;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080063import org.onosproject.net.resource.ResourceAllocation;
64import org.onosproject.net.resource.Resource;
65import org.onosproject.net.resource.ResourceService;
66import org.onosproject.net.resource.Resources;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070067import org.osgi.service.component.ComponentContext;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070068import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020071import com.google.common.collect.ImmutableList;
72import com.google.common.collect.Sets;
73
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074import java.util.Collections;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070075import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076import java.util.LinkedList;
77import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070078import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070079import java.util.Set;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020080import java.util.stream.Collectors;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020081import java.util.stream.Stream;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070082
83import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080084import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070085
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) {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800159 deviceService = opticalView(deviceService);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700160 appId = coreService.registerApplication("org.onosproject.net.intent");
161 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700162 cfgService.registerProperties(getClass());
163 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700164 }
165
166 @Deactivate
167 public void deactivate() {
168 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700169 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700170 }
171
172 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800173 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700174 // Check if ports are OduClt ports
175 ConnectPoint src = intent.getSrc();
176 ConnectPoint dst = intent.getDst();
177 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
178 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
179 checkArgument(srcPort instanceof OduCltPort);
180 checkArgument(dstPort instanceof OduCltPort);
181
182 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
183
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200184 // Release of intent resources here is only a temporary solution for handling the
185 // case of recompiling due to intent restoration (when intent state is FAILED).
186 // TODO: try to release intent resources in IntentManager.
187 resourceService.release(intent.id());
188
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200189 // Check OduClt ports availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800190 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
191 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200192 // If ports are not available, compilation fails
193 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700194 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700195 }
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200196 List<Resource> ports = ImmutableList.of(srcPortResource, dstPortResource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700197
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200198 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800199 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
200 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200201
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800202 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
203 intent.getSignalType(), multiplexingSupported);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800204
205 if (connIntent != null && !multiplexingSupported) {
206 return compile(intent, src, dst, Optional.of(connIntent), ports, false);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200207 }
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
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800210 if (connIntent == null) {
211 return compile(intent, src, dst, Optional.empty(), ports, multiplexingSupported);
212 }
213
214 List<Resource> slots = availableSlotResources(connIntent.getSrc(), connIntent.getDst(),
215 intent.getSignalType());
216 if (slots.isEmpty()) {
217 return compile(intent, src, dst, Optional.empty(), ports, true);
218 }
219
220 return compile(intent, src, dst, Optional.of(connIntent), ImmutableList.<Resource>builder()
221 .addAll(ports).addAll(slots).build(), false);
222
223 }
224
225 private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst,
226 Optional<OpticalConnectivityIntent> existingConnectivity,
227 List<Resource> resources, boolean supportsMultiplexing) {
228 OpticalConnectivityIntent connectivityIntent;
229 List<Resource> required;
230 if (existingConnectivity.isPresent()) {
231 connectivityIntent = existingConnectivity.get();
232 required = resources;
233 } else {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700234 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800235 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700236
237 if (ochPorts == null) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700238 throw new OpticalIntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200239 }
240
241 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
242 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
243
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700244 // Create optical connectivity intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800245 connectivityIntent = OpticalConnectivityIntent.builder()
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700246 .appId(appId)
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700247 // TODO New top-level Intent created and submitted
248 // during compilation.
249 // We'll need to track inter-Intent dependency,
250 // but `key` field cannot be used for the purpose.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700251 .src(srcCP)
252 .dst(dstCP)
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800253 .signalType(ochPorts.getLeft().signalType())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700254 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700255 .build();
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800256
257 if (!supportsMultiplexing) {
258 required = resources;
259 } else {
260 List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
261 if (slots.isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700262 throw new OpticalIntentCompilationException("Unable to find Tributary Slots for intent " + intent);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800263 }
264 required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
265 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700266 }
267
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800268 if (resourceService.allocate(intent.id(), required).isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700269 throw new OpticalIntentCompilationException("Unable to allocate resources for intent " + intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800270 + ": resources=" + required);
271 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700272
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800273 intentService.submit(connectivityIntent);
274
275 // Save circuit to connectivity intent mapping
276 intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
277
278 FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().
279 flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class)))
280 .collect(Collectors.toSet()));
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800281 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700282 }
283
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800284 private List<Resource> availableSlotResources(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200285 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800286 int requestedTsNum = oduSignalType.tributarySlots();
287 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(src, dst);
288 if (commonTributarySlots.isEmpty()) {
289 return Collections.emptyList();
290 }
291 if (commonTributarySlots.size() < requestedTsNum) {
292 return Collections.emptyList();
293 }
294
295 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
296 .limit(requestedTsNum)
297 .collect(Collectors.toSet());
298
299 final List<ConnectPoint> portsList = ImmutableList.of(src, dst);
300 List<Resource> tributarySlotResources = portsList.stream()
301 .flatMap(cp -> tributarySlots
302 .stream()
303 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
304 .collect(Collectors.toList());
305
306 if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
307 log.debug("Resource allocation for {} on {} and {} failed (resource request: {})",
308 signalType, src, dst, tributarySlotResources);
309 return Collections.emptyList();
310 }
311 return tributarySlotResources;
312 }
313
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800314 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
315 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
316 // Create optical circuit intent
317 List<FlowRule> rules = new LinkedList<>();
318 // at the source: ODUCLT port mapping to OCH port
319 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
320 // at the destination: OCH port mapping to ODUCLT port
321 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
322
323 // Create flow rules for reverse path
324 if (higherIntent.isBidirectional()) {
325 // at the destination: OCH port mapping to ODUCLT port
326 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
327 // at the source: ODUCLT port mapping to OCH port
328 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
329 }
330
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700331 return new FlowRuleIntent(appId, higherIntent.key(), rules, higherIntent.resources());
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800332 }
333
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700334 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700335 * Returns existing and available optical connectivity intent that matches the given circuit intent.
336 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800337 * @param src source connect point of optical circuit intent
338 * @param dst destination connect point of optical circuit intent
339 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200340 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700341 * @return existing optical connectivity intent, null otherwise.
342 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800343 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
344 ConnectPoint dst,
345 CltSignalType signalType,
346 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200347
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200348 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200349
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800350 return Tools.stream(intentService.getIntents())
351 .filter(x -> x instanceof OpticalConnectivityIntent)
352 .map(x -> (OpticalConnectivityIntent) x)
353 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
354 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
355 .filter(x -> isAllowed(src, x.getSrc()))
356 .filter(x -> isAllowed(dst, x.getDst()))
357 .filter(x -> isAvailable(x.id()))
358 .filter(x -> !multiplexingSupported ||
359 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
360 .findFirst()
361 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700362 }
363
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800364 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
365 ConnectPoint staticPort = staticPort(circuitCp);
366 return staticPort == null || staticPort.equals(connectivityCp);
367 }
368
369 /**
370 * Checks if current allocations on given resource can satisfy request.
371 * If the resource is null, return true.
372 *
373 * @param resource the resource on which to map the intent
374 * @return true if the resource can accept the request, false otherwise
375 */
376 private boolean isAvailable(IntentId resource) {
377 if (resource == null) {
378 return true;
379 }
380
381 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
382
383 if (mapping == null) {
384 return true;
385 }
386
387 return mapping.size() < maxCapacity;
388 }
389
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800390 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
391 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200392 if (common.isEmpty()) {
393 log.debug("No available TributarySlots");
394 return false;
395 }
396 if (common.size() < requestedTsNum) {
397 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
398 return false;
399 }
400 return true;
401 }
402
Marc De Leenheer723f5532015-06-03 20:16:17 -0700403 private ConnectPoint staticPort(ConnectPoint connectPoint) {
404 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
405
406 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
407
408 // FIXME: need a better way to match the port
409 if (staticPort != null) {
410 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
411 if (staticPort.equals(p.number().name())) {
412 return new ConnectPoint(p.element().id(), p.number());
413 }
414 }
415 }
416
417 return null;
418 }
419
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800420 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
421 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
422 switch (signalType) {
423 case CLT_1GBE:
424 case CLT_10GBE:
425 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
426 return findPorts(src, dst, OduSignalType.ODU2)
427 .orElse(findPorts(src, dst, OduSignalType.ODU4).orElse(null));
428 case CLT_100GBE:
429 return findPorts(src, dst, OduSignalType.ODU4).orElse(null);
430 case CLT_40GBE:
431 default:
432 return null;
433 }
434 }
435
436 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
437 OduSignalType ochPortSignalType) {
438 return findAvailableOchPort(src, ochPortSignalType)
439 .flatMap(srcOch ->
440 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
441 }
442
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800443 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700444 // First see if the port mappings are constrained
445 ConnectPoint ochCP = staticPort(oduPort);
446
447 if (ochCP != null) {
448 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700449 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800450 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800451 .stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700452 .map(ResourceAllocation::consumerId)
453 .map(ResourceHelper::getIntentId)
454 .flatMap(Tools::stream)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800455 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700456
457 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800458 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700459 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800460 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700461 }
462
463 // No port constraints, so find any port that works
464 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700465
466 for (Port port : ports) {
467 if (!(port instanceof OchPort)) {
468 continue;
469 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200470 // This should be the first allocation on the OCH port
471 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
472 continue;
473 }
474 // OchPort is required to have the requested oduSignalType
475 if (((OchPort) port).signalType() != ochPortSignalType) {
476 continue;
477 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700478
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700479 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800480 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800481 .stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700482 .map(ResourceAllocation::consumerId)
483 .map(ResourceHelper::getIntentId)
484 .flatMap(Tools::stream)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800485 .findAny();
486
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700487 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800488 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700489 }
490 }
491
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800492 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700493 }
494
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700495 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700496 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700497 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700498 * @param src source port
499 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200500 * @param priority
501 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700502 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700503 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200504 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700505 checkArgument(src.deviceId().equals(dst.deviceId()));
506
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700507 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
508 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
509
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700510 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200511 if (!slots.isEmpty()) {
512 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
513 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
514 OduSignalType oduCltPortOduSignalType;
515 OduSignalType ochPortOduSignalType;
516
517 if (srcPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200518 oduCltPortOduSignalType =
519 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200520 ochPortOduSignalType = ((OchPort) dstPort).signalType();
521
522 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
523 // use Instruction of OduSignalId only in case of ODU Multiplexing
524 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200525 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200526 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
527 }
528 } else { // srcPort is OchPort
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200529 oduCltPortOduSignalType =
530 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200531 ochPortOduSignalType = ((OchPort) srcPort).signalType();
532
533 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
534 // use Criteria of OduSignalId only in case of ODU Multiplexing
535 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200536 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200537 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
538 }
539 }
540 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700541 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700542
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700543 FlowRule flowRule = DefaultFlowRule.builder()
544 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700545 .withSelector(selectorBuilder.build())
546 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400547 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700548 .fromApp(appId)
549 .makePermanent()
550 .build();
551
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700552 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700553 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200554
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800555 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800556 Driver driver = driverService.getDriver(cp.deviceId());
557 return driver != null
558 && driver.hasBehaviour(TributarySlotQuery.class)
559 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200560 }
561
562 /**
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200563 * Finds the common TributarySlots available on the two connect points.
564 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800565 * @param src source connect point
566 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200567 * @return set of common TributarySlots on both connect points
568 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800569 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
570 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
571 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200572 return Sets.intersection(forward, backward);
573 }
574
575 /**
576 * Finds the TributarySlots available on the connect point.
577 *
578 * @param cp connect point
579 * @return set of TributarySlots available on the connect point
580 */
581 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800582 return resourceService.getAvailableResourceValues(
583 Resources.discrete(cp.deviceId(), cp.port()).id(),
584 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200585 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700586}