blob: 58c5907a11693e60098df0130305d5f2fd1ab02b [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
Ray Milkeye076c792015-03-24 09:38:30 -070018import java.util.List;
19import java.util.Set;
20
Sho SHIMIZU47eb3bd2015-06-12 11:41:23 -070021import com.google.common.collect.ImmutableSet;
weibitf32383b2014-10-22 10:17:31 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import 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;
Brian O'Connor6de2e202015-05-21 14:30:41 -070045import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
46import org.onosproject.net.resource.device.DeviceResourceService;
47import 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;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070052import org.onosproject.net.resource.ResourceAllocation;
53import org.onosproject.net.resource.ResourceType;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.topology.LinkWeight;
55import org.onosproject.net.topology.Topology;
56import org.onosproject.net.topology.TopologyEdge;
57import org.onosproject.net.topology.TopologyService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070058
Ray Milkeye076c792015-03-24 09:38:30 -070059import com.google.common.collect.ImmutableList;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070060import org.slf4j.Logger;
61import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070062
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070063import static com.google.common.base.Preconditions.checkArgument;
64
weibitf32383b2014-10-22 10:17:31 -070065/**
Brian O'Connorabafb502014-12-02 22:26:20 -080066 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070067 */
68@Component(immediate = true)
69public 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)
83 protected LinkResourceService linkResourceService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected DeviceResourceService deviceResourceService;
87
weibitf32383b2014-10-22 10:17:31 -070088 @Activate
89 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070090 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
91 }
92
93 @Deactivate
94 public void deactivate() {
95 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
96 }
97
98 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070099 public List<Intent> compile(OpticalConnectivityIntent intent,
100 List<Intent> installable,
101 Set<LinkResourceAllocations> resources) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700102 // Check if source and destination are optical OCh ports
103 ConnectPoint src = intent.getSrc();
104 ConnectPoint dst = intent.getDst();
105 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
106 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
107 checkArgument(srcPort instanceof OchPort);
108 checkArgument(dstPort instanceof OchPort);
109
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700110 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
111
112 // Reserve OCh ports
Sho SHIMIZU47eb3bd2015-06-12 11:41:23 -0700113 if (!deviceResourceService.requestPorts(ImmutableSet.of(srcPort, dstPort), intent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700114 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
115 }
116
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700117 // Calculate available light paths
118 Set<Path> paths = getOpticalPaths(intent);
119
120 // Use first path that can be successfully reserved
121 for (Path path : paths) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700122
123 // Static or dynamic lambda allocation
Marc De Leenheer723f5532015-06-03 20:16:17 -0700124 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
Marc De Leenheerc9733082015-06-04 12:22:38 -0700125 OchPort srcOchPort = (OchPort) srcPort;
126 OchPort dstOchPort = (OchPort) dstPort;
127 OchSignal ochSignal;
128
129 // FIXME: need to actually reserve the lambda for static lambda's
Marc De Leenheer723f5532015-06-03 20:16:17 -0700130 if (staticLambda != null) {
Marc De Leenheerc9733082015-06-04 12:22:38 -0700131 ochSignal = new OchSignal(Frequency.ofHz(Long.valueOf(staticLambda)),
132 srcOchPort.lambda().channelSpacing(),
133 srcOchPort.lambda().slotGranularity());
134 } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
135 // FIXME: also check OCh port
136 ochSignal = srcOchPort.lambda();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700137 } else {
138 // Request and reserve lambda on path
139 LinkResourceAllocations linkAllocs = assignWavelength(intent, path);
140 if (linkAllocs == null) {
141 continue;
142 }
Marc De Leenheerc9733082015-06-04 12:22:38 -0700143 LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
144 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
145 ochSignal = new OchSignal(lambdaAlloc, 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
166 deviceResourceService.releasePorts(intent.id());
167
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 /**
172 * Find the lambda allocated to the path.
173 *
174 * @param path the path
175 * @param linkAllocs the link allocations
176 * @return
177 */
178 private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) {
179 for (Link link : path.links()) {
180 for (ResourceAllocation alloc : linkAllocs.getResourceAllocation(link)) {
181 if (alloc.type() == ResourceType.LAMBDA) {
182 return (LambdaResourceAllocation) alloc;
183 }
184 }
185 }
186
187 return null;
188 }
189
190 /**
191 * Request and reserve first available wavelength across path.
192 *
193 * @param path path in WDM topology
194 * @return first available lambda resource allocation
195 */
196 private LinkResourceAllocations assignWavelength(Intent intent, Path path) {
197 LinkResourceRequest.Builder request =
198 DefaultLinkResourceRequest.builder(intent.id(), path.links())
199 .addLambdaRequest();
200
201 LinkResourceAllocations allocations = linkResourceService.requestResources(request.build());
202
Marc De Leenheer723f5532015-06-03 20:16:17 -0700203 if (!checkWavelengthContinuity(allocations, path)) {
204 linkResourceService.releaseResources(allocations);
205 return null;
206 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700207
208 return allocations;
209 }
210
211 /**
212 * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links.
213 * @return true if wavelength continuity is met, false otherwise
214 */
215 private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) {
216 if (allocations == null) {
217 return false;
218 }
219
220 LambdaResource lambda = null;
221
222 for (Link link : path.links()) {
223 for (ResourceAllocation alloc : allocations.getResourceAllocation(link)) {
224 if (alloc.type() == ResourceType.LAMBDA) {
225 LambdaResource nextLambda = ((LambdaResourceAllocation) alloc).lambda();
226 if (nextLambda == null) {
227 return false;
228 }
229 if (lambda == null) {
230 lambda = nextLambda;
231 continue;
232 }
233 if (!lambda.equals(nextLambda)) {
234 return false;
235 }
236 }
237 }
238 }
239
240 return true;
241 }
242
Marc De Leenheer723f5532015-06-03 20:16:17 -0700243 private ConnectPoint staticPort(ConnectPoint connectPoint) {
244 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
245
246 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
247
248 // FIXME: need a better way to match the port
249 if (staticPort != null) {
250 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
251 if (staticPort.equals(p.number().name())) {
252 return new ConnectPoint(p.element().id(), p.number());
253 }
254 }
255 }
256
257 return null;
258 }
259
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700260 /**
261 * Calculates optical paths in WDM topology.
262 *
263 * @param intent optical connectivity intent
264 * @return set of paths in WDM topology
265 */
266 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
267 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700268 Topology topology = topologyService.currentTopology();
269 LinkWeight weight = new LinkWeight() {
270 @Override
271 public double weight(TopologyEdge edge) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700272 // Disregard inactive or non-optical links
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800273 if (edge.link().state() == Link.State.INACTIVE) {
274 return -1;
275 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700276 if (edge.link().type() != Link.Type.OPTICAL) {
277 return -1;
278 }
Marc De Leenheer723f5532015-06-03 20:16:17 -0700279 // Adhere to static port mappings
280 DeviceId srcDeviceId = edge.link().src().deviceId();
281 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
282 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
283 if (srcStaticPort != null) {
284 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
285 }
286 }
287 DeviceId dstDeviceId = edge.link().dst().deviceId();
288 if (dstDeviceId.equals(intent.getDst().deviceId())) {
289 ConnectPoint dstStaticPort = staticPort(intent.getDst());
290 if (dstStaticPort != null) {
291 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
292 }
293 }
294
Marc De Leenheer88194c32015-05-29 22:10:59 -0700295 return 1;
weibit7e583462014-10-23 10:14:05 -0700296 }
297 };
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}