blob: b2403c68724d1ffd7b85b77100cd41e49ea24526 [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;
weibitf32383b2014-10-22 10:17:31 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070024import org.onlab.util.Frequency;
Marc De Leenheer723f5532015-06-03 20:16:17 -070025import org.onosproject.net.AnnotationKeys;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
Marc De Leenheer723f5532015-06-03 20:16:17 -070027import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070029import org.onosproject.net.OchPort;
30import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070031import org.onosproject.net.OchSignalType;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070032import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070034import org.onosproject.net.Port;
35import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentCompiler;
38import org.onosproject.net.intent.IntentExtensionService;
39import org.onosproject.net.intent.OpticalConnectivityIntent;
40import org.onosproject.net.intent.OpticalPathIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070041import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070042import org.onosproject.net.newresource.ResourcePath;
43import org.onosproject.net.newresource.ResourceService;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070044import org.onosproject.net.resource.ResourceType;
Brian O'Connor6de2e202015-05-21 14:30:41 -070045import org.onosproject.net.resource.device.DeviceResourceService;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070046import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
Brian O'Connor6de2e202015-05-21 14:30:41 -070047import org.onosproject.net.resource.link.LambdaResource;
48import org.onosproject.net.resource.link.LambdaResourceAllocation;
49import org.onosproject.net.resource.link.LinkResourceAllocations;
50import org.onosproject.net.resource.link.LinkResourceRequest;
51import org.onosproject.net.resource.link.LinkResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.topology.LinkWeight;
53import org.onosproject.net.topology.Topology;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070057
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;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070061
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070062import static com.google.common.base.Preconditions.checkArgument;
63
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 */
67@Component(immediate = true)
68public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
69
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070070 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
71
weibitf32383b2014-10-22 10:17:31 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected IntentExtensionService intentManager;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070076 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070077
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected DeviceService deviceService;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070082 protected ResourceService resourceService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070085 protected LinkResourceService linkResourceService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected DeviceResourceService deviceResourceService;
89
weibitf32383b2014-10-22 10:17:31 -070090 @Activate
91 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070092 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
93 }
94
95 @Deactivate
96 public void deactivate() {
97 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
98 }
99
100 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700101 public List<Intent> compile(OpticalConnectivityIntent intent,
102 List<Intent> installable,
103 Set<LinkResourceAllocations> resources) {
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
114 // Reserve OCh ports
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700115 ResourcePath srcPortPath = new ResourcePath(src.deviceId(), src.port());
116 ResourcePath dstPortPath = new ResourcePath(dst.deviceId(), dst.port());
117 List<org.onosproject.net.newresource.ResourceAllocation> allocation =
118 resourceService.allocate(intent.id(), srcPortPath, dstPortPath);
119 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700120 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
121 }
122
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700123 // Calculate available light paths
124 Set<Path> paths = getOpticalPaths(intent);
125
126 // Use first path that can be successfully reserved
127 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700128
129 // Static or dynamic lambda allocation
Marc De Leenheer723f5532015-06-03 20:16:17 -0700130 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
Marc De Leenheerc9733082015-06-04 12:22:38 -0700131 OchPort srcOchPort = (OchPort) srcPort;
132 OchPort dstOchPort = (OchPort) dstPort;
133 OchSignal ochSignal;
134
135 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700136 if (staticLambda != null) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -0700137 ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Marc De Leenheerc9733082015-06-04 12:22:38 -0700138 srcOchPort.lambda().channelSpacing(),
139 srcOchPort.lambda().slotGranularity());
140 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
141 // FIXME: also check OCh port
142 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700143 } else {
144 // Request and reserve lambda on path
145 LinkResourceAllocations linkAllocs = assignWavelength(intent, path);
146 if (linkAllocs == null) {
147 continue;
148 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700149 LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
150 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700151 ochSignal = new OchSignal(lambdaAlloc.lambda().toInt(), omsPort.maxFrequency(), omsPort.grid());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700152 }
153
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700154 // Create installable optical path intent
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700155 // Only support fixed grid for now
156 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700157
158 Intent newIntent = OpticalPathIntent.builder()
159 .appId(intent.appId())
160 .src(intent.getSrc())
161 .dst(intent.getDst())
162 .path(path)
163 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700164 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700165 .bidirectional(intent.isBidirectional())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700166 .build();
167
168 return ImmutableList.of(newIntent);
169 }
170
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700171 // Release port allocations if unsuccessful
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700172 resourceService.release(intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700173
174 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700175 }
176
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700177 /**
178 * Find the lambda allocated to the path.
179 *
180 * @param path the path
181 * @param linkAllocs the link allocations
Sho SHIMIZU5a6e2e52015-09-09 15:04:12 -0700182 * @return lambda allocated to the given path
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700183 */
184 private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) {
Sho SHIMIZU280e7912015-09-29 13:42:03 -0700185 return path.links().stream()
186 .flatMap(x -> linkAllocs.getResourceAllocation(x).stream())
187 .filter(x -> x.type() == ResourceType.LAMBDA)
188 .findFirst()
189 .map(x -> (LambdaResourceAllocation) x)
190 .orElse(null);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700191 }
192
193 /**
194 * Request and reserve first available wavelength across path.
195 *
196 * @param path path in WDM topology
197 * @return first available lambda resource allocation
198 */
199 private LinkResourceAllocations assignWavelength(Intent intent, Path path) {
200 LinkResourceRequest.Builder request =
201 DefaultLinkResourceRequest.builder(intent.id(), path.links())
202 .addLambdaRequest();
203
204 LinkResourceAllocations allocations = linkResourceService.requestResources(request.build());
205
Marc De Leenheer723f5532015-06-03 20:16:17 -0700206 if (!checkWavelengthContinuity(allocations, path)) {
207 linkResourceService.releaseResources(allocations);
208 return null;
209 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700210
211 return allocations;
212 }
213
214 /**
215 * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links.
216 * @return true if wavelength continuity is met, false otherwise
217 */
218 private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) {
219 if (allocations == null) {
220 return false;
221 }
222
Sho SHIMIZU280e7912015-09-29 13:42:03 -0700223 List<LambdaResource> lambdas = path.links().stream()
224 .flatMap(x -> allocations.getResourceAllocation(x).stream())
225 .filter(x -> x.type() == ResourceType.LAMBDA)
226 .map(x -> ((LambdaResourceAllocation) x).lambda())
227 .collect(Collectors.toList());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700228
Sho SHIMIZU280e7912015-09-29 13:42:03 -0700229 LambdaResource lambda = null;
230 for (LambdaResource nextLambda: lambdas) {
231 if (nextLambda == null) {
232 return false;
233 }
234 if (lambda == null) {
235 lambda = nextLambda;
236 continue;
237 }
238 if (!lambda.equals(nextLambda)) {
239 return false;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700240 }
241 }
242
243 return true;
244 }
245
Marc De Leenheer723f5532015-06-03 20:16:17 -0700246 private ConnectPoint staticPort(ConnectPoint connectPoint) {
247 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
248
249 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
250
251 // FIXME: need a better way to match the port
252 if (staticPort != null) {
253 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
254 if (staticPort.equals(p.number().name())) {
255 return new ConnectPoint(p.element().id(), p.number());
256 }
257 }
258 }
259
260 return null;
261 }
262
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700263 /**
264 * Calculates optical paths in WDM topology.
265 *
266 * @param intent optical connectivity intent
267 * @return set of paths in WDM topology
268 */
269 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
270 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700271 Topology topology = topologyService.currentTopology();
Sho SHIMIZU74626412015-09-11 11:46:27 -0700272 LinkWeight weight = edge -> {
273 // Disregard inactive or non-optical links
274 if (edge.link().state() == Link.State.INACTIVE) {
275 return -1;
weibit7e583462014-10-23 10:14:05 -0700276 }
Sho SHIMIZU74626412015-09-11 11:46:27 -0700277 if (edge.link().type() != Link.Type.OPTICAL) {
278 return -1;
279 }
280 // Adhere to static port mappings
281 DeviceId srcDeviceId = edge.link().src().deviceId();
282 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
283 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
284 if (srcStaticPort != null) {
285 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
286 }
287 }
288 DeviceId dstDeviceId = edge.link().dst().deviceId();
289 if (dstDeviceId.equals(intent.getDst().deviceId())) {
290 ConnectPoint dstStaticPort = staticPort(intent.getDst());
291 if (dstStaticPort != null) {
292 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
293 }
294 }
295
296 return 1;
weibit7e583462014-10-23 10:14:05 -0700297 };
weibitf32383b2014-10-22 10:17:31 -0700298
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700299 ConnectPoint start = intent.getSrc();
300 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700301 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700302 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700303
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700304 return paths;
weibitf32383b2014-10-22 10:17:31 -0700305 }
weibitf32383b2014-10-22 10:17:31 -0700306}