blob: a44d837debf3145bf18d1ce6d047771354739fed [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;
Luca Prete670ac5d2017-02-03 15:55:43 -080060import org.onosproject.net.intent.PathIntent;
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
Konstantinos Kanonakis198b0392017-02-22 13:18:09 -060075import java.util.Arrays;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076import java.util.Collections;
Konstantinos Kanonakis198b0392017-02-22 13:18:09 -060077import java.util.Comparator;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070078import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070079import java.util.LinkedList;
80import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070081import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070082import java.util.Set;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020083import java.util.stream.Collectors;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020084import java.util.stream.Stream;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070085
86import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080087import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070088
89/**
90 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
91 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070092@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070093public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
94
95 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
96
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070097 private static final int DEFAULT_MAX_CAPACITY = 10;
98
99 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
100 label = "Maximum utilization of an optical connection.")
101
102 private int maxCapacity = DEFAULT_MAX_CAPACITY;
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected ComponentConfigService cfgService;
106
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected IntentExtensionService intentManager;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected CoreService coreService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected DeviceService deviceService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700117 protected ResourceService resourceService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700120 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected IntentService intentService;
124
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected DriverService driverService;
127
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700128 private ApplicationId appId;
129
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700130 @Modified
131 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200132 if (context == null) {
133 return;
134 }
135
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700136 Dictionary properties = context.getProperties();
137
138 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
139 String propertyString = Tools.get(properties, "maxCapacity");
140
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800141 //Ignore if propertyString is empty or null
142 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700143 try {
144 int temp = Integer.parseInt(propertyString);
145 //Ensure value is non-negative but allow zero as a way to shutdown the link
146 if (temp >= 0) {
147 maxCapacity = temp;
148 }
149 } catch (NumberFormatException e) {
150 //Malformed arguments lead to no change of value (user should be notified of error)
151 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
152 }
153 } else {
154 //Notify of empty value but do not return (other properties will also go in this function)
155 log.error("The value for maxCapacity was set to an empty value.");
156 }
157
158 }
159
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700160 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700161 public void activate(ComponentContext context) {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800162 deviceService = opticalView(deviceService);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700163 appId = coreService.registerApplication("org.onosproject.net.intent");
164 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700165 cfgService.registerProperties(getClass());
166 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700167 }
168
169 @Deactivate
170 public void deactivate() {
171 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700172 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700173 }
174
175 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800176 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700177 // Check if ports are OduClt ports
178 ConnectPoint src = intent.getSrc();
179 ConnectPoint dst = intent.getDst();
180 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
181 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
182 checkArgument(srcPort instanceof OduCltPort);
183 checkArgument(dstPort instanceof OduCltPort);
184
185 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
186
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200187 // Release of intent resources here is only a temporary solution for handling the
188 // case of recompiling due to intent restoration (when intent state is FAILED).
189 // TODO: try to release intent resources in IntentManager.
190 resourceService.release(intent.id());
191
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200192 // Check OduClt ports availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800193 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
194 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200195 // If ports are not available, compilation fails
196 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700197 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700198 }
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200199 List<Resource> ports = ImmutableList.of(srcPortResource, dstPortResource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700200
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200201 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800202 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
203 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200204
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800205 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
206 intent.getSignalType(), multiplexingSupported);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800207
208 if (connIntent != null && !multiplexingSupported) {
209 return compile(intent, src, dst, Optional.of(connIntent), ports, false);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200210 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700211
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200212 // Create optical connectivity intent if needed - no optical intent or not enough slots available
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800213 if (connIntent == null) {
214 return compile(intent, src, dst, Optional.empty(), ports, multiplexingSupported);
215 }
216
217 List<Resource> slots = availableSlotResources(connIntent.getSrc(), connIntent.getDst(),
218 intent.getSignalType());
219 if (slots.isEmpty()) {
220 return compile(intent, src, dst, Optional.empty(), ports, true);
221 }
222
223 return compile(intent, src, dst, Optional.of(connIntent), ImmutableList.<Resource>builder()
224 .addAll(ports).addAll(slots).build(), false);
225
226 }
227
228 private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst,
229 Optional<OpticalConnectivityIntent> existingConnectivity,
230 List<Resource> resources, boolean supportsMultiplexing) {
231 OpticalConnectivityIntent connectivityIntent;
232 List<Resource> required;
233 if (existingConnectivity.isPresent()) {
234 connectivityIntent = existingConnectivity.get();
235 required = resources;
236 } else {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700237 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800238 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700239
240 if (ochPorts == null) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700241 throw new OpticalIntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200242 }
243
244 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
245 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
246
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700247 // Create optical connectivity intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800248 connectivityIntent = OpticalConnectivityIntent.builder()
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700249 .appId(appId)
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700250 // TODO New top-level Intent created and submitted
251 // during compilation.
252 // We'll need to track inter-Intent dependency,
253 // but `key` field cannot be used for the purpose.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700254 .src(srcCP)
255 .dst(dstCP)
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800256 .signalType(ochPorts.getLeft().signalType())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700257 .bidirectional(intent.isBidirectional())
Luca Prete670ac5d2017-02-03 15:55:43 -0800258 .resourceGroup(intent.resourceGroup())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700259 .build();
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800260
261 if (!supportsMultiplexing) {
262 required = resources;
263 } else {
264 List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
265 if (slots.isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700266 throw new OpticalIntentCompilationException("Unable to find Tributary Slots for intent " + intent);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800267 }
268 required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
269 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700270 }
271
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800272 if (resourceService.allocate(intent.id(), required).isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700273 throw new OpticalIntentCompilationException("Unable to allocate resources for intent " + intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800274 + ": resources=" + required);
275 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700276
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800277 intentService.submit(connectivityIntent);
278
279 // Save circuit to connectivity intent mapping
280 intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
281
282 FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().
283 flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class)))
284 .collect(Collectors.toSet()));
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800285 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700286 }
287
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800288 private List<Resource> availableSlotResources(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200289 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800290 int requestedTsNum = oduSignalType.tributarySlots();
291 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(src, dst);
292 if (commonTributarySlots.isEmpty()) {
293 return Collections.emptyList();
294 }
295 if (commonTributarySlots.size() < requestedTsNum) {
296 return Collections.emptyList();
297 }
298
299 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
300 .limit(requestedTsNum)
301 .collect(Collectors.toSet());
302
303 final List<ConnectPoint> portsList = ImmutableList.of(src, dst);
304 List<Resource> tributarySlotResources = portsList.stream()
305 .flatMap(cp -> tributarySlots
306 .stream()
Ray Milkey88cc3432017-03-30 17:19:08 -0700307 .map(ts -> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800308 .collect(Collectors.toList());
309
310 if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
311 log.debug("Resource allocation for {} on {} and {} failed (resource request: {})",
312 signalType, src, dst, tributarySlotResources);
313 return Collections.emptyList();
314 }
315 return tributarySlotResources;
316 }
317
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800318 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
319 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
320 // Create optical circuit intent
321 List<FlowRule> rules = new LinkedList<>();
322 // at the source: ODUCLT port mapping to OCH port
323 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
324 // at the destination: OCH port mapping to ODUCLT port
325 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
326
327 // Create flow rules for reverse path
328 if (higherIntent.isBidirectional()) {
329 // at the destination: OCH port mapping to ODUCLT port
330 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
331 // at the source: ODUCLT port mapping to OCH port
332 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
333 }
334
Luca Prete670ac5d2017-02-03 15:55:43 -0800335 return new FlowRuleIntent(appId, higherIntent.key(), rules,
336 higherIntent.resources(),
337 PathIntent.ProtectionType.PRIMARY,
338 higherIntent.resourceGroup());
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800339 }
340
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700341 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700342 * Returns existing and available optical connectivity intent that matches the given circuit intent.
343 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800344 * @param src source connect point of optical circuit intent
345 * @param dst destination connect point of optical circuit intent
346 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200347 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700348 * @return existing optical connectivity intent, null otherwise.
349 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800350 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
351 ConnectPoint dst,
352 CltSignalType signalType,
353 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200354
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200355 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200356
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800357 return Tools.stream(intentService.getIntents())
358 .filter(x -> x instanceof OpticalConnectivityIntent)
359 .map(x -> (OpticalConnectivityIntent) x)
360 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
361 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
362 .filter(x -> isAllowed(src, x.getSrc()))
363 .filter(x -> isAllowed(dst, x.getDst()))
364 .filter(x -> isAvailable(x.id()))
365 .filter(x -> !multiplexingSupported ||
366 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
367 .findFirst()
368 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700369 }
370
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800371 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
372 ConnectPoint staticPort = staticPort(circuitCp);
373 return staticPort == null || staticPort.equals(connectivityCp);
374 }
375
376 /**
377 * Checks if current allocations on given resource can satisfy request.
378 * If the resource is null, return true.
379 *
380 * @param resource the resource on which to map the intent
381 * @return true if the resource can accept the request, false otherwise
382 */
383 private boolean isAvailable(IntentId resource) {
384 if (resource == null) {
385 return true;
386 }
387
388 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
389
390 if (mapping == null) {
391 return true;
392 }
393
394 return mapping.size() < maxCapacity;
395 }
396
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800397 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
398 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200399 if (common.isEmpty()) {
400 log.debug("No available TributarySlots");
401 return false;
402 }
403 if (common.size() < requestedTsNum) {
404 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
405 return false;
406 }
407 return true;
408 }
409
Marc De Leenheer723f5532015-06-03 20:16:17 -0700410 private ConnectPoint staticPort(ConnectPoint connectPoint) {
411 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
412
413 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
414
415 // FIXME: need a better way to match the port
416 if (staticPort != null) {
417 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
418 if (staticPort.equals(p.number().name())) {
419 return new ConnectPoint(p.element().id(), p.number());
420 }
421 }
422 }
423
424 return null;
425 }
426
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800427 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
428 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Konstantinos Kanonakis198b0392017-02-22 13:18:09 -0600429 return Arrays.asList(OduSignalType.values()).stream()
430 .sorted(Comparator.comparingLong(OduSignalType::bitRate))
431 .filter(oduSignalType -> oduSignalType.bitRate() >= signalType.bitRate())
432 .map(oduSignalType -> findPorts(src, dst, oduSignalType))
433 .flatMap(Tools::stream)
434 .findFirst()
435 .orElse(null);
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800436 }
437
438 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
439 OduSignalType ochPortSignalType) {
440 return findAvailableOchPort(src, ochPortSignalType)
441 .flatMap(srcOch ->
442 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
443 }
444
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800445 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700446 // First see if the port mappings are constrained
447 ConnectPoint ochCP = staticPort(oduPort);
448
449 if (ochCP != null) {
450 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700451 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800452 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800453 .stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700454 .map(ResourceAllocation::consumerId)
455 .map(ResourceHelper::getIntentId)
456 .flatMap(Tools::stream)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800457 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700458
459 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800460 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700461 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800462 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700463 }
464
465 // No port constraints, so find any port that works
466 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700467
468 for (Port port : ports) {
469 if (!(port instanceof OchPort)) {
470 continue;
471 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200472 // This should be the first allocation on the OCH port
473 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
474 continue;
475 }
476 // OchPort is required to have the requested oduSignalType
477 if (((OchPort) port).signalType() != ochPortSignalType) {
478 continue;
479 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700480
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700481 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800482 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800483 .stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700484 .map(ResourceAllocation::consumerId)
485 .map(ResourceHelper::getIntentId)
486 .flatMap(Tools::stream)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800487 .findAny();
488
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700489 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800490 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700491 }
492 }
493
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800494 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700495 }
496
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700497 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700498 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700499 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700500 * @param src source port
501 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200502 * @param priority
503 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700504 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700505 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200506 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700507 checkArgument(src.deviceId().equals(dst.deviceId()));
508
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700509 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
510 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
511
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700512 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200513 if (!slots.isEmpty()) {
514 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
515 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
516 OduSignalType oduCltPortOduSignalType;
517 OduSignalType ochPortOduSignalType;
518
519 if (srcPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200520 oduCltPortOduSignalType =
521 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200522 ochPortOduSignalType = ((OchPort) dstPort).signalType();
523
524 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
525 // use Instruction of OduSignalId only in case of ODU Multiplexing
526 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200527 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200528 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
529 }
530 } else { // srcPort is OchPort
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200531 oduCltPortOduSignalType =
532 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200533 ochPortOduSignalType = ((OchPort) srcPort).signalType();
534
535 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
536 // use Criteria of OduSignalId only in case of ODU Multiplexing
537 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200538 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200539 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
540 }
541 }
542 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700543 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700544
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700545 FlowRule flowRule = DefaultFlowRule.builder()
546 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700547 .withSelector(selectorBuilder.build())
548 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400549 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700550 .fromApp(appId)
551 .makePermanent()
552 .build();
553
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700554 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700555 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200556
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800557 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800558 Driver driver = driverService.getDriver(cp.deviceId());
559 return driver != null
560 && driver.hasBehaviour(TributarySlotQuery.class)
561 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200562 }
563
564 /**
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200565 * Finds the common TributarySlots available on the two connect points.
566 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800567 * @param src source connect point
568 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200569 * @return set of common TributarySlots on both connect points
570 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800571 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
572 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
573 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200574 return Sets.intersection(forward, backward);
575 }
576
577 /**
578 * Finds the TributarySlots available on the connect point.
579 *
580 * @param cp connect point
581 * @return set of TributarySlots available on the connect point
582 */
583 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800584 return resourceService.getAvailableResourceValues(
585 Resources.discrete(cp.deviceId(), cp.port()).id(),
586 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200587 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700588}