blob: 45711a401b371bd686204eaa9ad1b88e9847b784 [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 SHIMIZUcaf0b342016-02-29 13:49:04 -0800197 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
198 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200199
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200200 // slots are used only for devices supporting multiplexing
201 Set<TributarySlot> slots = Collections.emptySet();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700202
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800203 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
204 intent.getSignalType(), multiplexingSupported);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200205 if ((connIntent != null) && multiplexingSupported) {
206 // Allocate TributarySlots on existing OCH ports
207 slots = assignTributarySlots(intent, Pair.of(connIntent.getSrc(), connIntent.getDst()));
208 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700209
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200210 // Create optical connectivity intent if needed - no optical intent or not enough slots available
211 if (connIntent == null || (multiplexingSupported && slots.isEmpty())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700212 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800213 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700214
215 if (ochPorts == null) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200216 // Release port allocations if unsuccessful
217 resourceService.release(intent.id());
218 throw new IntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
219 }
220
221 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
222 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
223
224 if (multiplexingSupported) {
225 // Allocate TributarySlots on OCH ports
226 slots = assignTributarySlots(intent, Pair.of(srcCP, dstCP));
227 if (slots.isEmpty()) {
228 // Release port allocations if unsuccessful
229 resourceService.release(intent.id());
230 throw new IntentCompilationException("Unable to find Tributary Slots for intent " + intent);
231 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700232 }
233
234 // Create optical connectivity intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200235 OduSignalType signalType = ochPorts.getLeft().signalType();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700236 connIntent = OpticalConnectivityIntent.builder()
237 .appId(appId)
238 .src(srcCP)
239 .dst(dstCP)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200240 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700241 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700242 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700243 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700244 }
245
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700246 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700247 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700248
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800249 FlowRuleIntent circuitIntent = createFlowRule(intent, connIntent, slots);
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800250 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700251 }
252
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800253 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
254 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
255 // Create optical circuit intent
256 List<FlowRule> rules = new LinkedList<>();
257 // at the source: ODUCLT port mapping to OCH port
258 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
259 // at the destination: OCH port mapping to ODUCLT port
260 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
261
262 // Create flow rules for reverse path
263 if (higherIntent.isBidirectional()) {
264 // at the destination: OCH port mapping to ODUCLT port
265 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
266 // at the source: ODUCLT port mapping to OCH port
267 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
268 }
269
270 return new FlowRuleIntent(appId, rules, higherIntent.resources());
271 }
272
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700273 /**
274 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700275 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700276 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700277 * @param resource the resource on which to map the intent
278 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700279 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700280 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700281 if (resource == null) {
282 return true;
283 }
284
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700285 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700286
Marc De Leenheer723f5532015-06-03 20:16:17 -0700287 if (mapping == null) {
288 return true;
289 }
290
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700291 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700292 }
293
Sho SHIMIZU54fa5802016-02-29 11:29:06 -0800294 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
295 ConnectPoint srcStaticPort = staticPort(circuitCp);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700296 if (srcStaticPort != null) {
Sho SHIMIZU54fa5802016-02-29 11:29:06 -0800297 if (!srcStaticPort.equals(connectivityCp)) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700298 return false;
299 }
300 }
Marc De Leenheer723f5532015-06-03 20:16:17 -0700301 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700302 }
303
304 /**
305 * Returns existing and available optical connectivity intent that matches the given circuit intent.
306 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800307 * @param src source connect point of optical circuit intent
308 * @param dst destination connect point of optical circuit intent
309 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200310 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700311 * @return existing optical connectivity intent, null otherwise.
312 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800313 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
314 ConnectPoint dst,
315 CltSignalType signalType,
316 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200317
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800318 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200319
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800320 return Tools.stream(intentService.getIntents())
321 .filter(x -> x instanceof OpticalConnectivityIntent)
322 .map(x -> (OpticalConnectivityIntent) x)
323 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
324 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
325 .filter(x -> isAllowed(src, x.getSrc()))
326 .filter(x -> isAllowed(dst, x.getDst()))
327 .filter(x -> isAvailable(x.id()))
328 .filter(x -> !multiplexingSupported ||
329 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
330 .findFirst()
331 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700332 }
333
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800334 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
335 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200336 if (common.isEmpty()) {
337 log.debug("No available TributarySlots");
338 return false;
339 }
340 if (common.size() < requestedTsNum) {
341 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
342 return false;
343 }
344 return true;
345 }
346
347 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
348 Pair<ConnectPoint, ConnectPoint> ports) {
349
350 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
351 int requestedTsNum = oduSignalType.tributarySlots();
352 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
353 if (commonTributarySlots.isEmpty()) {
354 return Collections.emptySet();
355 }
356 if (commonTributarySlots.size() < requestedTsNum) {
357 return Collections.emptySet();
358 }
359
360 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
361 .limit(requestedTsNum)
362 .collect(Collectors.toSet());
363
364 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
365 List<Resource> tributarySlotResources = portsList.stream()
366 .flatMap(cp -> tributarySlots
367 .stream()
368 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
369 .collect(Collectors.toList());
370
371 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
372 if (allocations.isEmpty()) {
373 log.debug("Resource allocation for {} failed (resource request: {})",
374 intent, tributarySlotResources);
375 return Collections.emptySet();
376 }
377 return tributarySlots;
378 }
379
Marc De Leenheer723f5532015-06-03 20:16:17 -0700380 private ConnectPoint staticPort(ConnectPoint connectPoint) {
381 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
382
383 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
384
385 // FIXME: need a better way to match the port
386 if (staticPort != null) {
387 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
388 if (staticPort.equals(p.number().name())) {
389 return new ConnectPoint(p.element().id(), p.number());
390 }
391 }
392 }
393
394 return null;
395 }
396
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200397 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700398 // First see if the port mappings are constrained
399 ConnectPoint ochCP = staticPort(oduPort);
400
401 if (ochCP != null) {
402 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700403 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800404 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800405 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700406 .map(ResourceAllocation::consumer)
407 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800408 .map(x -> (IntentId) x)
409 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700410
411 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700412 return ochPort;
413 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200414 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700415 }
416
417 // No port constraints, so find any port that works
418 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700419
420 for (Port port : ports) {
421 if (!(port instanceof OchPort)) {
422 continue;
423 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200424 // This should be the first allocation on the OCH port
425 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
426 continue;
427 }
428 // OchPort is required to have the requested oduSignalType
429 if (((OchPort) port).signalType() != ochPortSignalType) {
430 continue;
431 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700432
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700433 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800434 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800435 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700436 .map(ResourceAllocation::consumer)
437 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800438 .map(x -> (IntentId) x)
439 .findAny();
440
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700441 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700442 return (OchPort) port;
443 }
444 }
445
446 return null;
447 }
448
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800449 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200450 Pair<OchPort, OchPort> ochPorts = null;
451 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800452 switch (signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200453 case CLT_1GBE:
454 case CLT_10GBE:
455 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800456 ochPorts = findPorts(src, dst, OduSignalType.ODU2);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200457 if (ochPorts == null) {
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800458 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200459 }
460 break;
461 case CLT_100GBE:
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800462 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200463 break;
464 case CLT_40GBE:
465 default:
466 break;
467 }
468 return ochPorts;
469 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700470
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800471 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, OduSignalType ochPortSignalType) {
472 OchPort srcPort = findAvailableOchPort(src, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700473 if (srcPort == null) {
474 return null;
475 }
476
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800477 OchPort dstPort = findAvailableOchPort(dst, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700478 if (dstPort == null) {
479 return null;
480 }
481
482 return Pair.of(srcPort, dstPort);
483 }
484
485 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700486 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700487 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700488 * @param src source port
489 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200490 * @param priority
491 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700492 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700493 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200494 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700495 checkArgument(src.deviceId().equals(dst.deviceId()));
496
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700497 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
498 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
499
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700500 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200501 if (!slots.isEmpty()) {
502 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
503 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
504 OduSignalType oduCltPortOduSignalType;
505 OduSignalType ochPortOduSignalType;
506
507 if (srcPort instanceof OduCltPort) {
508 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
509 ochPortOduSignalType = ((OchPort) dstPort).signalType();
510
511 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
512 // use Instruction of OduSignalId only in case of ODU Multiplexing
513 if (oduCltPortOduSignalType != ochPortOduSignalType) {
514 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
515 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
516 }
517 } else { // srcPort is OchPort
518 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
519 ochPortOduSignalType = ((OchPort) srcPort).signalType();
520
521 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
522 // use Criteria of OduSignalId only in case of ODU Multiplexing
523 if (oduCltPortOduSignalType != ochPortOduSignalType) {
524 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
525 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
526 }
527 }
528 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700529 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700530
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700531 FlowRule flowRule = DefaultFlowRule.builder()
532 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700533 .withSelector(selectorBuilder.build())
534 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400535 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700536 .fromApp(appId)
537 .makePermanent()
538 .build();
539
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700540 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700541 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200542
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200543 protected OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200544 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
545 int tributarySlotLen = ochPortSignalType.tributarySlots();
546 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
547
548 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
549 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
550 }
551
552 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
553 return (int) tributarySlots.stream().findFirst().get().index();
554 }
555
556 private boolean isTributarySlotBehaviourSupported(DeviceId deviceId) {
557 Driver driver = driverService.getDriver(deviceId);
558 return (driver != null && driver.hasBehaviour(TributarySlotQuery.class));
559 }
560
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800561 private boolean isMultiplexingSupported(ConnectPoint cp) {
562 return isTributarySlotBehaviourSupported(cp.deviceId()) && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200563 }
564
565 /**
566 * Maps from Intent's OduClt SignalType to OduSignalType.
567 *
568 * @param cltSignalType OduClt port signal type
569 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
570 */
571 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200572 switch (cltSignalType) {
573 case CLT_1GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800574 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200575 case CLT_10GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800576 return OduSignalType.ODU2;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200577 case CLT_40GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800578 return OduSignalType.ODU3;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200579 case CLT_100GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800580 return OduSignalType.ODU4;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200581 default:
582 log.error("Unsupported CltSignalType {}", cltSignalType);
Sho SHIMIZU88799912016-02-29 13:34:12 -0800583 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200584 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200585 }
586
587 /**
588 * Finds the common TributarySlots available on the two connect points.
589 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800590 * @param src source connect point
591 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200592 * @return set of common TributarySlots on both connect points
593 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800594 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
595 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
596 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200597 return Sets.intersection(forward, backward);
598 }
599
600 /**
601 * Finds the TributarySlots available on the connect point.
602 *
603 * @param cp connect point
604 * @return set of TributarySlots available on the connect point
605 */
606 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800607 return resourceService.getAvailableResourceValues(
608 Resources.discrete(cp.deviceId(), cp.port()).id(),
609 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200610 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700611}