blob: 8f538bc7a6c2ab332e119ebd01144d0911d34a91 [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.intent.impl.compiler;
17
Sho SHIMIZU12d02682016-02-24 13:55:12 -080018import com.google.common.base.Strings;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070019import org.apache.commons.lang3.tuple.Pair;
20import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070021import org.apache.felix.scr.annotations.Component;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070022import org.apache.felix.scr.annotations.Deactivate;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070023import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070027import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070031import org.onosproject.net.AnnotationKeys;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020032import org.onosproject.net.CltSignalType;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070033import org.onosproject.net.ConnectPoint;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020034import org.onosproject.net.DeviceId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070035import org.onosproject.net.OchPort;
36import org.onosproject.net.OduCltPort;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020037import org.onosproject.net.OduSignalId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070038import org.onosproject.net.OduSignalType;
39import org.onosproject.net.Port;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020040import org.onosproject.net.TributarySlot;
41import org.onosproject.net.behaviour.TributarySlotQuery;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070042import org.onosproject.net.device.DeviceService;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020043import org.onosproject.net.driver.Driver;
44import org.onosproject.net.driver.DriverService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070045import org.onosproject.net.flow.DefaultFlowRule;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.FlowRule;
49import org.onosproject.net.flow.TrafficSelector;
50import org.onosproject.net.flow.TrafficTreatment;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020051import org.onosproject.net.flow.criteria.Criteria;
52import org.onosproject.net.flow.instructions.Instructions;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070053import org.onosproject.net.intent.FlowRuleIntent;
54import org.onosproject.net.intent.Intent;
55import org.onosproject.net.intent.IntentCompiler;
56import org.onosproject.net.intent.IntentExtensionService;
57import org.onosproject.net.intent.IntentId;
58import org.onosproject.net.intent.IntentService;
59import org.onosproject.net.intent.OpticalCircuitIntent;
60import org.onosproject.net.intent.OpticalConnectivityIntent;
61import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070062import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080063import org.onosproject.net.newresource.Resource;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070064import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080065import org.onosproject.net.newresource.Resources;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080066import org.onosproject.net.intent.IntentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070067import org.onosproject.net.resource.link.LinkResourceAllocations;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070068import org.osgi.service.component.ComponentContext;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070069import org.slf4j.Logger;
70import org.slf4j.LoggerFactory;
71
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020072import com.google.common.collect.ImmutableList;
73import com.google.common.collect.Sets;
74
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070075import java.util.Collections;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070076import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070077import java.util.LinkedList;
78import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070079import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070080import java.util.Set;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020081import java.util.stream.Collectors;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070082
83import static com.google.common.base.Preconditions.checkArgument;
84
85/**
86 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
87 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070088@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070089public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
90
91 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
92
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070093 private static final int DEFAULT_MAX_CAPACITY = 10;
94
95 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
96 label = "Maximum utilization of an optical connection.")
97
98 private int maxCapacity = DEFAULT_MAX_CAPACITY;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected ComponentConfigService cfgService;
102
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected IntentExtensionService intentManager;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected CoreService coreService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected DeviceService deviceService;
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700113 protected ResourceService resourceService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700116 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
119 protected IntentService intentService;
120
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected DriverService driverService;
123
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700124 private ApplicationId appId;
125
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700126 @Modified
127 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200128 if (context == null) {
129 return;
130 }
131
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700132 Dictionary properties = context.getProperties();
133
134 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
135 String propertyString = Tools.get(properties, "maxCapacity");
136
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800137 //Ignore if propertyString is empty or null
138 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700139 try {
140 int temp = Integer.parseInt(propertyString);
141 //Ensure value is non-negative but allow zero as a way to shutdown the link
142 if (temp >= 0) {
143 maxCapacity = temp;
144 }
145 } catch (NumberFormatException e) {
146 //Malformed arguments lead to no change of value (user should be notified of error)
147 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
148 }
149 } else {
150 //Notify of empty value but do not return (other properties will also go in this function)
151 log.error("The value for maxCapacity was set to an empty value.");
152 }
153
154 }
155
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700156 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700157 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700158 appId = coreService.registerApplication("org.onosproject.net.intent");
159 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700160 cfgService.registerProperties(getClass());
161 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700162 }
163
164 @Deactivate
165 public void deactivate() {
166 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700167 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700168 }
169
170 @Override
171 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
172 Set<LinkResourceAllocations> resources) {
173 // Check if ports are OduClt ports
174 ConnectPoint src = intent.getSrc();
175 ConnectPoint dst = intent.getDst();
176 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
177 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
178 checkArgument(srcPort instanceof OduCltPort);
179 checkArgument(dstPort instanceof OduCltPort);
180
181 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
182
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200183 // Release of intent resources here is only a temporary solution for handling the
184 // case of recompiling due to intent restoration (when intent state is FAILED).
185 // TODO: try to release intent resources in IntentManager.
186 resourceService.release(intent.id());
187
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700188 // Reserve OduClt ports
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800189 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
190 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800191 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700192 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700193 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
194 }
195
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200196 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUd8f7dc42016-02-29 11:14:18 -0800197 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc(), intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200198
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200199 // slots are used only for devices supporting multiplexing
200 Set<TributarySlot> slots = Collections.emptySet();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700201
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800202 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
203 intent.getSignalType(), multiplexingSupported);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200204 if ((connIntent != null) && multiplexingSupported) {
205 // Allocate TributarySlots on existing OCH ports
206 slots = assignTributarySlots(intent, Pair.of(connIntent.getSrc(), connIntent.getDst()));
207 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700208
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200209 // Create optical connectivity intent if needed - no optical intent or not enough slots available
210 if (connIntent == null || (multiplexingSupported && slots.isEmpty())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700211 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800212 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700213
214 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200215 // Release port allocations if unsuccessful
216 resourceService.release(intent.id());
217 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
218 }
219
220 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
221 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
222
223 if (multiplexingSupported) {
224 // Allocate TributarySlots on OCH ports
225 slots = assignTributarySlots(intent, Pair.of(srcCP, dstCP));
226 if (slots.isEmpty()) {
227 // Release port allocations if unsuccessful
228 resourceService.release(intent.id());
229 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
230 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700231 }
232
233 // Create optical connectivity intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200234 OduSignalType signalType = ochPorts.getLeft().signalType();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700235 connIntent = OpticalConnectivityIntent.builder()
236 .appId(appId)
237 .src(srcCP)
238 .dst(dstCP)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200239 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700240 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700241 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700242 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700243 }
244
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700245 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700246 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700247
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800248 FlowRuleIntent circuitIntent = createFlowRule(intent, connIntent, slots);
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800249 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700250 }
251
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800252 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
253 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
254 // Create optical circuit intent
255 List<FlowRule> rules = new LinkedList<>();
256 // at the source: ODUCLT port mapping to OCH port
257 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
258 // at the destination: OCH port mapping to ODUCLT port
259 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
260
261 // Create flow rules for reverse path
262 if (higherIntent.isBidirectional()) {
263 // at the destination: OCH port mapping to ODUCLT port
264 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
265 // at the source: ODUCLT port mapping to OCH port
266 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
267 }
268
269 return new FlowRuleIntent(appId, rules, higherIntent.resources());
270 }
271
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700272 /**
273 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700274 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700275 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700276 * @param resource the resource on which to map the intent
277 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700278 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700279 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700280 if (resource == null) {
281 return true;
282 }
283
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700284 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700285
Marc De Leenheer723f5532015-06-03 20:16:17 -0700286 if (mapping == null) {
287 return true;
288 }
289
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700290 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700291 }
292
Sho SHIMIZU54fa5802016-02-29 11:29:06 -0800293 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
294 ConnectPoint srcStaticPort = staticPort(circuitCp);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700295 if (srcStaticPort != null) {
Sho SHIMIZU54fa5802016-02-29 11:29:06 -0800296 if (!srcStaticPort.equals(connectivityCp)) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700297 return false;
298 }
299 }
Marc De Leenheer723f5532015-06-03 20:16:17 -0700300 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700301 }
302
303 /**
304 * Returns existing and available optical connectivity intent that matches the given circuit intent.
305 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800306 * @param src source connect point of optical circuit intent
307 * @param dst destination connect point of optical circuit intent
308 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200309 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700310 * @return existing optical connectivity intent, null otherwise.
311 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800312 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
313 ConnectPoint dst,
314 CltSignalType signalType,
315 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200316
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800317 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200318
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700319 for (Intent intent : intentService.getIntents()) {
320 if (!(intent instanceof OpticalConnectivityIntent)) {
321 continue;
322 }
323
324 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
325
Marc De Leenheer723f5532015-06-03 20:16:17 -0700326 // Ignore if the intents don't have identical src and dst devices
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200327 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) ||
Marc De Leenheer723f5532015-06-03 20:16:17 -0700328 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
329 continue;
330 }
331
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800332 if (!(isAllowed(src, connIntent.getSrc()) && isAllowed(dst, connIntent.getDst()))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700333 continue;
334 }
335
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200336 if (!isAvailable(connIntent.id())) {
337 continue;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700338 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200339
340 if (multiplexingSupported) {
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800341 if (!isAvailableTributarySlots(connIntent.getSrc(), connIntent.getDst(),
342 oduSignalType.tributarySlots())) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200343 continue;
344 }
345 }
346
347 return connIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700348 }
349
350 return null;
351 }
352
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800353 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
354 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200355 if (common.isEmpty()) {
356 log.debug("No available TributarySlots");
357 return false;
358 }
359 if (common.size() < requestedTsNum) {
360 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
361 return false;
362 }
363 return true;
364 }
365
366 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
367 Pair<ConnectPoint, ConnectPoint> ports) {
368
369 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
370 int requestedTsNum = oduSignalType.tributarySlots();
371 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
372 if (commonTributarySlots.isEmpty()) {
373 return Collections.emptySet();
374 }
375 if (commonTributarySlots.size() < requestedTsNum) {
376 return Collections.emptySet();
377 }
378
379 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
380 .limit(requestedTsNum)
381 .collect(Collectors.toSet());
382
383 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
384 List<Resource> tributarySlotResources = portsList.stream()
385 .flatMap(cp -> tributarySlots
386 .stream()
387 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
388 .collect(Collectors.toList());
389
390 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
391 if (allocations.isEmpty()) {
392 log.debug("Resource allocation for {} failed (resource request: {})",
393 intent, tributarySlotResources);
394 return Collections.emptySet();
395 }
396 return tributarySlots;
397 }
398
Marc De Leenheer723f5532015-06-03 20:16:17 -0700399 private ConnectPoint staticPort(ConnectPoint connectPoint) {
400 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
401
402 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
403
404 // FIXME: need a better way to match the port
405 if (staticPort != null) {
406 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
407 if (staticPort.equals(p.number().name())) {
408 return new ConnectPoint(p.element().id(), p.number());
409 }
410 }
411 }
412
413 return null;
414 }
415
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200416 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700417 // First see if the port mappings are constrained
418 ConnectPoint ochCP = staticPort(oduPort);
419
420 if (ochCP != null) {
421 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700422 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800423 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800424 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700425 .map(ResourceAllocation::consumer)
426 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800427 .map(x -> (IntentId) x)
428 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700429
430 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700431 return ochPort;
432 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200433 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700434 }
435
436 // No port constraints, so find any port that works
437 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700438
439 for (Port port : ports) {
440 if (!(port instanceof OchPort)) {
441 continue;
442 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200443 // This should be the first allocation on the OCH port
444 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
445 continue;
446 }
447 // OchPort is required to have the requested oduSignalType
448 if (((OchPort) port).signalType() != ochPortSignalType) {
449 continue;
450 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700451
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700452 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800453 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800454 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700455 .map(ResourceAllocation::consumer)
456 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800457 .map(x -> (IntentId) x)
458 .findAny();
459
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700460 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700461 return (OchPort) port;
462 }
463 }
464
465 return null;
466 }
467
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800468 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200469 Pair<OchPort, OchPort> ochPorts = null;
470 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800471 switch (signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200472 case CLT_1GBE:
473 case CLT_10GBE:
474 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800475 ochPorts = findPorts(src, dst, OduSignalType.ODU2);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200476 if (ochPorts == null) {
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800477 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200478 }
479 break;
480 case CLT_100GBE:
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800481 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200482 break;
483 case CLT_40GBE:
484 default:
485 break;
486 }
487 return ochPorts;
488 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700489
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800490 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, OduSignalType ochPortSignalType) {
491 OchPort srcPort = findAvailableOchPort(src, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700492 if (srcPort == null) {
493 return null;
494 }
495
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800496 OchPort dstPort = findAvailableOchPort(dst, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700497 if (dstPort == null) {
498 return null;
499 }
500
501 return Pair.of(srcPort, dstPort);
502 }
503
504 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700505 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700506 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700507 * @param src source port
508 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200509 * @param priority
510 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700511 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700512 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200513 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700514 checkArgument(src.deviceId().equals(dst.deviceId()));
515
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700516 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
517 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
518
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700519 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200520 if (!slots.isEmpty()) {
521 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
522 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
523 OduSignalType oduCltPortOduSignalType;
524 OduSignalType ochPortOduSignalType;
525
526 if (srcPort instanceof OduCltPort) {
527 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
528 ochPortOduSignalType = ((OchPort) dstPort).signalType();
529
530 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
531 // use Instruction of OduSignalId only in case of ODU Multiplexing
532 if (oduCltPortOduSignalType != ochPortOduSignalType) {
533 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
534 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
535 }
536 } else { // srcPort is OchPort
537 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
538 ochPortOduSignalType = ((OchPort) srcPort).signalType();
539
540 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
541 // use Criteria of OduSignalId only in case of ODU Multiplexing
542 if (oduCltPortOduSignalType != ochPortOduSignalType) {
543 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
544 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
545 }
546 }
547 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700548 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700549
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700550 FlowRule flowRule = DefaultFlowRule.builder()
551 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700552 .withSelector(selectorBuilder.build())
553 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400554 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700555 .fromApp(appId)
556 .makePermanent()
557 .build();
558
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700559 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700560 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200561
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200562 protected OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200563 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
564 int tributarySlotLen = ochPortSignalType.tributarySlots();
565 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
566
567 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
568 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
569 }
570
571 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
572 return (int) tributarySlots.stream().findFirst().get().index();
573 }
574
575 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
576 Driver driver = driverService.getDriver(deviceId);
577 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
578 }
579
Sho SHIMIZUd8f7dc42016-02-29 11:14:18 -0800580 private boolean isMultiplexingSupported(ConnectPoint src, ConnectPoint dst) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200581 if (!isTributarySlotBehaviourSupported(src.deviceId()) ||
582 !isTributarySlotBehaviourSupported(dst.deviceId())) {
583 return false;
584 }
585
586 ConnectPoint srcStaticPort = staticPort(src);
587 if (srcStaticPort != null) {
588 return false;
589 }
590 ConnectPoint dstStaticPort = staticPort(dst);
591 if (dstStaticPort != null) {
592 return false;
593 }
594
595 return true;
596 }
597
598 /**
599 * Maps from Intent's OduClt SignalType to OduSignalType.
600 *
601 * @param cltSignalType OduClt port signal type
602 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
603 */
604 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
605 OduSignalType oduSignalType = OduSignalType.ODU0;
606 switch (cltSignalType) {
607 case CLT_1GBE:
608 oduSignalType = OduSignalType.ODU0;
609 break;
610 case CLT_10GBE:
611 oduSignalType = OduSignalType.ODU2;
612 break;
613 case CLT_40GBE:
614 oduSignalType = OduSignalType.ODU3;
615 break;
616 case CLT_100GBE:
617 oduSignalType = OduSignalType.ODU4;
618 break;
619 default:
620 log.error("Unsupported CltSignalType {}", cltSignalType);
621 break;
622 }
623 return oduSignalType;
624 }
625
626 /**
627 * Finds the common TributarySlots available on the two connect points.
628 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800629 * @param src source connect point
630 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200631 * @return set of common TributarySlots on both connect points
632 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800633 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
634 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
635 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200636 return Sets.intersection(forward, backward);
637 }
638
639 /**
640 * Finds the TributarySlots available on the connect point.
641 *
642 * @param cp connect point
643 * @return set of TributarySlots available on the connect point
644 */
645 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800646 return resourceService.getAvailableResourceValues(
647 Resources.discrete(cp.deviceId(), cp.port()).id(),
648 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200649 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700650}