blob: 636de7dd6fff2d2d1a5ed674ca98245c54c64051 [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,
100 Set<LinkResourceAllocations> resources) {
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
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700116 // Reserve OCh ports
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 SHIMIZU763be922016-01-28 09:22:10 -0800119 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700120 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700121 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
122 }
123
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700124 // Calculate available light paths
125 Set<Path> paths = getOpticalPaths(intent);
126
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800127 // Static or dynamic lambda allocation
128 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
129 OchPort srcOchPort = (OchPort) srcPort;
130 OchPort dstOchPort = (OchPort) dstPort;
131 OchSignal ochSignal;
132
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700133 // Use first path that can be successfully reserved
134 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700135
Marc De Leenheerc9733082015-06-04 12:22:38 -0700136 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700137 if (staticLambda != null) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -0700138 ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Marc De Leenheerc9733082015-06-04 12:22:38 -0700139 srcOchPort.lambda().channelSpacing(),
140 srcOchPort.lambda().slotGranularity());
141 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800142 // FIXME: also check destination OCh port
Marc De Leenheerc9733082015-06-04 12:22:38 -0700143 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700144 } else {
145 // Request and reserve lambda on path
Marc De Leenheer2c305302015-12-07 21:37:44 -0800146 List<OchSignal> lambdas = assignWavelength(intent, path);
147 if (lambdas.isEmpty()) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700148 continue;
149 }
Marc De Leenheer2c305302015-12-07 21:37:44 -0800150 ochSignal = OchSignal.toFixedGrid(lambdas, ChannelSpacing.CHL_50GHZ);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700151 }
152
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700153 // Create installable optical path intent
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700154 // Only support fixed grid for now
155 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700156
157 Intent newIntent = OpticalPathIntent.builder()
158 .appId(intent.appId())
159 .src(intent.getSrc())
160 .dst(intent.getDst())
161 .path(path)
162 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700163 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700164 .bidirectional(intent.isBidirectional())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700165 .build();
166
167 return ImmutableList.of(newIntent);
168 }
169
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700170 // Release port allocations if unsuccessful
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700171 resourceService.release(intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700172
173 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700174 }
175
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700176 /**
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700177 * Request and reserve first available wavelength across path.
178 *
179 * @param path path in WDM topology
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700180 * @return first available lambda allocated
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700181 */
Marc De Leenheer2c305302015-12-07 21:37:44 -0800182 private List<OchSignal> assignWavelength(Intent intent, Path path) {
183 Set<OchSignal> lambdas = findCommonLambdasOverLinks(path.links());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700184 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800185 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700186 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700187
Marc De Leenheer2c305302015-12-07 21:37:44 -0800188 List<OchSignal> minLambda = findFirstLambda(lambdas, slotCount());
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800189 List<Resource> lambdaResources = path.links().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800190 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800191 Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
192 Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800193 ))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800194 .flatMap(x -> minLambda.stream().map(l -> x.child(l)))
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700195 .collect(Collectors.toList());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700196
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700197 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), lambdaResources);
198 if (allocations.isEmpty()) {
199 log.info("Resource allocation for {} failed (resource request: {})", intent, lambdaResources);
Marc De Leenheer2c305302015-12-07 21:37:44 -0800200 return Collections.emptyList();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700201 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700202
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700203 return minLambda;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700204 }
205
Marc De Leenheer2c305302015-12-07 21:37:44 -0800206 /**
207 * Get the number of 12.5 GHz slots required for the path.
208 *
209 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
210 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
211 *
212 * @return number of slots
213 */
214 private int slotCount() {
215 return SLOT_COUNT;
216 }
217
218 private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700219 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800220 .flatMap(x -> Stream.of(
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800221 Resources.discrete(x.src().deviceId(), x.src().port()).id(),
222 Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800223 ))
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800224 .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800225 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700226 .reduce(Sets::intersection)
227 .orElse(Collections.emptySet());
228 }
229
Marc De Leenheer2c305302015-12-07 21:37:44 -0800230 /**
231 * Returns list of consecutive resources in given set of lambdas.
232 *
233 * @param lambdas list of lambdas
234 * @param count number of consecutive lambdas to return
235 * @return list of consecutive lambdas
236 */
237 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
238 // Sort available lambdas
239 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
240 lambdaList.sort(new DefaultOchSignalComparator());
241
242 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
243 for (int i = 0; i < lambdaList.size() - count; i++) {
244 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
245 lambdaList.get(i + count).spacingMultiplier()) {
246 return lambdaList.subList(i, i + count);
247 }
248 }
249
250 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700251 }
252
Marc De Leenheer723f5532015-06-03 20:16:17 -0700253 private ConnectPoint staticPort(ConnectPoint connectPoint) {
254 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
255
256 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
257
258 // FIXME: need a better way to match the port
259 if (staticPort != null) {
260 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
261 if (staticPort.equals(p.number().name())) {
262 return new ConnectPoint(p.element().id(), p.number());
263 }
264 }
265 }
266
267 return null;
268 }
269
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700270 /**
271 * Calculates optical paths in WDM topology.
272 *
273 * @param intent optical connectivity intent
274 * @return set of paths in WDM topology
275 */
276 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
277 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700278 Topology topology = topologyService.currentTopology();
Sho SHIMIZU74626412015-09-11 11:46:27 -0700279 LinkWeight weight = edge -> {
280 // Disregard inactive or non-optical links
281 if (edge.link().state() == Link.State.INACTIVE) {
282 return -1;
weibit7e583462014-10-23 10:14:05 -0700283 }
Sho SHIMIZU74626412015-09-11 11:46:27 -0700284 if (edge.link().type() != Link.Type.OPTICAL) {
285 return -1;
286 }
287 // Adhere to static port mappings
288 DeviceId srcDeviceId = edge.link().src().deviceId();
289 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
290 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
291 if (srcStaticPort != null) {
292 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
293 }
294 }
295 DeviceId dstDeviceId = edge.link().dst().deviceId();
296 if (dstDeviceId.equals(intent.getDst().deviceId())) {
297 ConnectPoint dstStaticPort = staticPort(intent.getDst());
298 if (dstStaticPort != null) {
299 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
300 }
301 }
302
303 return 1;
weibit7e583462014-10-23 10:14:05 -0700304 };
weibitf32383b2014-10-22 10:17:31 -0700305
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700306 ConnectPoint start = intent.getSrc();
307 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700308 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700309 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700310
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700311 return paths;
weibitf32383b2014-10-22 10:17:31 -0700312 }
weibitf32383b2014-10-22 10:17:31 -0700313}