blob: 29ba5fb7c0343581309fcaf1e10c3021b3703a91 [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 /**
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
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800319 return Tools.stream(intentService.getIntents())
320 .filter(x -> x instanceof OpticalConnectivityIntent)
321 .map(x -> (OpticalConnectivityIntent) x)
322 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
323 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
324 .filter(x -> isAllowed(src, x.getSrc()))
325 .filter(x -> isAllowed(dst, x.getDst()))
326 .filter(x -> isAvailable(x.id()))
327 .filter(x -> !multiplexingSupported ||
328 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
329 .findFirst()
330 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700331 }
332
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800333 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
334 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200335 if (common.isEmpty()) {
336 log.debug("No available TributarySlots");
337 return false;
338 }
339 if (common.size() < requestedTsNum) {
340 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
341 return false;
342 }
343 return true;
344 }
345
346 private Set<TributarySlot> assignTributarySlots(OpticalCircuitIntent intent,
347 Pair<ConnectPoint, ConnectPoint> ports) {
348
349 OduSignalType oduSignalType = mappingCltSignalTypeToOduSignalType(intent.getSignalType());
350 int requestedTsNum = oduSignalType.tributarySlots();
351 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(ports.getLeft(), ports.getRight());
352 if (commonTributarySlots.isEmpty()) {
353 return Collections.emptySet();
354 }
355 if (commonTributarySlots.size() < requestedTsNum) {
356 return Collections.emptySet();
357 }
358
359 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
360 .limit(requestedTsNum)
361 .collect(Collectors.toSet());
362
363 final List<ConnectPoint> portsList = ImmutableList.of(ports.getLeft(), ports.getRight());
364 List<Resource> tributarySlotResources = portsList.stream()
365 .flatMap(cp -> tributarySlots
366 .stream()
367 .map(ts-> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
368 .collect(Collectors.toList());
369
370 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), tributarySlotResources);
371 if (allocations.isEmpty()) {
372 log.debug("Resource allocation for {} failed (resource request: {})",
373 intent, tributarySlotResources);
374 return Collections.emptySet();
375 }
376 return tributarySlots;
377 }
378
Marc De Leenheer723f5532015-06-03 20:16:17 -0700379 private ConnectPoint staticPort(ConnectPoint connectPoint) {
380 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
381
382 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
383
384 // FIXME: need a better way to match the port
385 if (staticPort != null) {
386 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
387 if (staticPort.equals(p.number().name())) {
388 return new ConnectPoint(p.element().id(), p.number());
389 }
390 }
391 }
392
393 return null;
394 }
395
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200396 private OchPort findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700397 // First see if the port mappings are constrained
398 ConnectPoint ochCP = staticPort(oduPort);
399
400 if (ochCP != null) {
401 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700402 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800403 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800404 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700405 .map(ResourceAllocation::consumer)
406 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800407 .map(x -> (IntentId) x)
408 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700409
410 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700411 return ochPort;
412 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200413 return null;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700414 }
415
416 // No port constraints, so find any port that works
417 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700418
419 for (Port port : ports) {
420 if (!(port instanceof OchPort)) {
421 continue;
422 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200423 // This should be the first allocation on the OCH port
424 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
425 continue;
426 }
427 // OchPort is required to have the requested oduSignalType
428 if (((OchPort) port).signalType() != ochPortSignalType) {
429 continue;
430 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700431
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700432 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800433 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800434 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700435 .map(ResourceAllocation::consumer)
436 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800437 .map(x -> (IntentId) x)
438 .findAny();
439
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700440 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700441 return (OchPort) port;
442 }
443 }
444
445 return null;
446 }
447
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800448 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200449 Pair<OchPort, OchPort> ochPorts = null;
450 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800451 switch (signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200452 case CLT_1GBE:
453 case CLT_10GBE:
454 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800455 ochPorts = findPorts(src, dst, OduSignalType.ODU2);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200456 if (ochPorts == null) {
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800457 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200458 }
459 break;
460 case CLT_100GBE:
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800461 ochPorts = findPorts(src, dst, OduSignalType.ODU4);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200462 break;
463 case CLT_40GBE:
464 default:
465 break;
466 }
467 return ochPorts;
468 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700469
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800470 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, OduSignalType ochPortSignalType) {
471 OchPort srcPort = findAvailableOchPort(src, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700472 if (srcPort == null) {
473 return null;
474 }
475
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800476 OchPort dstPort = findAvailableOchPort(dst, ochPortSignalType);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700477 if (dstPort == null) {
478 return null;
479 }
480
481 return Pair.of(srcPort, dstPort);
482 }
483
484 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700485 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700486 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700487 * @param src source port
488 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200489 * @param priority
490 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700491 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700492 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200493 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700494 checkArgument(src.deviceId().equals(dst.deviceId()));
495
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700496 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
497 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
498
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700499 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200500 if (!slots.isEmpty()) {
501 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
502 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
503 OduSignalType oduCltPortOduSignalType;
504 OduSignalType ochPortOduSignalType;
505
506 if (srcPort instanceof OduCltPort) {
507 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
508 ochPortOduSignalType = ((OchPort) dstPort).signalType();
509
510 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
511 // use Instruction of OduSignalId only in case of ODU Multiplexing
512 if (oduCltPortOduSignalType != ochPortOduSignalType) {
513 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
514 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
515 }
516 } else { // srcPort is OchPort
517 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
518 ochPortOduSignalType = ((OchPort) srcPort).signalType();
519
520 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
521 // use Criteria of OduSignalId only in case of ODU Multiplexing
522 if (oduCltPortOduSignalType != ochPortOduSignalType) {
523 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
524 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
525 }
526 }
527 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700528 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700529
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700530 FlowRule flowRule = DefaultFlowRule.builder()
531 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700532 .withSelector(selectorBuilder.build())
533 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400534 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700535 .fromApp(appId)
536 .makePermanent()
537 .build();
538
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700539 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700540 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200541
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200542 protected OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200543 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
544 int tributarySlotLen = ochPortSignalType.tributarySlots();
545 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
546
547 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
548 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
549 }
550
551 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
552 return (int) tributarySlots.stream().findFirst().get().index();
553 }
554
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800555 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800556 Driver driver = driverService.getDriver(cp.deviceId());
557 return driver != null
558 && driver.hasBehaviour(TributarySlotQuery.class)
559 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200560 }
561
562 /**
563 * Maps from Intent's OduClt SignalType to OduSignalType.
564 *
565 * @param cltSignalType OduClt port signal type
566 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
567 */
568 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200569 switch (cltSignalType) {
570 case CLT_1GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800571 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200572 case CLT_10GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800573 return OduSignalType.ODU2;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200574 case CLT_40GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800575 return OduSignalType.ODU3;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200576 case CLT_100GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800577 return OduSignalType.ODU4;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200578 default:
579 log.error("Unsupported CltSignalType {}", cltSignalType);
Sho SHIMIZU88799912016-02-29 13:34:12 -0800580 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200581 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200582 }
583
584 /**
585 * Finds the common TributarySlots available on the two connect points.
586 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800587 * @param src source connect point
588 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200589 * @return set of common TributarySlots on both connect points
590 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800591 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
592 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
593 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200594 return Sets.intersection(forward, backward);
595 }
596
597 /**
598 * Finds the TributarySlots available on the connect point.
599 *
600 * @param cp connect point
601 * @return set of TributarySlots available on the connect point
602 */
603 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800604 return resourceService.getAvailableResourceValues(
605 Resources.discrete(cp.deviceId(), cp.port()).id(),
606 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200607 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700608}