blob: 05a20f961c3e65bff254184d21b0de2a22173078 [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 SHIMIZU47eb3bd2015-06-12 11:41:23 -070019import com.google.common.collect.ImmutableSet;
weibitf32383b2014-10-22 10:17:31 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070025import org.onlab.util.Frequency;
Marc De Leenheer723f5532015-06-03 20:16:17 -070026import org.onosproject.net.AnnotationKeys;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
Marc De Leenheer723f5532015-06-03 20:16:17 -070028import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070030import org.onosproject.net.OchPort;
31import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070032import org.onosproject.net.OchSignalType;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070033import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070035import org.onosproject.net.Port;
36import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.intent.Intent;
38import org.onosproject.net.intent.IntentCompiler;
39import org.onosproject.net.intent.IntentExtensionService;
40import org.onosproject.net.intent.OpticalConnectivityIntent;
41import org.onosproject.net.intent.OpticalPathIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070042import org.onosproject.net.intent.impl.IntentCompilationException;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070043import org.onosproject.net.resource.ResourceAllocation;
44import 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;
60
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070061import static com.google.common.base.Preconditions.checkArgument;
62
weibitf32383b2014-10-22 10:17:31 -070063/**
Brian O'Connorabafb502014-12-02 22:26:20 -080064 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070065 */
66@Component(immediate = true)
67public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
68
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070069 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
70
weibitf32383b2014-10-22 10:17:31 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected IntentExtensionService intentManager;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070075 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070076
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected DeviceService deviceService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected LinkResourceService linkResourceService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected DeviceResourceService deviceResourceService;
85
weibitf32383b2014-10-22 10:17:31 -070086 @Activate
87 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070088 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
89 }
90
91 @Deactivate
92 public void deactivate() {
93 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
94 }
95
96 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070097 public List<Intent> compile(OpticalConnectivityIntent intent,
98 List<Intent> installable,
99 Set<LinkResourceAllocations> resources) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700100 // Check if source and destination are optical OCh ports
101 ConnectPoint src = intent.getSrc();
102 ConnectPoint dst = intent.getDst();
103 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
104 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
105 checkArgument(srcPort instanceof OchPort);
106 checkArgument(dstPort instanceof OchPort);
107
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700108 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
109
110 // Reserve OCh ports
Sho SHIMIZU47eb3bd2015-06-12 11:41:23 -0700111 if (!deviceResourceService.requestPorts(ImmutableSet.of(srcPort, dstPort), intent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700112 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
113 }
114
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700115 // Calculate available light paths
116 Set<Path> paths = getOpticalPaths(intent);
117
118 // Use first path that can be successfully reserved
119 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700120
121 // Static or dynamic lambda allocation
Marc De Leenheer723f5532015-06-03 20:16:17 -0700122 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
Marc De Leenheerc9733082015-06-04 12:22:38 -0700123 OchPort srcOchPort = (OchPort) srcPort;
124 OchPort dstOchPort = (OchPort) dstPort;
125 OchSignal ochSignal;
126
127 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700128 if (staticLambda != null) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -0700129 ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Marc De Leenheerc9733082015-06-04 12:22:38 -0700130 srcOchPort.lambda().channelSpacing(),
131 srcOchPort.lambda().slotGranularity());
132 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
133 // FIXME: also check OCh port
134 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700135 } else {
136 // Request and reserve lambda on path
137 LinkResourceAllocations linkAllocs = assignWavelength(intent, path);
138 if (linkAllocs == null) {
139 continue;
140 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700141 LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
142 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700143 ochSignal = new OchSignal(lambdaAlloc.lambda().toInt(), omsPort.maxFrequency(), omsPort.grid());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700144 }
145
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700146 // Create installable optical path intent
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700147 // Only support fixed grid for now
148 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700149
150 Intent newIntent = OpticalPathIntent.builder()
151 .appId(intent.appId())
152 .src(intent.getSrc())
153 .dst(intent.getDst())
154 .path(path)
155 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700156 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700157 .bidirectional(intent.isBidirectional())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700158 .build();
159
160 return ImmutableList.of(newIntent);
161 }
162
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700163 // Release port allocations if unsuccessful
164 deviceResourceService.releasePorts(intent.id());
165
166 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700167 }
168
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700169 /**
170 * Find the lambda allocated to the path.
171 *
172 * @param path the path
173 * @param linkAllocs the link allocations
Sho SHIMIZU5a6e2e52015-09-09 15:04:12 -0700174 * @return lambda allocated to the given path
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700175 */
176 private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) {
177 for (Link link : path.links()) {
178 for (ResourceAllocation alloc : linkAllocs.getResourceAllocation(link)) {
179 if (alloc.type() == ResourceType.LAMBDA) {
180 return (LambdaResourceAllocation) alloc;
181 }
182 }
183 }
184
185 return null;
186 }
187
188 /**
189 * Request and reserve first available wavelength across path.
190 *
191 * @param path path in WDM topology
192 * @return first available lambda resource allocation
193 */
194 private LinkResourceAllocations assignWavelength(Intent intent, Path path) {
195 LinkResourceRequest.Builder request =
196 DefaultLinkResourceRequest.builder(intent.id(), path.links())
197 .addLambdaRequest();
198
199 LinkResourceAllocations allocations = linkResourceService.requestResources(request.build());
200
Marc De Leenheer723f5532015-06-03 20:16:17 -0700201 if (!checkWavelengthContinuity(allocations, path)) {
202 linkResourceService.releaseResources(allocations);
203 return null;
204 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700205
206 return allocations;
207 }
208
209 /**
210 * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links.
211 * @return true if wavelength continuity is met, false otherwise
212 */
213 private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) {
214 if (allocations == null) {
215 return false;
216 }
217
218 LambdaResource lambda = null;
219
220 for (Link link : path.links()) {
221 for (ResourceAllocation alloc : allocations.getResourceAllocation(link)) {
222 if (alloc.type() == ResourceType.LAMBDA) {
223 LambdaResource nextLambda = ((LambdaResourceAllocation) alloc).lambda();
224 if (nextLambda == null) {
225 return false;
226 }
227 if (lambda == null) {
228 lambda = nextLambda;
229 continue;
230 }
231 if (!lambda.equals(nextLambda)) {
232 return false;
233 }
234 }
235 }
236 }
237
238 return true;
239 }
240
Marc De Leenheer723f5532015-06-03 20:16:17 -0700241 private ConnectPoint staticPort(ConnectPoint connectPoint) {
242 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
243
244 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
245
246 // FIXME: need a better way to match the port
247 if (staticPort != null) {
248 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
249 if (staticPort.equals(p.number().name())) {
250 return new ConnectPoint(p.element().id(), p.number());
251 }
252 }
253 }
254
255 return null;
256 }
257
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700258 /**
259 * Calculates optical paths in WDM topology.
260 *
261 * @param intent optical connectivity intent
262 * @return set of paths in WDM topology
263 */
264 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
265 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700266 Topology topology = topologyService.currentTopology();
Sho SHIMIZU74626412015-09-11 11:46:27 -0700267 LinkWeight weight = edge -> {
268 // Disregard inactive or non-optical links
269 if (edge.link().state() == Link.State.INACTIVE) {
270 return -1;
weibit7e583462014-10-23 10:14:05 -0700271 }
Sho SHIMIZU74626412015-09-11 11:46:27 -0700272 if (edge.link().type() != Link.Type.OPTICAL) {
273 return -1;
274 }
275 // Adhere to static port mappings
276 DeviceId srcDeviceId = edge.link().src().deviceId();
277 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
278 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
279 if (srcStaticPort != null) {
280 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
281 }
282 }
283 DeviceId dstDeviceId = edge.link().dst().deviceId();
284 if (dstDeviceId.equals(intent.getDst().deviceId())) {
285 ConnectPoint dstStaticPort = staticPort(intent.getDst());
286 if (dstStaticPort != null) {
287 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
288 }
289 }
290
291 return 1;
weibit7e583462014-10-23 10:14:05 -0700292 };
weibitf32383b2014-10-22 10:17:31 -0700293
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700294 ConnectPoint start = intent.getSrc();
295 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700296 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700297 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700298
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700299 return paths;
weibitf32383b2014-10-22 10:17:31 -0700300 }
weibitf32383b2014-10-22 10:17:31 -0700301}