blob: bcb26099ea08b200b87e2837612f39fa4347a987 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.optical;
weibitf32383b2014-10-22 10:17:31 -070017
Brian O'Connor772852a2014-11-17 15:51:19 -080018import com.google.common.collect.Lists;
weibitf32383b2014-10-22 10:17:31 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
Marc De Leenheer16f857b2015-05-05 20:50:24 -070021import org.apache.felix.scr.annotations.Deactivate;
weibitf32383b2014-10-22 10:17:31 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080024import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.NodeId;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080028import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070030import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Host;
32import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070033import org.onosproject.net.OduCltPort;
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.host.HostService;
38import org.onosproject.net.intent.HostToHostIntent;
39import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentEvent;
41import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.intent.IntentService;
43import org.onosproject.net.intent.IntentState;
44import org.onosproject.net.intent.OpticalConnectivityIntent;
45import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070046import org.onosproject.net.resource.device.DeviceResourceService;
47import org.onosproject.net.resource.link.LinkResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.topology.LinkWeight;
49import org.onosproject.net.topology.PathService;
50import org.onosproject.net.topology.TopologyEdge;
weibitf32383b2014-10-22 10:17:31 -070051import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Marc De Leenheer16f857b2015-05-05 20:50:24 -070054import java.util.Collections;
Brian O'Connor772852a2014-11-17 15:51:19 -080055import java.util.Iterator;
Marc De Leenheer16f857b2015-05-05 20:50:24 -070056import java.util.LinkedList;
Brian O'Connor772852a2014-11-17 15:51:19 -080057import java.util.List;
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -080058import java.util.Map;
Brian O'Connor772852a2014-11-17 15:51:19 -080059import java.util.Set;
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -080060import java.util.concurrent.ConcurrentHashMap;
61
Marc De Leenheer16f857b2015-05-05 20:50:24 -070062import static com.google.common.base.Preconditions.checkArgument;
Praseed Balakrishnanc0029652014-11-14 13:38:49 -080063
Marc De Leenheer16f857b2015-05-05 20:50:24 -070064import static com.google.common.base.Preconditions.checkNotNull;
65
weibitf32383b2014-10-22 10:17:31 -070066/**
Marc De Leenheer16f857b2015-05-05 20:50:24 -070067 * OpticalPathProvisioner listens for event notifications from the Intent F/W.
weibitf32383b2014-10-22 10:17:31 -070068 * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W
69 * for adding/releasing capacity at the packet layer.
weibitf32383b2014-10-22 10:17:31 -070070 */
71
72@Component(immediate = true)
73public class OpticalPathProvisioner {
74
75 protected static final Logger log = LoggerFactory
76 .getLogger(OpticalPathProvisioner.class);
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 private IntentService intentService;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Brian O'Connor772852a2014-11-17 15:51:19 -080082 protected PathService pathService;
weibitf32383b2014-10-22 10:17:31 -070083
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected CoreService coreService;
86
weibit7e583462014-10-23 10:14:05 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Praseed Balakrishnanc0029652014-11-14 13:38:49 -080088 protected HostService hostService;
89
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080090 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected MastershipService mastershipService;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected ClusterService clusterService;
95
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070097 protected DeviceService deviceService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected DeviceResourceService deviceResourceService;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected LinkResourceService linkResourceService;
104
105 private ApplicationId appId;
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800106
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700107 private final Map<ConnectPoint, Map<ConnectPoint, Intent>> intentMap =
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800108 new ConcurrentHashMap<>();
weibitf32383b2014-10-22 10:17:31 -0700109
110 private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner();
111
112 @Activate
113 protected void activate() {
114 intentService.addListener(pathProvisioner);
Brian O'Connorabafb502014-12-02 22:26:20 -0800115 appId = coreService.registerApplication("org.onosproject.optical");
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700116 initOpticalPorts();
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700117 log.info("Started");
118 }
119
120 @Deactivate
121 protected void deactivate() {
122 intentService.removeListener(pathProvisioner);
123 log.info("Stopped");
weibitf32383b2014-10-22 10:17:31 -0700124 }
125
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700126 /**
127 * Initialize availability of optical ports.
128 */
129 private void initOpticalPorts() {
130 // TODO: check for existing optical intents
131 return;
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800132 }
133
weibitf32383b2014-10-22 10:17:31 -0700134 public class InternalOpticalPathProvisioner implements IntentListener {
135 @Override
136 public void event(IntentEvent event) {
137 switch (event.type()) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800138 case INSTALL_REQ:
weibitf32383b2014-10-22 10:17:31 -0700139 break;
140 case INSTALLED:
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700141 // track h2h & p2p intents using our connectivity
weibitf32383b2014-10-22 10:17:31 -0700142 break;
143 case FAILED:
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700144 log.info("Intent {} failed, calling optical path provisioning app.", event.subject());
Brian O'Connor772852a2014-11-17 15:51:19 -0800145 setupLightpath(event.subject());
weibitf32383b2014-10-22 10:17:31 -0700146 break;
147 case WITHDRAWN:
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700148 log.info("Intent {} withdrawn.", event.subject());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700149 withdrawIntent(event.subject());
weibitf32383b2014-10-22 10:17:31 -0700150 break;
151 default:
152 break;
153 }
154 }
155
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800156 /**
157 * Registers an intent from src to dst.
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700158 *
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800159 * @param src source point
160 * @param dst destination point
161 * @param intent intent to be registered
162 * @return true if intent has not been previously added, false otherwise
163 */
164 private boolean addIntent(ConnectPoint src, ConnectPoint dst, Intent intent) {
165 Map<ConnectPoint, Intent> srcMap = intentMap.get(src);
166 if (srcMap == null) {
167 srcMap = new ConcurrentHashMap<>();
168 intentMap.put(src, srcMap);
169 }
170 if (srcMap.containsKey(dst)) {
171 return false;
172 } else {
173 srcMap.put(dst, intent);
174 return true;
175 }
176 }
177
Brian O'Connor772852a2014-11-17 15:51:19 -0800178 private void setupLightpath(Intent intent) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700179 checkNotNull(intent);
180
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800181 // TODO change the coordination approach between packet intents and optical intents
182 // Low speed LLDP may cause multiple calls which are not expected
183
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800184 if (!IntentState.FAILED.equals(intentService.getIntentState(intent.key()))) {
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800185 return;
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800186 }
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800187
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700188 // Get source and destination based on intent type
189 ConnectPoint src;
190 ConnectPoint dst;
Brian O'Connor772852a2014-11-17 15:51:19 -0800191 if (intent instanceof HostToHostIntent) {
192 HostToHostIntent hostToHostIntent = (HostToHostIntent) intent;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800193
Brian O'Connor772852a2014-11-17 15:51:19 -0800194 Host one = hostService.getHost(hostToHostIntent.one());
195 Host two = hostService.getHost(hostToHostIntent.two());
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800196
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700197 checkNotNull(one);
198 checkNotNull(two);
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800199
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700200 src = one.location();
201 dst = two.location();
Brian O'Connor772852a2014-11-17 15:51:19 -0800202 } else if (intent instanceof PointToPointIntent) {
203 PointToPointIntent p2pIntent = (PointToPointIntent) intent;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800204
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700205 src = p2pIntent.ingressPoint();
206 dst = p2pIntent.egressPoint();
Brian O'Connor772852a2014-11-17 15:51:19 -0800207 } else {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700208 log.error("Unsupported intent type: {}", intent.getClass());
209 return;
Brian O'Connor772852a2014-11-17 15:51:19 -0800210 }
Praseed Balakrishnanc0029652014-11-14 13:38:49 -0800211
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700212 if (src == null || dst == null) {
213 return;
214 }
215
216 // Ignore if we're not the master for the intent's origin device
217 NodeId localNode = clusterService.getLocalNode().id();
218 NodeId sourceMaster = mastershipService.getMasterFor(src.deviceId());
219 if (!localNode.equals(sourceMaster)) {
220 return;
221 }
222
223 // Generate optical connectivity intents
224 List<Intent> intents = Lists.newArrayList();
225 intents.addAll(getOpticalIntents(src, dst));
226
227 // Submit the intents
Brian O'Connor772852a2014-11-17 15:51:19 -0800228 for (Intent i : intents) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700229 intentService.submit(i);
Brian O'Connor772852a2014-11-17 15:51:19 -0800230 }
weibit7e583462014-10-23 10:14:05 -0700231 }
232
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700233 /**
234 * Returns list of cross connection points of missing optical path sections.
235 *
236 * Scans the given multi-layer path and looks for sections that use cross connect links.
237 * The ingress and egress points in the optical layer are returned in a list.
238 *
239 * @param path the multi-layer path
240 * @return list of cross connection points on the optical layer
241 */
242 private List<ConnectPoint> getCrossConnectPoints(Path path) {
243 boolean scanning = false;
244 List<ConnectPoint> connectPoints = new LinkedList<ConnectPoint>();
245
246 for (Link link : path.links()) {
247 if (!isCrossConnectLink(link)) {
248 continue;
249 }
250
251 if (scanning) {
252 connectPoints.add(checkNotNull(link.src()));
253 scanning = false;
254 } else {
255 connectPoints.add(checkNotNull(link.dst()));
256 scanning = true;
257 }
258 }
259
260 return connectPoints;
261 }
262
263 /**
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700264 * Checks if cross connect points are of same type.
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700265 *
266 * @param crossConnectPoints list of cross connection points
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700267 * @return true if cross connect point pairs are of same type, false otherwise
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700268 */
269 private boolean checkCrossConnectPoints(List<ConnectPoint> crossConnectPoints) {
270 checkArgument(crossConnectPoints.size() % 2 == 0);
271
272 Iterator<ConnectPoint> itr = crossConnectPoints.iterator();
273
274 while (itr.hasNext()) {
275 // checkArgument at start ensures we'll always have pairs of connect points
276 ConnectPoint src = itr.next();
277 ConnectPoint dst = itr.next();
278
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700279 Device.Type srcType = deviceService.getDevice(src.deviceId()).type();
280 Device.Type dstType = deviceService.getDevice(dst.deviceId()).type();
281
282 // Only support connections between identical port types
283 if (srcType != dstType) {
284 log.warn("Unsupported mix of cross connect points");
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700285 return false;
286 }
287 }
288
289 return true;
290 }
291
292 /**
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700293 * Scans the list of cross connection points and returns a list of optical connectivity intents.
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700294 *
295 * @param crossConnectPoints list of cross connection points
296 * @return list of optical connectivity intents
297 */
298 private List<Intent> getIntents(List<ConnectPoint> crossConnectPoints) {
299 checkArgument(crossConnectPoints.size() % 2 == 0);
300
Sho SHIMIZU79945e82015-05-20 17:20:47 -0700301 List<Intent> intents = new LinkedList<>();
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700302 Iterator<ConnectPoint> itr = crossConnectPoints.iterator();
303
304 while (itr.hasNext()) {
305 // checkArgument at start ensures we'll always have pairs of connect points
306 ConnectPoint src = itr.next();
307 ConnectPoint dst = itr.next();
308
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700309 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
310 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
311 // Create lightpath
312 // TODO: Ensure src & dst are of type OchPort
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700313 Intent opticalIntent = OpticalConnectivityIntent.builder()
314 .appId(appId)
315 .src(src)
316 .dst(dst)
317 .build();
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700318 intents.add(opticalIntent);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700319 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
320 continue;
321 // also create OTN service
322 }
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700323 }
324
325 return intents;
326 }
327
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700328 /**
329 * Returns list of optical connectivity intents needed to create connectivity
330 * between ingress and egress.
331 *
332 * @param ingress the ingress connect point
333 * @param egress the egress connect point
334 * @return list of optical connectivity intents, empty list if no path was found
335 */
336 private List<Intent> getOpticalIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor772852a2014-11-17 15:51:19 -0800337 Set<Path> paths = pathService.getPaths(ingress.deviceId(),
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700338 egress.deviceId(),
339 new OpticalLinkWeight());
Brian O'Connor772852a2014-11-17 15:51:19 -0800340
341 if (paths.isEmpty()) {
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700342 return Collections.emptyList();
weibit7e583462014-10-23 10:14:05 -0700343 }
Brian O'Connor772852a2014-11-17 15:51:19 -0800344
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700345 // Search path with available cross connect points
346 for (Path path : paths) {
347 List<ConnectPoint> crossConnectPoints = getCrossConnectPoints(path);
Brian O'Connor772852a2014-11-17 15:51:19 -0800348
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700349 // Skip to next path if cross connect points are mismatched
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700350 if (!checkCrossConnectPoints(crossConnectPoints)) {
351 continue;
Brian O'Connor772852a2014-11-17 15:51:19 -0800352 }
353
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700354 return getIntents(crossConnectPoints);
Brian O'Connor772852a2014-11-17 15:51:19 -0800355 }
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800356
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700357 return Collections.emptyList();
Brian O'Connor772852a2014-11-17 15:51:19 -0800358 }
359
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700360 /**
361 * Link weight function that emphasizes re-use of packet links.
362 */
363 private class OpticalLinkWeight implements LinkWeight {
364 @Override
365 public double weight(TopologyEdge edge) {
366 // Ignore inactive links
367 if (edge.link().state() == Link.State.INACTIVE) {
368 return -1;
369 }
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800370
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700371 // TODO: Ignore cross connect links with used ports
372
373 // Transport links have highest weight
374 if (edge.link().type() == Link.Type.OPTICAL) {
375 return 1000;
376 }
377
378 // Packet links
379 return 1;
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800380 }
weibitf32383b2014-10-22 10:17:31 -0700381 }
Brian O'Connorc7bdd8c2014-12-08 01:29:53 -0800382
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700383 /**
384 * Handle withdrawn intent on each network layer.
385 *
386 * @param intent the withdrawn intent
387 */
388 private void withdrawIntent(Intent intent) {
389 if (intent instanceof OpticalConnectivityIntent) {
390 deviceResourceService.releasePorts(intent.id());
391 linkResourceService.releaseResources(linkResourceService.getAllocations(intent.id()));
392 }
393 // TODO: add other layers
394 }
Brian O'Connor772852a2014-11-17 15:51:19 -0800395 }
weibitf32383b2014-10-22 10:17:31 -0700396
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700397 /**
398 * Verifies if given link is cross-connect between packet and optical layer.
399 *
400 * @param link the link
401 * @return true if the link is a cross-connect link
402 */
403 public static boolean isCrossConnectLink(Link link) {
404 if (link.type() != Link.Type.OPTICAL) {
405 return false;
Praseed Balakrishnanc0029652014-11-14 13:38:49 -0800406 }
Marc De Leenheer16f857b2015-05-05 20:50:24 -0700407
408 checkNotNull(link.annotations());
409 checkNotNull(link.annotations().value("optical.type"));
410
411 if (link.annotations().value("optical.type").equals("cross-connect")) {
412 return true;
413 }
414
415 return false;
Brian O'Connor772852a2014-11-17 15:51:19 -0800416 }
Praseed Balakrishnanc0029652014-11-14 13:38:49 -0800417
weibitf32383b2014-10-22 10:17:31 -0700418}