blob: f48b844839f8c393685628b7abc209152844b45e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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;
weibitf32383b2014-10-22 10:17:31 -070017
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070018import com.google.common.collect.ImmutableList;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070019import com.google.common.collect.ImmutableSet;
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -080020import com.google.common.collect.Maps;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070021import com.google.common.collect.Sets;
weibitf32383b2014-10-22 10:17:31 -070022import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070023import org.apache.felix.scr.annotations.Component;
weibitf32383b2014-10-22 10:17:31 -070024import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070027import org.onlab.util.Frequency;
Marc De Leenheer723f5532015-06-03 20:16:17 -070028import org.onosproject.net.AnnotationKeys;
Marc De Leenheer2c305302015-12-07 21:37:44 -080029import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
Marc De Leenheer2c305302015-12-07 21:37:44 -080031import org.onosproject.net.DefaultOchSignalComparator;
Marc De Leenheer723f5532015-06-03 20:16:17 -070032import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070034import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070035import org.onosproject.net.OchSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070037import org.onosproject.net.Port;
38import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentCompiler;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.OpticalConnectivityIntent;
43import org.onosproject.net.intent.OpticalPathIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080044import org.onosproject.net.optical.OchPort;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080045import org.onosproject.net.resource.ResourceAllocation;
46import org.onosproject.net.resource.Resource;
47import org.onosproject.net.resource.ResourceService;
48import org.onosproject.net.resource.Resources;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.topology.LinkWeight;
50import org.onosproject.net.topology.Topology;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070051import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070055
Marc De Leenheer2c305302015-12-07 21:37:44 -080056import java.util.ArrayList;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070057import java.util.Collections;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070058import java.util.List;
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -080059import java.util.Map;
60import java.util.Optional;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070061import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070062import java.util.stream.Collectors;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080063import java.util.stream.Stream;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070064
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070065import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080066import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070067
weibitf32383b2014-10-22 10:17:31 -070068/**
Brian O'Connorabafb502014-12-02 22:26:20 -080069 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070070 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070071@Component(immediate = true)
weibitf32383b2014-10-22 10:17:31 -070072public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
73
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
Marc De Leenheer2c305302015-12-07 21:37:44 -080075 // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent.
76 private static final int SLOT_COUNT = 4;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070077
weibitf32383b2014-10-22 10:17:31 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected IntentExtensionService intentManager;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070082 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070083
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected DeviceService deviceService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070088 protected ResourceService resourceService;
89
weibitf32383b2014-10-22 10:17:31 -070090 @Activate
91 public void activate() {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080092 deviceService = opticalView(deviceService);
weibitf32383b2014-10-22 10:17:31 -070093 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
94 }
95
96 @Deactivate
97 public void deactivate() {
98 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
99 }
100
101 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700102 public List<Intent> compile(OpticalConnectivityIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800103 List<Intent> installable) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700104 // Check if source and destination are optical OCh ports
105 ConnectPoint src = intent.getSrc();
106 ConnectPoint dst = intent.getDst();
107 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
108 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
109 checkArgument(srcPort instanceof OchPort);
110 checkArgument(dstPort instanceof OchPort);
111
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700112 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
113
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200114 // Release of intent resources here is only a temporary solution for handling the
115 // case of recompiling due to intent restoration (when intent state is FAILED).
116 // TODO: try to release intent resources in IntentManager.
117 resourceService.release(intent.id());
118
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800119 // Check OCh port availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800120 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
121 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800122 // If ports are not available, compilation fails
123 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700124 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700125 }
126
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800127 List<Resource> resources = new ArrayList<>();
128 resources.add(srcPortResource);
129 resources.add(dstPortResource);
130
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700131 // Calculate available light paths
132 Set<Path> paths = getOpticalPaths(intent);
133
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800134 if (paths.isEmpty()) {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700135 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800136 }
137
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800138 // Static or dynamic lambda allocation
139 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
140 OchPort srcOchPort = (OchPort) srcPort;
141 OchPort dstOchPort = (OchPort) dstPort;
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800142
143 Path firstPath = paths.iterator().next();
144 // FIXME: need to actually reserve the lambda for static lambda's
145 // static lambda case: early return
146 if (staticLambda != null) {
147 allocateResources(intent, resources);
148
149 OchSignal lambda = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
150 srcOchPort.lambda().channelSpacing(),
151 srcOchPort.lambda().slotGranularity());
152 return ImmutableList.of(createIntent(intent, firstPath, lambda));
153 }
154
155 // FIXME: also check destination OCh port
156 // non-tunable case: early return
157 if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
158 allocateResources(intent, resources);
159
160 OchSignal lambda = srcOchPort.lambda();
161 return ImmutableList.of(createIntent(intent, firstPath, lambda));
162 }
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800163
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800164 // remaining cases
165 // Use first path that the required resources are available
166 Optional<Map.Entry<Path, List<OchSignal>>> found = paths.stream()
167 .map(path -> Maps.immutableEntry(path, findFirstAvailableOch(path)))
168 .filter(entry -> !entry.getValue().isEmpty())
169 .filter(entry -> convertToResources(entry.getKey().links(),
170 entry.getValue()).stream().allMatch(resourceService::isAvailable))
171 .findFirst();
172
173 if (found.isPresent()) {
174 resources.addAll(convertToResources(found.get().getKey().links(), found.get().getValue()));
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700175
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800176 allocateResources(intent, resources);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700177
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800178 OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ);
179 return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal));
180 } else {
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700181 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700182 }
weibitf32383b2014-10-22 10:17:31 -0700183 }
184
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800185 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
186 // Create installable optical path intent
187 // Only support fixed grid for now
188 OchSignalType signalType = OchSignalType.FIXED_GRID;
189
190 return OpticalPathIntent.builder()
191 .appId(parentIntent.appId())
192 .src(parentIntent.getSrc())
193 .dst(parentIntent.getDst())
194 // calling paths.iterator().next() is safe because of non-empty set
195 .path(path)
196 .lambda(lambda)
197 .signalType(signalType)
198 .bidirectional(parentIntent.isBidirectional())
199 .build();
200 }
201
202 private void allocateResources(Intent intent, List<Resource> resources) {
203 // reserve all of required resources
204 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), resources);
205 if (allocations.isEmpty()) {
206 log.info("Resource allocation for {} failed (resource request: {})", intent, resources);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700207 throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800208 }
209 }
210
211 private List<OchSignal> findFirstAvailableOch(Path path) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800212 Set<OchSignal> lambdas = findCommonLambdasOverLinks(path.links());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700213 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800214 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700215 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700216
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800217 return findFirstLambda(lambdas, slotCount());
218 }
219
220 private List<Resource> convertToResources(List<Link> links, List<OchSignal> lambda) {
221 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800222 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800223 Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
224 Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800225 ))
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800226 .flatMap(x -> lambda.stream().map(x::child))
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700227 .collect(Collectors.toList());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700228 }
229
Marc De Leenheer2c305302015-12-07 21:37:44 -0800230 /**
231 * Get the number of 12.5 GHz slots required for the path.
232 *
233 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
234 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
235 *
236 * @return number of slots
237 */
238 private int slotCount() {
239 return SLOT_COUNT;
240 }
241
242 private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700243 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800244 .flatMap(x -> Stream.of(
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800245 Resources.discrete(x.src().deviceId(), x.src().port()).id(),
246 Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800247 ))
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800248 .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800249 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700250 .reduce(Sets::intersection)
251 .orElse(Collections.emptySet());
252 }
253
Marc De Leenheer2c305302015-12-07 21:37:44 -0800254 /**
255 * Returns list of consecutive resources in given set of lambdas.
256 *
257 * @param lambdas list of lambdas
258 * @param count number of consecutive lambdas to return
259 * @return list of consecutive lambdas
260 */
261 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
262 // Sort available lambdas
263 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
264 lambdaList.sort(new DefaultOchSignalComparator());
265
266 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
267 for (int i = 0; i < lambdaList.size() - count; i++) {
268 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
269 lambdaList.get(i + count).spacingMultiplier()) {
270 return lambdaList.subList(i, i + count);
271 }
272 }
273
274 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700275 }
276
Marc De Leenheer723f5532015-06-03 20:16:17 -0700277 private ConnectPoint staticPort(ConnectPoint connectPoint) {
278 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
279
280 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
281
282 // FIXME: need a better way to match the port
283 if (staticPort != null) {
284 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
285 if (staticPort.equals(p.number().name())) {
286 return new ConnectPoint(p.element().id(), p.number());
287 }
288 }
289 }
290
291 return null;
292 }
293
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700294 /**
295 * Calculates optical paths in WDM topology.
296 *
297 * @param intent optical connectivity intent
298 * @return set of paths in WDM topology
299 */
300 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
301 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700302 Topology topology = topologyService.currentTopology();
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700303 LinkWeight weight = new LinkWeight() {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700304
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700305 @Override
306 public double weight(TopologyEdge edge) {
307 // Disregard inactive or non-optical links
308 if (edge.link().state() == Link.State.INACTIVE) {
309 return -1;
310 }
311 if (edge.link().type() != Link.Type.OPTICAL) {
312 return -1;
313 }
314 // Adhere to static port mappings
315 DeviceId srcDeviceId = edge.link().src().deviceId();
316 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
317 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
318 if (srcStaticPort != null) {
319 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
320 }
321 }
322 DeviceId dstDeviceId = edge.link().dst().deviceId();
323 if (dstDeviceId.equals(intent.getDst().deviceId())) {
324 ConnectPoint dstStaticPort = staticPort(intent.getDst());
325 if (dstStaticPort != null) {
326 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
327 }
328 }
329
330 return 1;
331 }
weibit7e583462014-10-23 10:14:05 -0700332 };
weibitf32383b2014-10-22 10:17:31 -0700333
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700334 ConnectPoint start = intent.getSrc();
335 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700336 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700337 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700338
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700339 return paths;
weibitf32383b2014-10-22 10:17:31 -0700340 }
weibitf32383b2014-10-22 10:17:31 -0700341}