blob: 4b7b1dbae91af89913c8026ea688ee1625a944ac [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.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 SHIMIZUc25a0082015-10-27 17:06:29 -070020import com.google.common.collect.Sets;
weibitf32383b2014-10-22 10:17:31 -070021import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070022import org.apache.felix.scr.annotations.Component;
weibitf32383b2014-10-22 10:17:31 -070023import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070026import org.onlab.util.Frequency;
Marc De Leenheer723f5532015-06-03 20:16:17 -070027import org.onosproject.net.AnnotationKeys;
Marc De Leenheer2c305302015-12-07 21:37:44 -080028import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
Marc De Leenheer2c305302015-12-07 21:37:44 -080030import org.onosproject.net.DefaultOchSignalComparator;
Marc De Leenheer723f5532015-06-03 20:16:17 -070031import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070033import org.onosproject.net.OchPort;
34import 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;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070044import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -070045import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080046import org.onosproject.net.newresource.Resource;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070047import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080048import org.onosproject.net.newresource.Resources;
Brian O'Connor6de2e202015-05-21 14:30:41 -070049import org.onosproject.net.resource.link.LinkResourceAllocations;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.topology.LinkWeight;
51import org.onosproject.net.topology.Topology;
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;
59import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070060import java.util.stream.Collectors;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080061import java.util.stream.Stream;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070062
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070063import static com.google.common.base.Preconditions.checkArgument;
64
weibitf32383b2014-10-22 10:17:31 -070065/**
Brian O'Connorabafb502014-12-02 22:26:20 -080066 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070067 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070068@Component(immediate = true)
weibitf32383b2014-10-22 10:17:31 -070069public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
70
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070071 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
Marc De Leenheer2c305302015-12-07 21:37:44 -080072 // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent.
73 private static final int SLOT_COUNT = 4;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074
weibitf32383b2014-10-22 10:17:31 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected IntentExtensionService intentManager;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070079 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070080
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected DeviceService deviceService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070085 protected ResourceService resourceService;
86
weibitf32383b2014-10-22 10:17:31 -070087 @Activate
88 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070089 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
90 }
91
92 @Deactivate
93 public void deactivate() {
94 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
95 }
96
97 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070098 public List<Intent> compile(OpticalConnectivityIntent intent,
99 List<Intent> installable,
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800100 Set<LinkResourceAllocations> linkResources) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700101 // Check if source and destination are optical OCh ports
102 ConnectPoint src = intent.getSrc();
103 ConnectPoint dst = intent.getDst();
104 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
105 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
106 checkArgument(srcPort instanceof OchPort);
107 checkArgument(dstPort instanceof OchPort);
108
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700109 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
110
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200111 // Release of intent resources here is only a temporary solution for handling the
112 // case of recompiling due to intent restoration (when intent state is FAILED).
113 // TODO: try to release intent resources in IntentManager.
114 resourceService.release(intent.id());
115
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800116 // Check OCh port availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800117 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
118 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800119 // If ports are not available, compilation fails
120 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
121 throw new IntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700122 }
123
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800124 List<Resource> resources = new ArrayList<>();
125 resources.add(srcPortResource);
126 resources.add(dstPortResource);
127
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700128 // Calculate available light paths
129 Set<Path> paths = getOpticalPaths(intent);
130
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800131 if (paths.isEmpty()) {
132 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
133 }
134
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800135 // Static or dynamic lambda allocation
136 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
137 OchPort srcOchPort = (OchPort) srcPort;
138 OchPort dstOchPort = (OchPort) dstPort;
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800139
140 Path firstPath = paths.iterator().next();
141 // FIXME: need to actually reserve the lambda for static lambda's
142 // static lambda case: early return
143 if (staticLambda != null) {
144 allocateResources(intent, resources);
145
146 OchSignal lambda = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
147 srcOchPort.lambda().channelSpacing(),
148 srcOchPort.lambda().slotGranularity());
149 return ImmutableList.of(createIntent(intent, firstPath, lambda));
150 }
151
152 // FIXME: also check destination OCh port
153 // non-tunable case: early return
154 if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
155 allocateResources(intent, resources);
156
157 OchSignal lambda = srcOchPort.lambda();
158 return ImmutableList.of(createIntent(intent, firstPath, lambda));
159 }
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800160
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700161 // Use first path that can be successfully reserved
162 for (Path path : paths) {
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800163 // find common lambda on path
164 // a list of OchSignal indicates consecutive OchSignals
165 List<OchSignal> lambda = findFirstAvailableOch(path);
166 if (lambda.isEmpty()) {
167 continue;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700168 }
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800169 List<Resource> lambdaResources = convertToResources(path.links(), lambda);
170 if (!lambdaResources.stream().allMatch(resourceService::isAvailable)) {
171 continue;
172 }
173 resources.addAll(lambdaResources);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700174
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800175 allocateResources(intent, resources);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700176
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800177 OchSignal ochSignal = OchSignal.toFixedGrid(lambda, ChannelSpacing.CHL_50GHZ);
178 return ImmutableList.of(createIntent(intent, path, ochSignal));
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700179 }
180
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700181 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700182 }
183
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800184 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
185 // Create installable optical path intent
186 // Only support fixed grid for now
187 OchSignalType signalType = OchSignalType.FIXED_GRID;
188
189 return OpticalPathIntent.builder()
190 .appId(parentIntent.appId())
191 .src(parentIntent.getSrc())
192 .dst(parentIntent.getDst())
193 // calling paths.iterator().next() is safe because of non-empty set
194 .path(path)
195 .lambda(lambda)
196 .signalType(signalType)
197 .bidirectional(parentIntent.isBidirectional())
198 .build();
199 }
200
201 private void allocateResources(Intent intent, List<Resource> resources) {
202 // reserve all of required resources
203 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), resources);
204 if (allocations.isEmpty()) {
205 log.info("Resource allocation for {} failed (resource request: {})", intent, resources);
206 throw new IntentCompilationException("Unable to allocate resources: " + resources);
207 }
208 }
209
210 private List<OchSignal> findFirstAvailableOch(Path path) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800211 Set<OchSignal> lambdas = findCommonLambdasOverLinks(path.links());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700212 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800213 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700214 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700215
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800216 return findFirstLambda(lambdas, slotCount());
217 }
218
219 private List<Resource> convertToResources(List<Link> links, List<OchSignal> lambda) {
220 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800221 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800222 Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
223 Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800224 ))
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800225 .flatMap(x -> lambda.stream().map(x::child))
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700226 .collect(Collectors.toList());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700227 }
228
Marc De Leenheer2c305302015-12-07 21:37:44 -0800229 /**
230 * Get the number of 12.5 GHz slots required for the path.
231 *
232 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
233 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
234 *
235 * @return number of slots
236 */
237 private int slotCount() {
238 return SLOT_COUNT;
239 }
240
241 private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700242 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800243 .flatMap(x -> Stream.of(
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800244 Resources.discrete(x.src().deviceId(), x.src().port()).id(),
245 Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800246 ))
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800247 .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800248 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700249 .reduce(Sets::intersection)
250 .orElse(Collections.emptySet());
251 }
252
Marc De Leenheer2c305302015-12-07 21:37:44 -0800253 /**
254 * Returns list of consecutive resources in given set of lambdas.
255 *
256 * @param lambdas list of lambdas
257 * @param count number of consecutive lambdas to return
258 * @return list of consecutive lambdas
259 */
260 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
261 // Sort available lambdas
262 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
263 lambdaList.sort(new DefaultOchSignalComparator());
264
265 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
266 for (int i = 0; i < lambdaList.size() - count; i++) {
267 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
268 lambdaList.get(i + count).spacingMultiplier()) {
269 return lambdaList.subList(i, i + count);
270 }
271 }
272
273 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700274 }
275
Marc De Leenheer723f5532015-06-03 20:16:17 -0700276 private ConnectPoint staticPort(ConnectPoint connectPoint) {
277 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
278
279 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
280
281 // FIXME: need a better way to match the port
282 if (staticPort != null) {
283 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
284 if (staticPort.equals(p.number().name())) {
285 return new ConnectPoint(p.element().id(), p.number());
286 }
287 }
288 }
289
290 return null;
291 }
292
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700293 /**
294 * Calculates optical paths in WDM topology.
295 *
296 * @param intent optical connectivity intent
297 * @return set of paths in WDM topology
298 */
299 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
300 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700301 Topology topology = topologyService.currentTopology();
Sho SHIMIZU74626412015-09-11 11:46:27 -0700302 LinkWeight weight = edge -> {
303 // Disregard inactive or non-optical links
304 if (edge.link().state() == Link.State.INACTIVE) {
305 return -1;
weibit7e583462014-10-23 10:14:05 -0700306 }
Sho SHIMIZU74626412015-09-11 11:46:27 -0700307 if (edge.link().type() != Link.Type.OPTICAL) {
308 return -1;
309 }
310 // Adhere to static port mappings
311 DeviceId srcDeviceId = edge.link().src().deviceId();
312 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
313 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
314 if (srcStaticPort != null) {
315 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
316 }
317 }
318 DeviceId dstDeviceId = edge.link().dst().deviceId();
319 if (dstDeviceId.equals(intent.getDst().deviceId())) {
320 ConnectPoint dstStaticPort = staticPort(intent.getDst());
321 if (dstStaticPort != null) {
322 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
323 }
324 }
325
326 return 1;
weibit7e583462014-10-23 10:14:05 -0700327 };
weibitf32383b2014-10-22 10:17:31 -0700328
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700329 ConnectPoint start = intent.getSrc();
330 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700331 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700332 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700333
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700334 return paths;
weibitf32383b2014-10-22 10:17:31 -0700335 }
weibitf32383b2014-10-22 10:17:31 -0700336}