blob: 5e02f3cab97efc55991ea070c782cdf2d9ba68ef [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
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800396 private Optional<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))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800411 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700412 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800413 return Optional.empty();
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))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800441 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700442 }
443 }
444
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800445 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700446 }
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 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800450 switch (signalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200451 case CLT_1GBE:
452 case CLT_10GBE:
453 // First search for OCH ports with OduSignalType of ODU2. If not found - search for those with ODU4
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800454 return findPorts(src, dst, OduSignalType.ODU2)
455 .orElse(findPorts(src, dst, OduSignalType.ODU4).orElse(null));
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200456 case CLT_100GBE:
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800457 return findPorts(src, dst, OduSignalType.ODU4).orElse(null);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200458 case CLT_40GBE:
459 default:
Sho SHIMIZUa5671242016-02-29 16:09:46 -0800460 return null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200461 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200462 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700463
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800464 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
465 OduSignalType ochPortSignalType) {
466 return findAvailableOchPort(src, ochPortSignalType)
467 .flatMap(srcOch ->
468 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700469 }
470
471 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700472 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700473 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700474 * @param src source port
475 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200476 * @param priority
477 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700478 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700479 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200480 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700481 checkArgument(src.deviceId().equals(dst.deviceId()));
482
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700483 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
484 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
485
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700486 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200487 if (!slots.isEmpty()) {
488 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
489 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
490 OduSignalType oduCltPortOduSignalType;
491 OduSignalType ochPortOduSignalType;
492
493 if (srcPort instanceof OduCltPort) {
494 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
495 ochPortOduSignalType = ((OchPort) dstPort).signalType();
496
497 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
498 // use Instruction of OduSignalId only in case of ODU Multiplexing
499 if (oduCltPortOduSignalType != ochPortOduSignalType) {
500 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
501 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
502 }
503 } else { // srcPort is OchPort
504 oduCltPortOduSignalType = mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
505 ochPortOduSignalType = ((OchPort) srcPort).signalType();
506
507 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
508 // use Criteria of OduSignalId only in case of ODU Multiplexing
509 if (oduCltPortOduSignalType != ochPortOduSignalType) {
510 OduSignalId oduSignalId = buildOduSignalId(ochPortOduSignalType, slots);
511 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
512 }
513 }
514 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700515 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700516
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700517 FlowRule flowRule = DefaultFlowRule.builder()
518 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700519 .withSelector(selectorBuilder.build())
520 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400521 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700522 .fromApp(appId)
523 .makePermanent()
524 .build();
525
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700526 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700527 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200528
Sho SHIMIZU6953cf12016-02-29 13:58:13 -0800529 OduSignalId buildOduSignalId(OduSignalType ochPortSignalType, Set<TributarySlot> slots) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200530 int tributaryPortNumber = findFirstTributarySlotIndex(slots);
531 int tributarySlotLen = ochPortSignalType.tributarySlots();
532 byte[] tributarySlotBitmap = new byte[OduSignalId.TRIBUTARY_SLOT_BITMAP_SIZE];
533
534 slots.forEach(ts -> tributarySlotBitmap[(byte) (ts.index() - 1) / 8] |= 0x1 << ((ts.index() - 1) % 8));
535 return OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
536 }
537
538 private int findFirstTributarySlotIndex(Set<TributarySlot> tributarySlots) {
539 return (int) tributarySlots.stream().findFirst().get().index();
540 }
541
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800542 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800543 Driver driver = driverService.getDriver(cp.deviceId());
544 return driver != null
545 && driver.hasBehaviour(TributarySlotQuery.class)
546 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200547 }
548
549 /**
550 * Maps from Intent's OduClt SignalType to OduSignalType.
551 *
552 * @param cltSignalType OduClt port signal type
553 * @return OduSignalType the result of mapping CltSignalType to OduSignalType
554 */
555 OduSignalType mappingCltSignalTypeToOduSignalType(CltSignalType cltSignalType) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200556 switch (cltSignalType) {
557 case CLT_1GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800558 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200559 case CLT_10GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800560 return OduSignalType.ODU2;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200561 case CLT_40GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800562 return OduSignalType.ODU3;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200563 case CLT_100GBE:
Sho SHIMIZU88799912016-02-29 13:34:12 -0800564 return OduSignalType.ODU4;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200565 default:
566 log.error("Unsupported CltSignalType {}", cltSignalType);
Sho SHIMIZU88799912016-02-29 13:34:12 -0800567 return OduSignalType.ODU0;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200568 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200569 }
570
571 /**
572 * Finds the common TributarySlots available on the two connect points.
573 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800574 * @param src source connect point
575 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200576 * @return set of common TributarySlots on both connect points
577 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800578 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
579 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
580 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200581 return Sets.intersection(forward, backward);
582 }
583
584 /**
585 * Finds the TributarySlots available on the connect point.
586 *
587 * @param cp connect point
588 * @return set of TributarySlots available on the connect point
589 */
590 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800591 return resourceService.getAvailableResourceValues(
592 Resources.discrete(cp.deviceId(), cp.port()).id(),
593 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200594 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700595}