blob: e4df2272188bea5e72c0e2d5f3a3449369e602e9 [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;
20import com.google.common.collect.Iterables;
21import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
Marc De Leenheer723f5532015-06-03 20:16:17 -070030import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070032import org.onosproject.net.OchPort;
33import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070034import org.onosproject.net.OchSignalType;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070035import org.onosproject.net.OmsPort;
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 SHIMIZU5c16df82015-09-29 12:52:07 -070045import org.onosproject.net.newresource.ResourcePath;
46import org.onosproject.net.newresource.ResourceService;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070047import org.onosproject.net.resource.ResourceType;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070048import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
Brian O'Connor6de2e202015-05-21 14:30:41 -070049import org.onosproject.net.resource.link.LambdaResource;
50import org.onosproject.net.resource.link.LambdaResourceAllocation;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070051import org.onosproject.net.resource.link.LambdaResourceRequest;
Brian O'Connor6de2e202015-05-21 14:30:41 -070052import org.onosproject.net.resource.link.LinkResourceAllocations;
53import org.onosproject.net.resource.link.LinkResourceRequest;
54import org.onosproject.net.resource.link.LinkResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.topology.LinkWeight;
56import org.onosproject.net.topology.Topology;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070058import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070060
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070061import java.util.Collections;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070062import java.util.List;
63import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070064import java.util.stream.Collectors;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070065
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070066import static com.google.common.base.Preconditions.checkArgument;
67
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 */
Ray Milkeye6099412015-10-05 10:53:17 -070071// For now, remove component designation until dependency on the new resource manager is available.
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070072@Component(immediate = true)
weibitf32383b2014-10-22 10:17:31 -070073public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
74
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070075 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
76
weibitf32383b2014-10-22 10:17:31 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected IntentExtensionService intentManager;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070081 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070082
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected DeviceService deviceService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070087 protected ResourceService resourceService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070090 protected LinkResourceService linkResourceService;
91
weibitf32383b2014-10-22 10:17:31 -070092 @Activate
93 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070094 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
95 }
96
97 @Deactivate
98 public void deactivate() {
99 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
100 }
101
102 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700103 public List<Intent> compile(OpticalConnectivityIntent intent,
104 List<Intent> installable,
105 Set<LinkResourceAllocations> resources) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700106 // Check if source and destination are optical OCh ports
107 ConnectPoint src = intent.getSrc();
108 ConnectPoint dst = intent.getDst();
109 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
110 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
111 checkArgument(srcPort instanceof OchPort);
112 checkArgument(dstPort instanceof OchPort);
113
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700114 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
115
116 // Reserve OCh ports
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700117 ResourcePath srcPortPath = new ResourcePath(src.deviceId(), src.port());
118 ResourcePath dstPortPath = new ResourcePath(dst.deviceId(), dst.port());
119 List<org.onosproject.net.newresource.ResourceAllocation> allocation =
120 resourceService.allocate(intent.id(), srcPortPath, dstPortPath);
121 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700122 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
123 }
124
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700125 // Calculate available light paths
126 Set<Path> paths = getOpticalPaths(intent);
127
128 // Use first path that can be successfully reserved
129 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700130
131 // Static or dynamic lambda allocation
Marc De Leenheer723f5532015-06-03 20:16:17 -0700132 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
Marc De Leenheerc9733082015-06-04 12:22:38 -0700133 OchPort srcOchPort = (OchPort) srcPort;
134 OchPort dstOchPort = (OchPort) dstPort;
135 OchSignal ochSignal;
136
137 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700138 if (staticLambda != null) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -0700139 ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Marc De Leenheerc9733082015-06-04 12:22:38 -0700140 srcOchPort.lambda().channelSpacing(),
141 srcOchPort.lambda().slotGranularity());
142 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
143 // FIXME: also check OCh port
144 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700145 } else {
146 // Request and reserve lambda on path
147 LinkResourceAllocations linkAllocs = assignWavelength(intent, path);
148 if (linkAllocs == null) {
149 continue;
150 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700151 LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
152 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700153 ochSignal = new OchSignal(lambdaAlloc.lambda().toInt(), omsPort.maxFrequency(), omsPort.grid());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700154 }
155
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700156 // Create installable optical path intent
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700157 // Only support fixed grid for now
158 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700159
160 Intent newIntent = OpticalPathIntent.builder()
161 .appId(intent.appId())
162 .src(intent.getSrc())
163 .dst(intent.getDst())
164 .path(path)
165 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700166 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700167 .bidirectional(intent.isBidirectional())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700168 .build();
169
170 return ImmutableList.of(newIntent);
171 }
172
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700173 // Release port allocations if unsuccessful
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700174 resourceService.release(intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700175
176 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700177 }
178
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700179 /**
180 * Find the lambda allocated to the path.
181 *
182 * @param path the path
183 * @param linkAllocs the link allocations
Sho SHIMIZU5a6e2e52015-09-09 15:04:12 -0700184 * @return lambda allocated to the given path
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700185 */
186 private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) {
Sho SHIMIZU280e7912015-09-29 13:42:03 -0700187 return path.links().stream()
188 .flatMap(x -> linkAllocs.getResourceAllocation(x).stream())
189 .filter(x -> x.type() == ResourceType.LAMBDA)
190 .findFirst()
191 .map(x -> (LambdaResourceAllocation) x)
192 .orElse(null);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700193 }
194
195 /**
196 * Request and reserve first available wavelength across path.
197 *
198 * @param path path in WDM topology
199 * @return first available lambda resource allocation
200 */
201 private LinkResourceAllocations assignWavelength(Intent intent, Path path) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700202 Set<LambdaResource> lambdas = findCommonLambdasOverLinks(path.links());
203 if (lambdas.isEmpty()) {
204 return null;
205 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700206
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700207 LambdaResource minLambda = findFirstLambda(lambdas);
208
209 LinkResourceRequest request =
210 DefaultLinkResourceRequest.builder(intent.id(), path.links())
211 .addLambdaRequest(minLambda)
212 .build();
213
214 LinkResourceAllocations allocations = linkResourceService.requestResources(request);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700215
Marc De Leenheer723f5532015-06-03 20:16:17 -0700216 if (!checkWavelengthContinuity(allocations, path)) {
217 linkResourceService.releaseResources(allocations);
218 return null;
219 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700220
221 return allocations;
222 }
223
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700224 private Set<LambdaResource> findCommonLambdasOverLinks(List<Link> links) {
225 return links.stream()
226 .map(x -> ImmutableSet.copyOf(linkResourceService.getAvailableResources(x)))
227 .map(x -> Sets.filter(x, req -> req instanceof LambdaResourceRequest))
228 .map(x -> Iterables.transform(x, req -> (LambdaResourceRequest) req))
229 .map(x -> Iterables.transform(x, LambdaResourceRequest::lambda))
230 .map(x -> (Set<LambdaResource>) ImmutableSet.copyOf(x))
231 .reduce(Sets::intersection)
232 .orElse(Collections.emptySet());
233 }
234
235 private LambdaResource findFirstLambda(Set<LambdaResource> lambdas) {
236 return lambdas.stream()
237 .findFirst()
238 .get();
239 }
240
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700241 /**
242 * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links.
243 * @return true if wavelength continuity is met, false otherwise
244 */
245 private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) {
246 if (allocations == null) {
247 return false;
248 }
249
Sho SHIMIZU280e7912015-09-29 13:42:03 -0700250 List<LambdaResource> lambdas = path.links().stream()
251 .flatMap(x -> allocations.getResourceAllocation(x).stream())
252 .filter(x -> x.type() == ResourceType.LAMBDA)
253 .map(x -> ((LambdaResourceAllocation) x).lambda())
254 .collect(Collectors.toList());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700255
Sho SHIMIZU280e7912015-09-29 13:42:03 -0700256 LambdaResource lambda = null;
257 for (LambdaResource nextLambda: lambdas) {
258 if (nextLambda == null) {
259 return false;
260 }
261 if (lambda == null) {
262 lambda = nextLambda;
263 continue;
264 }
265 if (!lambda.equals(nextLambda)) {
266 return false;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700267 }
268 }
269
270 return true;
271 }
272
Marc De Leenheer723f5532015-06-03 20:16:17 -0700273 private ConnectPoint staticPort(ConnectPoint connectPoint) {
274 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
275
276 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
277
278 // FIXME: need a better way to match the port
279 if (staticPort != null) {
280 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
281 if (staticPort.equals(p.number().name())) {
282 return new ConnectPoint(p.element().id(), p.number());
283 }
284 }
285 }
286
287 return null;
288 }
289
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700290 /**
291 * Calculates optical paths in WDM topology.
292 *
293 * @param intent optical connectivity intent
294 * @return set of paths in WDM topology
295 */
296 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
297 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700298 Topology topology = topologyService.currentTopology();
Sho SHIMIZU74626412015-09-11 11:46:27 -0700299 LinkWeight weight = edge -> {
300 // Disregard inactive or non-optical links
301 if (edge.link().state() == Link.State.INACTIVE) {
302 return -1;
weibit7e583462014-10-23 10:14:05 -0700303 }
Sho SHIMIZU74626412015-09-11 11:46:27 -0700304 if (edge.link().type() != Link.Type.OPTICAL) {
305 return -1;
306 }
307 // Adhere to static port mappings
308 DeviceId srcDeviceId = edge.link().src().deviceId();
309 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
310 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
311 if (srcStaticPort != null) {
312 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
313 }
314 }
315 DeviceId dstDeviceId = edge.link().dst().deviceId();
316 if (dstDeviceId.equals(intent.getDst().deviceId())) {
317 ConnectPoint dstStaticPort = staticPort(intent.getDst());
318 if (dstStaticPort != null) {
319 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
320 }
321 }
322
323 return 1;
weibit7e583462014-10-23 10:14:05 -0700324 };
weibitf32383b2014-10-22 10:17:31 -0700325
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700326 ConnectPoint start = intent.getSrc();
327 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700328 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700329 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700330
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700331 return paths;
weibitf32383b2014-10-22 10:17:31 -0700332 }
weibitf32383b2014-10-22 10:17:31 -0700333}