blob: 7e873d1f1e57cbe1f35919c4dbaa525d070b4f59 [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07003 *
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 */
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070016package org.onosproject.net.optical.intent.impl.compiler;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070017
Sho SHIMIZU12d02682016-02-24 13:55:12 -080018import com.google.common.base.Strings;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Sets;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070021import org.apache.commons.lang3.tuple.Pair;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070022import org.onlab.util.Tools;
23import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070026import org.onosproject.net.AnnotationKeys;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020027import org.onosproject.net.CltSignalType;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070028import org.onosproject.net.ConnectPoint;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020029import org.onosproject.net.OduSignalId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070030import org.onosproject.net.OduSignalType;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020031import org.onosproject.net.OduSignalUtils;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070032import org.onosproject.net.Port;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020033import org.onosproject.net.TributarySlot;
34import org.onosproject.net.behaviour.TributarySlotQuery;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070035import org.onosproject.net.device.DeviceService;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020036import org.onosproject.net.driver.Driver;
37import org.onosproject.net.driver.DriverService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070038import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020044import org.onosproject.net.flow.criteria.Criteria;
45import org.onosproject.net.flow.instructions.Instructions;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070046import org.onosproject.net.intent.FlowRuleIntent;
47import org.onosproject.net.intent.Intent;
48import org.onosproject.net.intent.IntentCompiler;
49import org.onosproject.net.intent.IntentExtensionService;
50import org.onosproject.net.intent.IntentId;
51import org.onosproject.net.intent.IntentService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052import org.onosproject.net.intent.IntentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070053import org.onosproject.net.intent.OpticalCircuitIntent;
54import org.onosproject.net.intent.OpticalConnectivityIntent;
Luca Prete670ac5d2017-02-03 15:55:43 -080055import org.onosproject.net.intent.PathIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080056import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070057import org.onosproject.net.optical.OduCltPort;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080058import org.onosproject.net.resource.Resource;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059import org.onosproject.net.resource.ResourceAllocation;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080060import org.onosproject.net.resource.ResourceService;
61import org.onosproject.net.resource.Resources;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070062import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063import org.osgi.service.component.annotations.Activate;
64import org.osgi.service.component.annotations.Component;
65import org.osgi.service.component.annotations.Deactivate;
66import org.osgi.service.component.annotations.Modified;
67import org.osgi.service.component.annotations.Reference;
68import org.osgi.service.component.annotations.ReferenceCardinality;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070069import org.slf4j.Logger;
70import org.slf4j.LoggerFactory;
71
Konstantinos Kanonakis198b0392017-02-22 13:18:09 -060072import java.util.Arrays;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070073import java.util.Collections;
Konstantinos Kanonakis198b0392017-02-22 13:18:09 -060074import java.util.Comparator;
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;
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +020081import java.util.stream.Stream;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070082
83import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080084import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Ray Milkey4694e062018-10-31 13:17:18 -070085import static org.onosproject.net.optical.intent.impl.compiler.OsgiPropertyConstants.MAX_CAPACITY;
86import static org.onosproject.net.optical.intent.impl.compiler.OsgiPropertyConstants.MAX_CAPACITY_DEFAULT;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070087
88/**
89 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
90 */
Ray Milkey4694e062018-10-31 13:17:18 -070091@Component(
92 immediate = true,
93 property = {
94 MAX_CAPACITY + ":Integer=" + MAX_CAPACITY_DEFAULT
95 }
96)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070097public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
98
99 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
100
Ray Milkey4694e062018-10-31 13:17:18 -0700101 /** Maximum utilization of an optical connection. */
102 private int maxCapacity = MAX_CAPACITY_DEFAULT;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700103
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700105 protected ComponentConfigService cfgService;
106
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700108 protected IntentExtensionService intentManager;
109
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700111 protected CoreService coreService;
112
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700114 protected DeviceService deviceService;
115
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700117 protected ResourceService resourceService;
118
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700120 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700121
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700123 protected IntentService intentService;
124
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200126 protected DriverService driverService;
127
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700128 private ApplicationId appId;
129
alessio0a0f3342019-10-28 16:58:01 +0100130 //Priority value of created OpticalConnectivity intent rules.
131 //The OpticalCircuitIntent is created with DEFAULT_INTENT_PRIORITY = 100 (see Intent.java)
132 //The two values have to be different to avoid rules overwriting.
133 private static final int OPTICAL_CONNECTIVITY_INTENT_PRIORITY = 200;
134
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700135 @Modified
136 public void modified(ComponentContext context) {
Rimon Ashkenazya4e3bd32016-02-22 16:12:20 +0200137 if (context == null) {
138 return;
139 }
140
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700141 Dictionary properties = context.getProperties();
142
143 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
Ray Milkey4694e062018-10-31 13:17:18 -0700144 String propertyString = Tools.get(properties, MAX_CAPACITY);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700145
Sho SHIMIZU12d02682016-02-24 13:55:12 -0800146 //Ignore if propertyString is empty or null
147 if (!Strings.isNullOrEmpty(propertyString)) {
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700148 try {
149 int temp = Integer.parseInt(propertyString);
150 //Ensure value is non-negative but allow zero as a way to shutdown the link
151 if (temp >= 0) {
152 maxCapacity = temp;
153 }
154 } catch (NumberFormatException e) {
155 //Malformed arguments lead to no change of value (user should be notified of error)
156 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
157 }
158 } else {
159 //Notify of empty value but do not return (other properties will also go in this function)
160 log.error("The value for maxCapacity was set to an empty value.");
161 }
162
163 }
164
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700165 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700166 public void activate(ComponentContext context) {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800167 deviceService = opticalView(deviceService);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700168 appId = coreService.registerApplication("org.onosproject.net.intent");
169 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700170 cfgService.registerProperties(getClass());
171 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700172 }
173
174 @Deactivate
175 public void deactivate() {
176 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700177 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700178 }
179
180 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800181 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700182 // Check if ports are OduClt ports
183 ConnectPoint src = intent.getSrc();
184 ConnectPoint dst = intent.getDst();
185 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
186 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
187 checkArgument(srcPort instanceof OduCltPort);
188 checkArgument(dstPort instanceof OduCltPort);
189
190 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
191
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200192 // Release of intent resources here is only a temporary solution for handling the
193 // case of recompiling due to intent restoration (when intent state is FAILED).
194 // TODO: try to release intent resources in IntentManager.
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700195 resourceService.release(intent.key());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200196
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200197 // Check OduClt ports availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800198 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
199 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200200 // If ports are not available, compilation fails
201 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700202 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700203 }
Rimon Ashkenazy0c59de02016-03-21 13:23:26 +0200204 List<Resource> ports = ImmutableList.of(srcPortResource, dstPortResource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700205
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200206 // Check if both devices support multiplexing (usage of TributarySlots)
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800207 boolean multiplexingSupported = isMultiplexingSupported(intent.getSrc())
208 && isMultiplexingSupported(intent.getDst());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200209
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800210 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent.getSrc(), intent.getDst(),
211 intent.getSignalType(), multiplexingSupported);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800212
213 if (connIntent != null && !multiplexingSupported) {
214 return compile(intent, src, dst, Optional.of(connIntent), ports, false);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200215 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700216
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200217 // Create optical connectivity intent if needed - no optical intent or not enough slots available
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800218 if (connIntent == null) {
219 return compile(intent, src, dst, Optional.empty(), ports, multiplexingSupported);
220 }
221
222 List<Resource> slots = availableSlotResources(connIntent.getSrc(), connIntent.getDst(),
223 intent.getSignalType());
224 if (slots.isEmpty()) {
225 return compile(intent, src, dst, Optional.empty(), ports, true);
226 }
227
228 return compile(intent, src, dst, Optional.of(connIntent), ImmutableList.<Resource>builder()
229 .addAll(ports).addAll(slots).build(), false);
230
231 }
232
233 private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst,
234 Optional<OpticalConnectivityIntent> existingConnectivity,
235 List<Resource> resources, boolean supportsMultiplexing) {
236 OpticalConnectivityIntent connectivityIntent;
237 List<Resource> required;
238 if (existingConnectivity.isPresent()) {
239 connectivityIntent = existingConnectivity.get();
240 required = resources;
241 } else {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700242 // Find OCh ports with available resources
Sho SHIMIZU979c3d92016-02-29 11:08:57 -0800243 Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700244
245 if (ochPorts == null) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700246 throw new OpticalIntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200247 }
248
249 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
250 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
251
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700252 // Create optical connectivity intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800253 connectivityIntent = OpticalConnectivityIntent.builder()
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700254 .appId(appId)
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700255 // TODO New top-level Intent created and submitted
256 // during compilation.
257 // We'll need to track inter-Intent dependency,
258 // but `key` field cannot be used for the purpose.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700259 .src(srcCP)
260 .dst(dstCP)
alessio0a0f3342019-10-28 16:58:01 +0100261 .priority(OPTICAL_CONNECTIVITY_INTENT_PRIORITY)
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800262 .signalType(ochPorts.getLeft().signalType())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700263 .bidirectional(intent.isBidirectional())
Luca Prete670ac5d2017-02-03 15:55:43 -0800264 .resourceGroup(intent.resourceGroup())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700265 .build();
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800266
267 if (!supportsMultiplexing) {
268 required = resources;
269 } else {
270 List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
271 if (slots.isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700272 throw new OpticalIntentCompilationException("Unable to find Tributary Slots for intent " + intent);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800273 }
274 required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
275 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700276 }
277
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700278 if (resourceService.allocate(intent.key(), required).isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700279 throw new OpticalIntentCompilationException("Unable to allocate resources for intent " + intent
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800280 + ": resources=" + required);
281 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700282
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800283 intentService.submit(connectivityIntent);
284
285 // Save circuit to connectivity intent mapping
286 intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
287
288 FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().
289 flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class)))
290 .collect(Collectors.toSet()));
Sho SHIMIZU953b0fb2016-02-26 16:42:13 -0800291 return ImmutableList.of(circuitIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700292 }
293
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800294 private List<Resource> availableSlotResources(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200295 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800296 int requestedTsNum = oduSignalType.tributarySlots();
297 Set<TributarySlot> commonTributarySlots = findCommonTributarySlotsOnCps(src, dst);
298 if (commonTributarySlots.isEmpty()) {
299 return Collections.emptyList();
300 }
301 if (commonTributarySlots.size() < requestedTsNum) {
302 return Collections.emptyList();
303 }
304
305 Set<TributarySlot> tributarySlots = commonTributarySlots.stream()
306 .limit(requestedTsNum)
307 .collect(Collectors.toSet());
308
309 final List<ConnectPoint> portsList = ImmutableList.of(src, dst);
310 List<Resource> tributarySlotResources = portsList.stream()
311 .flatMap(cp -> tributarySlots
312 .stream()
Ray Milkey88cc3432017-03-30 17:19:08 -0700313 .map(ts -> Resources.discrete(cp.deviceId(), cp.port()).resource().child(ts)))
Sho SHIMIZU8a4f8a12016-03-01 17:38:10 -0800314 .collect(Collectors.toList());
315
316 if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
317 log.debug("Resource allocation for {} on {} and {} failed (resource request: {})",
318 signalType, src, dst, tributarySlotResources);
319 return Collections.emptyList();
320 }
321 return tributarySlotResources;
322 }
323
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800324 private FlowRuleIntent createFlowRule(OpticalCircuitIntent higherIntent,
325 OpticalConnectivityIntent lowerIntent, Set<TributarySlot> slots) {
326 // Create optical circuit intent
327 List<FlowRule> rules = new LinkedList<>();
328 // at the source: ODUCLT port mapping to OCH port
alessio0a0f3342019-10-28 16:58:01 +0100329 log.debug("OpticalCircuitIntent creating FlowRules");
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800330 rules.add(connectPorts(higherIntent.getSrc(), lowerIntent.getSrc(), higherIntent.priority(), slots));
331 // at the destination: OCH port mapping to ODUCLT port
332 rules.add(connectPorts(lowerIntent.getDst(), higherIntent.getDst(), higherIntent.priority(), slots));
333
334 // Create flow rules for reverse path
335 if (higherIntent.isBidirectional()) {
alessio0a0f3342019-10-28 16:58:01 +0100336 log.debug("OpticalCircuitIntent creating FlowRules for reverse path");
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800337 // at the destination: OCH port mapping to ODUCLT port
338 rules.add(connectPorts(lowerIntent.getSrc(), higherIntent.getSrc(), higherIntent.priority(), slots));
339 // at the source: ODUCLT port mapping to OCH port
340 rules.add(connectPorts(higherIntent.getDst(), lowerIntent.getDst(), higherIntent.priority(), slots));
341 }
342
Luca Prete670ac5d2017-02-03 15:55:43 -0800343 return new FlowRuleIntent(appId, higherIntent.key(), rules,
344 higherIntent.resources(),
345 PathIntent.ProtectionType.PRIMARY,
346 higherIntent.resourceGroup());
Sho SHIMIZU874e0a32016-02-26 16:56:11 -0800347 }
348
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700349 /**
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700350 * Returns existing and available optical connectivity intent that matches the given circuit intent.
351 *
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800352 * @param src source connect point of optical circuit intent
353 * @param dst destination connect point of optical circuit intent
354 * @param signalType signal type of optical circuit intent
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200355 * @param multiplexingSupported indicates whether ODU multiplexing is supported
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700356 * @return existing optical connectivity intent, null otherwise.
357 */
Sho SHIMIZU6d10aed2016-02-29 11:44:42 -0800358 private OpticalConnectivityIntent findOpticalConnectivityIntent(ConnectPoint src,
359 ConnectPoint dst,
360 CltSignalType signalType,
361 boolean multiplexingSupported) {
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200362
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200363 OduSignalType oduSignalType = OduSignalUtils.mappingCltSignalTypeToOduSignalType(signalType);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200364
Sho SHIMIZU0505f1d2016-02-29 13:31:47 -0800365 return Tools.stream(intentService.getIntents())
366 .filter(x -> x instanceof OpticalConnectivityIntent)
367 .map(x -> (OpticalConnectivityIntent) x)
368 .filter(x -> src.deviceId().equals(x.getSrc().deviceId()))
369 .filter(x -> dst.deviceId().equals(x.getDst().deviceId()))
370 .filter(x -> isAllowed(src, x.getSrc()))
371 .filter(x -> isAllowed(dst, x.getDst()))
372 .filter(x -> isAvailable(x.id()))
373 .filter(x -> !multiplexingSupported ||
374 isAvailableTributarySlots(x.getSrc(), x.getDst(), oduSignalType.tributarySlots()))
375 .findFirst()
376 .orElse(null);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700377 }
378
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800379 private boolean isAllowed(ConnectPoint circuitCp, ConnectPoint connectivityCp) {
380 ConnectPoint staticPort = staticPort(circuitCp);
381 return staticPort == null || staticPort.equals(connectivityCp);
382 }
383
384 /**
385 * Checks if current allocations on given resource can satisfy request.
386 * If the resource is null, return true.
387 *
388 * @param resource the resource on which to map the intent
389 * @return true if the resource can accept the request, false otherwise
390 */
391 private boolean isAvailable(IntentId resource) {
392 if (resource == null) {
393 return true;
394 }
395
396 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
397
398 if (mapping == null) {
399 return true;
400 }
401
402 return mapping.size() < maxCapacity;
403 }
404
Sho SHIMIZU64339fe2016-02-29 11:21:29 -0800405 private boolean isAvailableTributarySlots(ConnectPoint src, ConnectPoint dst, int requestedTsNum) {
406 Set<TributarySlot> common = findCommonTributarySlotsOnCps(src, dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200407 if (common.isEmpty()) {
408 log.debug("No available TributarySlots");
409 return false;
410 }
411 if (common.size() < requestedTsNum) {
412 log.debug("Not enough available TributarySlots={} < requestedTsNum={}", common.size(), requestedTsNum);
413 return false;
414 }
415 return true;
416 }
417
Marc De Leenheer723f5532015-06-03 20:16:17 -0700418 private ConnectPoint staticPort(ConnectPoint connectPoint) {
419 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
420
421 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
422
423 // FIXME: need a better way to match the port
424 if (staticPort != null) {
425 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
426 if (staticPort.equals(p.number().name())) {
427 return new ConnectPoint(p.element().id(), p.number());
428 }
429 }
430 }
431
432 return null;
433 }
434
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800435 private Pair<OchPort, OchPort> findPorts(ConnectPoint src, ConnectPoint dst, CltSignalType signalType) {
436 // According to the OpticalCircuitIntent's signalType find OCH ports with available TributarySlots resources
Konstantinos Kanonakis198b0392017-02-22 13:18:09 -0600437 return Arrays.asList(OduSignalType.values()).stream()
438 .sorted(Comparator.comparingLong(OduSignalType::bitRate))
439 .filter(oduSignalType -> oduSignalType.bitRate() >= signalType.bitRate())
440 .map(oduSignalType -> findPorts(src, dst, oduSignalType))
441 .flatMap(Tools::stream)
442 .findFirst()
443 .orElse(null);
Sho SHIMIZU98de57a2016-02-29 17:20:44 -0800444 }
445
446 private Optional<Pair<OchPort, OchPort>> findPorts(ConnectPoint src, ConnectPoint dst,
447 OduSignalType ochPortSignalType) {
448 return findAvailableOchPort(src, ochPortSignalType)
449 .flatMap(srcOch ->
450 findAvailableOchPort(dst, ochPortSignalType).map(dstOch -> Pair.of(srcOch, dstOch)));
451 }
452
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800453 private Optional<OchPort> findAvailableOchPort(ConnectPoint oduPort, OduSignalType ochPortSignalType) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700454 // First see if the port mappings are constrained
455 ConnectPoint ochCP = staticPort(oduPort);
456
457 if (ochCP != null) {
458 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700459 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800460 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800461 .stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700462 .map(ResourceAllocation::consumerId)
463 .map(ResourceHelper::getIntentId)
464 .flatMap(Tools::stream)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800465 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700466
467 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800468 return Optional.of(ochPort);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700469 }
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800470 return Optional.empty();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700471 }
472
473 // No port constraints, so find any port that works
474 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700475
476 for (Port port : ports) {
477 if (!(port instanceof OchPort)) {
478 continue;
479 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200480 // This should be the first allocation on the OCH port
481 if (!resourceService.isAvailable(Resources.discrete(oduPort.deviceId(), port.number()).resource())) {
482 continue;
483 }
484 // OchPort is required to have the requested oduSignalType
485 if (((OchPort) port).signalType() != ochPortSignalType) {
486 continue;
487 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700488
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700489 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800490 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800491 .stream()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700492 .map(ResourceAllocation::consumerId)
493 .map(ResourceHelper::getIntentId)
494 .flatMap(Tools::stream)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800495 .findAny();
496
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700497 if (isAvailable(intentId.orElse(null))) {
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800498 return Optional.of((OchPort) port);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700499 }
500 }
501
Sho SHIMIZUaa00e502016-02-29 16:21:54 -0800502 return Optional.empty();
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700503 }
504
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700505 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700506 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700507 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700508 * @param src source port
509 * @param dst destination port
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200510 * @param priority
511 * @param slots Set of TributarySlots
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700512 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700513 */
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200514 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority, Set<TributarySlot> slots) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700515 checkArgument(src.deviceId().equals(dst.deviceId()));
516
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700517 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
518 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
519
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700520 selectorBuilder.matchInPort(src.port());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200521 if (!slots.isEmpty()) {
522 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
523 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
524 OduSignalType oduCltPortOduSignalType;
525 OduSignalType ochPortOduSignalType;
526
527 if (srcPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200528 oduCltPortOduSignalType =
529 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) srcPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200530 ochPortOduSignalType = ((OchPort) dstPort).signalType();
531
532 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
533 // use Instruction of OduSignalId only in case of ODU Multiplexing
534 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200535 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200536 treatmentBuilder.add(Instructions.modL1OduSignalId(oduSignalId));
537 }
538 } else { // srcPort is OchPort
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200539 oduCltPortOduSignalType =
540 OduSignalUtils.mappingCltSignalTypeToOduSignalType(((OduCltPort) dstPort).signalType());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200541 ochPortOduSignalType = ((OchPort) srcPort).signalType();
542
543 selectorBuilder.add(Criteria.matchOduSignalType(oduCltPortOduSignalType));
544 // use Criteria of OduSignalId only in case of ODU Multiplexing
545 if (oduCltPortOduSignalType != ochPortOduSignalType) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200546 OduSignalId oduSignalId = OduSignalUtils.buildOduSignalId(ochPortOduSignalType, slots);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200547 selectorBuilder.add(Criteria.matchOduSignalId(oduSignalId));
548 }
549 }
550 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700551 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700552
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700553 FlowRule flowRule = DefaultFlowRule.builder()
554 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700555 .withSelector(selectorBuilder.build())
556 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400557 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700558 .fromApp(appId)
559 .makePermanent()
560 .build();
561
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700562 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700563 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200564
Sho SHIMIZUcaf0b342016-02-29 13:49:04 -0800565 private boolean isMultiplexingSupported(ConnectPoint cp) {
Sho SHIMIZU0a8332c2016-02-29 13:54:08 -0800566 Driver driver = driverService.getDriver(cp.deviceId());
567 return driver != null
568 && driver.hasBehaviour(TributarySlotQuery.class)
569 && staticPort(cp) == null;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200570 }
571
572 /**
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200573 * Finds the common TributarySlots available on the two connect points.
574 *
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800575 * @param src source connect point
576 * @param dst dest connect point
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200577 * @return set of common TributarySlots on both connect points
578 */
Sho SHIMIZU3763a022016-02-29 11:10:17 -0800579 Set<TributarySlot> findCommonTributarySlotsOnCps(ConnectPoint src, ConnectPoint dst) {
580 Set<TributarySlot> forward = findTributarySlotsOnCp(src);
581 Set<TributarySlot> backward = findTributarySlotsOnCp(dst);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200582 return Sets.intersection(forward, backward);
583 }
584
585 /**
586 * Finds the TributarySlots available on the connect point.
587 *
588 * @param cp connect point
589 * @return set of TributarySlots available on the connect point
590 */
591 Set<TributarySlot> findTributarySlotsOnCp(ConnectPoint cp) {
Sho SHIMIZUc4ae4d52016-02-19 15:15:31 -0800592 return resourceService.getAvailableResourceValues(
593 Resources.discrete(cp.deviceId(), cp.port()).id(),
594 TributarySlot.class);
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200595 }
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700596}