blob: d6725b7c70a16353fc4f83934744bd4ef5c1e83c [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;
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -070031import org.onosproject.net.IndexedLambda;
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;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070036import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070038import org.onosproject.net.Port;
39import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.intent.Intent;
41import org.onosproject.net.intent.IntentCompiler;
42import org.onosproject.net.intent.IntentExtensionService;
43import org.onosproject.net.intent.OpticalConnectivityIntent;
44import org.onosproject.net.intent.OpticalPathIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070045import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -070046import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070047import org.onosproject.net.newresource.ResourcePath;
48import org.onosproject.net.newresource.ResourceService;
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
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070056import java.util.Collections;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070057import java.util.List;
58import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070059import java.util.stream.Collectors;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070060
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070061import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -070062import static org.onosproject.net.LinkKey.linkKey;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070063
weibitf32383b2014-10-22 10:17:31 -070064/**
Brian O'Connorabafb502014-12-02 22:26:20 -080065 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070066 */
Ray Milkeye6099412015-10-05 10:53:17 -070067// For now, remove component designation until dependency on the new resource manager is available.
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);
72
weibitf32383b2014-10-22 10:17:31 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected IntentExtensionService intentManager;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070077 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070078
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected DeviceService deviceService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070083 protected ResourceService resourceService;
84
weibitf32383b2014-10-22 10:17:31 -070085 @Activate
86 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070087 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
88 }
89
90 @Deactivate
91 public void deactivate() {
92 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
93 }
94
95 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070096 public List<Intent> compile(OpticalConnectivityIntent intent,
97 List<Intent> installable,
98 Set<LinkResourceAllocations> resources) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070099 // Check if source and destination are optical OCh ports
100 ConnectPoint src = intent.getSrc();
101 ConnectPoint dst = intent.getDst();
102 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
103 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
104 checkArgument(srcPort instanceof OchPort);
105 checkArgument(dstPort instanceof OchPort);
106
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700107 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
108
109 // Reserve OCh ports
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700110 ResourcePath srcPortPath = new ResourcePath(src.deviceId(), src.port());
111 ResourcePath dstPortPath = new ResourcePath(dst.deviceId(), dst.port());
112 List<org.onosproject.net.newresource.ResourceAllocation> allocation =
113 resourceService.allocate(intent.id(), srcPortPath, dstPortPath);
114 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700115 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
116 }
117
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700118 // Calculate available light paths
119 Set<Path> paths = getOpticalPaths(intent);
120
121 // Use first path that can be successfully reserved
122 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700123
124 // Static or dynamic lambda allocation
Marc De Leenheer723f5532015-06-03 20:16:17 -0700125 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
Marc De Leenheerc9733082015-06-04 12:22:38 -0700126 OchPort srcOchPort = (OchPort) srcPort;
127 OchPort dstOchPort = (OchPort) dstPort;
128 OchSignal ochSignal;
129
130 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700131 if (staticLambda != null) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -0700132 ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Marc De Leenheerc9733082015-06-04 12:22:38 -0700133 srcOchPort.lambda().channelSpacing(),
134 srcOchPort.lambda().slotGranularity());
135 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
136 // FIXME: also check OCh port
137 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700138 } else {
139 // Request and reserve lambda on path
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700140 IndexedLambda lambda = assignWavelength(intent, path);
141 if (lambda == null) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700142 continue;
143 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700144 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700145 ochSignal = new OchSignal((int) lambda.index(), omsPort.maxFrequency(), omsPort.grid());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700146 }
147
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700148 // Create installable optical path intent
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700149 // Only support fixed grid for now
150 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700151
152 Intent newIntent = OpticalPathIntent.builder()
153 .appId(intent.appId())
154 .src(intent.getSrc())
155 .dst(intent.getDst())
156 .path(path)
157 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700158 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700159 .bidirectional(intent.isBidirectional())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700160 .build();
161
162 return ImmutableList.of(newIntent);
163 }
164
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700165 // Release port allocations if unsuccessful
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700166 resourceService.release(intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700167
168 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700169 }
170
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700171 /**
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700172 * Request and reserve first available wavelength across path.
173 *
174 * @param path path in WDM topology
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700175 * @return first available lambda allocated
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700176 */
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700177 private IndexedLambda assignWavelength(Intent intent, Path path) {
178 Set<IndexedLambda> lambdas = findCommonLambdasOverLinks(path.links());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700179 if (lambdas.isEmpty()) {
180 return null;
181 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700182
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700183 IndexedLambda minLambda = findFirstLambda(lambdas);
184 List<ResourcePath> lambdaResources = path.links().stream()
185 .map(x -> new ResourcePath(linkKey(x.src(), x.dst())))
186 .map(x -> ResourcePath.child(x, minLambda))
187 .collect(Collectors.toList());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700188
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700189 List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), lambdaResources);
190 if (allocations.isEmpty()) {
191 log.info("Resource allocation for {} failed (resource request: {})", intent, lambdaResources);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700192 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700193
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700194 return minLambda;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700195 }
196
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700197 private Set<IndexedLambda> findCommonLambdasOverLinks(List<Link> links) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700198 return links.stream()
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700199 .map(x -> new ResourcePath(linkKey(x.src(), x.dst())))
200 .map(resourceService::getAvailableResources)
201 .map(x -> Iterables.filter(x, r -> r.lastComponent() instanceof IndexedLambda))
202 .map(x -> Iterables.transform(x, r -> (IndexedLambda) r.lastComponent()))
203 .map(x -> (Set<IndexedLambda>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700204 .reduce(Sets::intersection)
205 .orElse(Collections.emptySet());
206 }
207
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700208 private IndexedLambda findFirstLambda(Set<IndexedLambda> lambdas) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700209 return lambdas.stream()
210 .findFirst()
211 .get();
212 }
213
Marc De Leenheer723f5532015-06-03 20:16:17 -0700214 private ConnectPoint staticPort(ConnectPoint connectPoint) {
215 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
216
217 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
218
219 // FIXME: need a better way to match the port
220 if (staticPort != null) {
221 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
222 if (staticPort.equals(p.number().name())) {
223 return new ConnectPoint(p.element().id(), p.number());
224 }
225 }
226 }
227
228 return null;
229 }
230
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700231 /**
232 * Calculates optical paths in WDM topology.
233 *
234 * @param intent optical connectivity intent
235 * @return set of paths in WDM topology
236 */
237 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
238 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700239 Topology topology = topologyService.currentTopology();
Sho SHIMIZU74626412015-09-11 11:46:27 -0700240 LinkWeight weight = edge -> {
241 // Disregard inactive or non-optical links
242 if (edge.link().state() == Link.State.INACTIVE) {
243 return -1;
weibit7e583462014-10-23 10:14:05 -0700244 }
Sho SHIMIZU74626412015-09-11 11:46:27 -0700245 if (edge.link().type() != Link.Type.OPTICAL) {
246 return -1;
247 }
248 // Adhere to static port mappings
249 DeviceId srcDeviceId = edge.link().src().deviceId();
250 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
251 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
252 if (srcStaticPort != null) {
253 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
254 }
255 }
256 DeviceId dstDeviceId = edge.link().dst().deviceId();
257 if (dstDeviceId.equals(intent.getDst().deviceId())) {
258 ConnectPoint dstStaticPort = staticPort(intent.getDst());
259 if (dstStaticPort != null) {
260 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
261 }
262 }
263
264 return 1;
weibit7e583462014-10-23 10:14:05 -0700265 };
weibitf32383b2014-10-22 10:17:31 -0700266
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700267 ConnectPoint start = intent.getSrc();
268 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700269 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700270 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700271
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700272 return paths;
weibitf32383b2014-10-22 10:17:31 -0700273 }
weibitf32383b2014-10-22 10:17:31 -0700274}