blob: c60325a77cf6a91cf874efbad79d5c253d8cd097 [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;
54import org.onosproject.net.topology.TopologyEdge;
55import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070058
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070059import java.util.List;
60import java.util.Set;
61
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)
82 protected LinkResourceService linkResourceService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected DeviceResourceService deviceResourceService;
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
111 // Reserve OCh ports
Sho SHIMIZU47eb3bd2015-06-12 11:41:23 -0700112 if (!deviceResourceService.requestPorts(ImmutableSet.of(srcPort, dstPort), intent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700113 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
114 }
115
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700116 // Calculate available light paths
117 Set<Path> paths = getOpticalPaths(intent);
118
119 // Use first path that can be successfully reserved
120 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700121
122 // Static or dynamic lambda allocation
Marc De Leenheer723f5532015-06-03 20:16:17 -0700123 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
Marc De Leenheerc9733082015-06-04 12:22:38 -0700124 OchPort srcOchPort = (OchPort) srcPort;
125 OchPort dstOchPort = (OchPort) dstPort;
126 OchSignal ochSignal;
127
128 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700129 if (staticLambda != null) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -0700130 ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Marc De Leenheerc9733082015-06-04 12:22:38 -0700131 srcOchPort.lambda().channelSpacing(),
132 srcOchPort.lambda().slotGranularity());
133 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
134 // FIXME: also check OCh port
135 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700136 } else {
137 // Request and reserve lambda on path
138 LinkResourceAllocations linkAllocs = assignWavelength(intent, path);
139 if (linkAllocs == null) {
140 continue;
141 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700142 LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
143 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700144 ochSignal = new OchSignal(lambdaAlloc.lambda().toInt(), omsPort.maxFrequency(), omsPort.grid());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700145 }
146
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700147 // Create installable optical path intent
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700148 // Only support fixed grid for now
149 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700150
151 Intent newIntent = OpticalPathIntent.builder()
152 .appId(intent.appId())
153 .src(intent.getSrc())
154 .dst(intent.getDst())
155 .path(path)
156 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700157 .signalType(signalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700158 .bidirectional(intent.isBidirectional())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700159 .build();
160
161 return ImmutableList.of(newIntent);
162 }
163
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700164 // Release port allocations if unsuccessful
165 deviceResourceService.releasePorts(intent.id());
166
167 throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
weibitf32383b2014-10-22 10:17:31 -0700168 }
169
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700170 /**
171 * Find the lambda allocated to the path.
172 *
173 * @param path the path
174 * @param linkAllocs the link allocations
Sho SHIMIZU5a6e2e52015-09-09 15:04:12 -0700175 * @return lambda allocated to the given path
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700176 */
177 private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) {
178 for (Link link : path.links()) {
179 for (ResourceAllocation alloc : linkAllocs.getResourceAllocation(link)) {
180 if (alloc.type() == ResourceType.LAMBDA) {
181 return (LambdaResourceAllocation) alloc;
182 }
183 }
184 }
185
186 return null;
187 }
188
189 /**
190 * Request and reserve first available wavelength across path.
191 *
192 * @param path path in WDM topology
193 * @return first available lambda resource allocation
194 */
195 private LinkResourceAllocations assignWavelength(Intent intent, Path path) {
196 LinkResourceRequest.Builder request =
197 DefaultLinkResourceRequest.builder(intent.id(), path.links())
198 .addLambdaRequest();
199
200 LinkResourceAllocations allocations = linkResourceService.requestResources(request.build());
201
Marc De Leenheer723f5532015-06-03 20:16:17 -0700202 if (!checkWavelengthContinuity(allocations, path)) {
203 linkResourceService.releaseResources(allocations);
204 return null;
205 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700206
207 return allocations;
208 }
209
210 /**
211 * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links.
212 * @return true if wavelength continuity is met, false otherwise
213 */
214 private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) {
215 if (allocations == null) {
216 return false;
217 }
218
219 LambdaResource lambda = null;
220
221 for (Link link : path.links()) {
222 for (ResourceAllocation alloc : allocations.getResourceAllocation(link)) {
223 if (alloc.type() == ResourceType.LAMBDA) {
224 LambdaResource nextLambda = ((LambdaResourceAllocation) alloc).lambda();
225 if (nextLambda == null) {
226 return false;
227 }
228 if (lambda == null) {
229 lambda = nextLambda;
230 continue;
231 }
232 if (!lambda.equals(nextLambda)) {
233 return false;
234 }
235 }
236 }
237 }
238
239 return true;
240 }
241
Marc De Leenheer723f5532015-06-03 20:16:17 -0700242 private ConnectPoint staticPort(ConnectPoint connectPoint) {
243 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
244
245 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
246
247 // FIXME: need a better way to match the port
248 if (staticPort != null) {
249 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
250 if (staticPort.equals(p.number().name())) {
251 return new ConnectPoint(p.element().id(), p.number());
252 }
253 }
254 }
255
256 return null;
257 }
258
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700259 /**
260 * Calculates optical paths in WDM topology.
261 *
262 * @param intent optical connectivity intent
263 * @return set of paths in WDM topology
264 */
265 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
266 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700267 Topology topology = topologyService.currentTopology();
268 LinkWeight weight = new LinkWeight() {
269 @Override
270 public double weight(TopologyEdge edge) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700271 // Disregard inactive or non-optical links
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800272 if (edge.link().state() == Link.State.INACTIVE) {
273 return -1;
274 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700275 if (edge.link().type() != Link.Type.OPTICAL) {
276 return -1;
277 }
Marc De Leenheer723f5532015-06-03 20:16:17 -0700278 // Adhere to static port mappings
279 DeviceId srcDeviceId = edge.link().src().deviceId();
280 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
281 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
282 if (srcStaticPort != null) {
283 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
284 }
285 }
286 DeviceId dstDeviceId = edge.link().dst().deviceId();
287 if (dstDeviceId.equals(intent.getDst().deviceId())) {
288 ConnectPoint dstStaticPort = staticPort(intent.getDst());
289 if (dstStaticPort != null) {
290 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
291 }
292 }
293
Marc De Leenheer88194c32015-05-29 22:10:59 -0700294 return 1;
weibit7e583462014-10-23 10:14:05 -0700295 }
296 };
weibitf32383b2014-10-22 10:17:31 -0700297
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700298 ConnectPoint start = intent.getSrc();
299 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700300 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700301 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700302
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700303 return paths;
weibitf32383b2014-10-22 10:17:31 -0700304 }
weibitf32383b2014-10-22 10:17:31 -0700305}