Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 19 | package org.onlab.onos.provider.lldp.impl; |
| 20 | |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 26 | import org.onlab.onos.mastership.MastershipService; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.ConnectPoint; |
| 28 | import org.onlab.onos.net.Device; |
| 29 | import org.onlab.onos.net.DeviceId; |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.Port; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.device.DeviceEvent; |
| 32 | import org.onlab.onos.net.device.DeviceListener; |
| 33 | import org.onlab.onos.net.device.DeviceService; |
| 34 | import org.onlab.onos.net.link.LinkProvider; |
| 35 | import org.onlab.onos.net.link.LinkProviderRegistry; |
| 36 | import org.onlab.onos.net.link.LinkProviderService; |
| 37 | import org.onlab.onos.net.packet.PacketContext; |
| 38 | import org.onlab.onos.net.packet.PacketProcessor; |
| 39 | import org.onlab.onos.net.packet.PacketService; |
| 40 | import org.onlab.onos.net.provider.AbstractProvider; |
| 41 | import org.onlab.onos.net.provider.ProviderId; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import java.util.Map; |
| 45 | import java.util.concurrent.ConcurrentHashMap; |
| 46 | |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Provider which uses an OpenFlow controller to detect network |
| 52 | * infrastructure links. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | public class LLDPLinkProvider extends AbstractProvider implements LinkProvider { |
| 56 | |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected LinkProviderRegistry providerRegistry; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected DeviceService deviceService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected PacketService packetSevice; |
| 67 | |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected MastershipService masterService; |
| 70 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 71 | private LinkProviderService providerService; |
| 72 | |
| 73 | private final boolean useBDDP = true; |
| 74 | |
| 75 | |
| 76 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
| 77 | |
| 78 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 79 | |
| 80 | /** |
| 81 | * Creates an OpenFlow link provider. |
| 82 | */ |
| 83 | public LLDPLinkProvider() { |
| 84 | super(new ProviderId("lldp", "org.onlab.onos.provider.lldp")); |
| 85 | } |
| 86 | |
| 87 | @Activate |
| 88 | public void activate() { |
| 89 | providerService = providerRegistry.register(this); |
| 90 | deviceService.addListener(listener); |
| 91 | packetSevice.addProcessor(listener, 0); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 92 | LinkDiscovery ld; |
| 93 | for (Device device : deviceService.getDevices()) { |
| 94 | ld = new LinkDiscovery(device, packetSevice, masterService, |
| 95 | providerService, useBDDP); |
| 96 | discoverers.put(device.id(), ld); |
| 97 | for (Port p : deviceService.getPorts(device.id())) { |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 98 | if (!p.number().isLogical()) { |
| 99 | ld.addPort(p); |
| 100 | } |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 103 | |
| 104 | log.info("Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | public void deactivate() { |
| 109 | for (LinkDiscovery ld : discoverers.values()) { |
| 110 | ld.stop(); |
| 111 | } |
| 112 | providerRegistry.unregister(this); |
| 113 | deviceService.removeListener(listener); |
| 114 | packetSevice.removeProcessor(listener); |
| 115 | providerService = null; |
| 116 | |
| 117 | log.info("Stopped"); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | private class InternalLinkProvider implements PacketProcessor, DeviceListener { |
| 122 | |
| 123 | @Override |
| 124 | public void event(DeviceEvent event) { |
| 125 | LinkDiscovery ld = null; |
| 126 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 127 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 128 | if (device == null) { |
| 129 | log.error("Device is null."); |
| 130 | return; |
| 131 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 132 | log.trace("{} {} {}", event.type(), event.subject(), event); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 133 | switch (event.type()) { |
| 134 | case DEVICE_ADDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 135 | case DEVICE_UPDATED: |
| 136 | ld = discoverers.get(device.id()); |
| 137 | if (ld == null) { |
| 138 | log.debug("Device added ({}) {}", event.type(), device.id()); |
| 139 | discoverers.put(device.id(), |
| 140 | new LinkDiscovery(device, packetSevice, masterService, |
| 141 | providerService, useBDDP)); |
| 142 | } else { |
| 143 | if (ld.isStopped()) { |
| 144 | log.debug("Device restarted ({}) {}", event.type(), device.id()); |
| 145 | ld.start(); |
| 146 | } |
| 147 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 148 | break; |
| 149 | case PORT_ADDED: |
| 150 | case PORT_UPDATED: |
| 151 | if (event.port().isEnabled()) { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 152 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 153 | if (ld == null) { |
| 154 | return; |
| 155 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 156 | if (!port.number().isLogical()) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 157 | log.debug("Port added {}", port); |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 158 | ld.addPort(port); |
| 159 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 160 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 161 | ConnectPoint point = new ConnectPoint(device.id(), |
| 162 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 163 | providerService.linksVanished(point); |
| 164 | } |
| 165 | break; |
| 166 | case PORT_REMOVED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 167 | log.debug("Port removed {}", port); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 168 | ConnectPoint point = new ConnectPoint(device.id(), |
| 169 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 170 | providerService.linksVanished(point); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 171 | // TODO: Don't we need to removePort from ld? |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 172 | break; |
| 173 | case DEVICE_REMOVED: |
| 174 | case DEVICE_SUSPENDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 175 | log.debug("Device removed {}", device.id()); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 176 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 177 | if (ld == null) { |
| 178 | return; |
| 179 | } |
| 180 | ld.stop(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 181 | providerService.linksVanished(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 182 | break; |
| 183 | case DEVICE_AVAILABILITY_CHANGED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 184 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 185 | if (ld == null) { |
| 186 | return; |
| 187 | } |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 188 | if (deviceService.isAvailable(device.id())) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 189 | log.debug("Device up {}", device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 190 | ld.start(); |
| 191 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 192 | providerService.linksVanished(device.id()); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 193 | log.debug("Device down {}", device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 194 | ld.stop(); |
| 195 | } |
| 196 | break; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 197 | case DEVICE_MASTERSHIP_CHANGED: |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 198 | if (!discoverers.containsKey(device.id())) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 199 | // TODO: ideally, should never reach here |
| 200 | log.debug("Device mastership changed ({}) {}", event.type(), device.id()); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 201 | discoverers.put(device.id(), |
| 202 | new LinkDiscovery(device, packetSevice, masterService, |
| 203 | providerService, useBDDP)); |
| 204 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 205 | break; |
| 206 | default: |
| 207 | log.debug("Unknown event {}", event); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 213 | if (context == null) { |
| 214 | return; |
| 215 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 216 | LinkDiscovery ld = discoverers.get( |
| 217 | context.inPacket().receivedFrom().deviceId()); |
| 218 | if (ld == null) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | if (ld.handleLLDP(context)) { |
| 223 | context.block(); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | } |