blob: cf03c318a8e1fed493a6f49e489bbea42ba1239d [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;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070034import org.onosproject.net.OchPort;
35import org.onosproject.net.OduCltPort;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020036import org.onosproject.net.OduSignalId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070037import org.onosproject.net.OduSignalType;
38import 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;
60import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070061import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080062import org.onosproject.net.newresource.Resource;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070063import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080064import org.onosproject.net.newresource.Resources;
Sho SHIMIZU739873b2016-02-23 17:02:12 -080065import org.onosproject.net.intent.IntentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070066import org.onosproject.net.resource.link.LinkResourceAllocations;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070067import org.osgi.service.component.ComponentContext;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070068import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020071import com.google.common.collect.ImmutableList;
72import com.google.common.collect.Sets;
73
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074import java.util.Collections;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070075import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076import java.util.LinkedList;
77import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070078import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070079import java.util.Set;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020080import java.util.stream.Collectors;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070081
82import static com.google.common.base.Preconditions.checkArgument;
83
84/**
85 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
86 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070087@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070088public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
89
90 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
91
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070092 private static final int DEFAULT_MAX_CAPACITY = 10;
93
94 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
95 label = "Maximum utilization of an optical connection.")
96
97 private int maxCapacity = DEFAULT_MAX_CAPACITY;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected ComponentConfigService cfgService;
101
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected IntentExtensionService intentManager;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected CoreService coreService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected DeviceService deviceService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700112 protected ResourceService resourceService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700115 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected IntentService intentService;
119
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected DriverService driverService;
122
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700123 private ApplicationId appId;
124
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700125 @Modified
126 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200127 if (context == null) {
128 return;
129 }
130
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700131 Dictionary properties = context.getProperties();
132
133 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
134 String propertyString = Tools.get(properties, "maxCapacity");
135
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800136 //Ignore if propertyString is empty or null
137 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700138 try {
139 int temp = Integer.parseInt(propertyString);
140 //Ensure value is non-negative but allow zero as a way to shutdown the link
141 if (temp >= 0) {
142 maxCapacity = temp;
143 }
144 } catch (NumberFormatException e) {
145 //Malformed arguments lead to no change of value (user should be notified of error)
146 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
147 }
148 } else {
149 //Notify of empty value but do not return (other properties will also go in this function)
150 log.error("The value for maxCapacity was set to an empty value.");
151 }
152
153 }
154
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700155 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700156 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700157 appId = coreService.registerApplication("org.onosproject.net.intent");
158 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700159 cfgService.registerProperties(getClass());
160 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700161 }
162
163 @Deactivate
164 public void deactivate() {
165 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700166 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700167 }
168
169 @Override
170 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
171 Set<LinkResourceAllocations> resources) {
172 // Check if ports are OduClt ports
173 ConnectPoint src = intent.getSrc();
174 ConnectPoint dst = intent.getDst();
175 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
176 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
177 checkArgument(srcPort instanceof OduCltPort);
178 checkArgument(dstPort instanceof OduCltPort);
179
180 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
181
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200182 // Release of intent resources here is only a temporary solution for handling the
183 // case of recompiling due to intent restoration (when intent state is FAILED).
184 // TODO: try to release intent resources in IntentManager.
185 resourceService.release(intent.id());
186
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700187 // Reserve OduClt ports
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800188 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
189 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800190 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700191 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700192 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
193 }
194
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200195 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800196 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
197 && isMultiplexingSupported(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 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700273 * Returns existing and available optical connectivity intent that matches the given circuit intent.
274 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800275 * @param src source connect point of optical circuit intent
276 * @param dst destination connect point of optical circuit intent
277 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200278 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700279 * @return existing optical connectivity intent, null otherwise.
280 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800281 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
282 ConnectPoint dst,
283 CltSignalType signalType,
284 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200285
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800286 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200287
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800288 return Tools.stream(intentService.getIntents())
289 .filter(x -> x instanceof OpticalConnectivityIntent)
290 .map(x -> (OpticalConnectivityIntent) x)
291 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
292 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
293 .filter(x -> isAllowed(src, x.getSrc()))
294 .filter(x -> isAllowed(dst, x.getDst()))
295 .filter(x -> isAvailable(x.id()))
296 .filter(x -> !multiplexingSupported ||
297 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
298 .findFirst()
299 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700300 }
301
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800302 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
303 ConnectPoint staticPort = staticPort(circuitCp);
304 return staticPort == null || staticPort.equals(connectivityCp);
305 }
306
307 /**
308 * Checks if current allocations on given resource can satisfy request.
309 * If the resource is null, return true.
310 *
311 * @param resource the resource on which to map the intent
312 * @return true if the resource can accept the request, false otherwise
313 */
314 private boolean isAvailable(IntentId resource) {
315 if (resource == null) {
316 return true;
317 }
318
319 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
320
321 if (mapping == null) {
322 return true;
323 }
324
325 return mapping.size() < maxCapacity;
326 }
327
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800328 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
329 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200330 if (common.isEmpty()) {
331 log.debug("No available TributarySlots");
332 return false;
333 }
334 if (common.size() < requestedTsNum) {
335 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
336 return false;
337 }
338 return true;
339 }
340
341 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
342 Pair<ConnectPoint, ConnectPoint> ports) {
343
344 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
345 int requestedTsNum = oduSignalType.tributarySlots();
346 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
347 if (commonTributarySlots.isEmpty()) {
348 return Collections.emptySet();
349 }
350 if (commonTributarySlots.size() < requestedTsNum) {
351 return Collections.emptySet();
352 }
353
354 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
355 .limit(requestedTsNum)
356 .collect(Collectors.toSet());
357
358 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
359 List<Resource> tributarySlotResources = portsList.stream()
360 .flatMap(cp -> tributarySlots
361 .stream()
362 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
363 .collect(Collectors.toList());
364
365 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
366 if (allocations.isEmpty()) {
367 log.debug("Resource allocation for {} failed (resource request: {})",
368 intent, tributarySlotResources);
369 return Collections.emptySet();
370 }
371 return tributarySlots;
372 }
373
Marc De Leenheer723f5532015-06-03 20:16:17 -0700374 private ConnectPoint staticPort(ConnectPoint connectPoint) {
375 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
376
377 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
378
379 // FIXME: need a better way to match the port
380 if (staticPort != null) {
381 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
382 if (staticPort.equals(p.number().name())) {
383 return new ConnectPoint(p.element().id(), p.number());
384 }
385 }
386 }
387
388 return null;
389 }
390
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800391 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
392 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
393 switch (signalType) {
394 case CLT_1GBE:
395 case CLT_10GBE:
396 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
397 return findPorts(src, dst, OduSignalType.ODU2)
398 .orElse(findPorts(src, dst, OduSignalType.ODU4).orElse(null));
399 case CLT_100GBE:
400 return findPorts(src, dst, OduSignalType.ODU4).orElse(null);
401 case CLT_40GBE:
402 default:
403 return null;
404 }
405 }
406
407 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
408 OduSignalType ochPortSignalType) {
409 return findAvailableOchPort(src, ochPortSignalType)
410 .flatMap(srcOch ->
411 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
412 }
413
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800414 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700415 // First see if the port mappings are constrained
416 ConnectPoint ochCP = staticPort(oduPort);
417
418 if (ochCP != null) {
419 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700420 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800421 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800422 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700423 .map(ResourceAllocation::consumer)
424 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800425 .map(x -> (IntentId) x)
426 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700427
428 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800429 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700430 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800431 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700432 }
433
434 // No port constraints, so find any port that works
435 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700436
437 for (Port port : ports) {
438 if (!(port instanceof OchPort)) {
439 continue;
440 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200441 // This should be the first allocation on the OCH port
442 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
443 continue;
444 }
445 // OchPort is required to have the requested oduSignalType
446 if (((OchPort) port).signalType() != ochPortSignalType) {
447 continue;
448 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700449
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700450 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800451 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800452 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700453 .map(ResourceAllocation::consumer)
454 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800455 .map(x -> (IntentId) x)
456 .findAny();
457
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700458 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800459 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700460 }
461 }
462
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800463 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700464 }
465
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700466 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700467 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700468 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700469 * @param src source port
470 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200471 * @param priority
472 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700473 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700474 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200475 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700476 checkArgument(src.deviceId().equals(dst.deviceId()));
477
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700478 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
479 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
480
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700481 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200482 if (!slots.isEmpty()) {
483 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
484 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
485 OduSignalType oduCltPortOduSignalType;
486 OduSignalType ochPortOduSignalType;
487
488 if (srcPort instanceof OduCltPort) {
489 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
490 ochPortOduSignalType = ((OchPort) dstPort).signalType();
491
492 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
493 // use Instruction of OduSignalId only in case of ODU Multiplexing
494 if (oduCltPortOduSignalType != ochPortOduSignalType) {
495 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
496 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
497 }
498 } else { // srcPort is OchPort
499 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
500 ochPortOduSignalType = ((OchPort) srcPort).signalType();
501
502 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
503 // use Criteria of OduSignalId only in case of ODU Multiplexing
504 if (oduCltPortOduSignalType != ochPortOduSignalType) {
505 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
506 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
507 }
508 }
509 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700510 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700511
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700512 FlowRule flowRule = DefaultFlowRule.builder()
513 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700514 .withSelector(selectorBuilder.build())
515 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400516 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700517 .fromApp(appId)
518 .makePermanent()
519 .build();
520
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700521 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700522 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200523
Sho SHIMIZU6953cf12016-02-29 13:58:13 -0800524 OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200525 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
526 int tributarySlotLen = ochPortSignalType.tributarySlots();
527 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
528
529 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
530 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
531 }
532
533 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
534 return (int) tributarySlots.stream().findFirst().get().index();
535 }
536
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800537 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800538 Driver driver = driverService.getDriver(cp.deviceId());
539 return driver != null
540 && driver.hasBehaviour(TributarySlotQuery.class)
541 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200542 }
543
544 /**
545 * Maps from Intent's OduClt SignalType to OduSignalType.
546 *
547 * @param cltSignalType OduClt port signal type
548 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
549 */
550 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200551 switch (cltSignalType) {
552 case CLT_1GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800553 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200554 case CLT_10GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800555 return OduSignalType.ODU2;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200556 case CLT_40GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800557 return OduSignalType.ODU3;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200558 case CLT_100GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800559 return OduSignalType.ODU4;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200560 default:
561 log.error("Unsupported CltSignalType {}", cltSignalType);
Sho SHIMIZU88799912016-02-29 13:34:12 -0800562 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200563 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200564 }
565
566 /**
567 * Finds the common TributarySlots available on the two connect points.
568 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800569 * @param src source connect point
570 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200571 * @return set of common TributarySlots on both connect points
572 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800573 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
574 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
575 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200576 return Sets.intersection(forward, backward);
577 }
578
579 /**
580 * Finds the TributarySlots available on the connect point.
581 *
582 * @param cp connect point
583 * @return set of TributarySlots available on the connect point
584 */
585 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800586 return resourceService.getAvailableResourceValues(
587 Resources.discrete(cp.deviceId(), cp.port()).id(),
588 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200589 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700590}