Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | |
| 17 | |
| 18 | package org.onosproject.net.edgeservice.impl; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 29 | import org.onosproject.event.AbstractListenerManager; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.device.DeviceEvent; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceListener; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.edge.EdgePortEvent; |
| 36 | import org.onosproject.net.edge.EdgePortListener; |
| 37 | import org.onosproject.net.edge.EdgePortService; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 40 | import org.onosproject.net.link.LinkEvent; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 41 | import org.onosproject.net.link.LinkListener; |
| 42 | import org.onosproject.net.link.LinkService; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 43 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 44 | import org.onosproject.net.packet.OutboundPacket; |
| 45 | import org.onosproject.net.packet.PacketService; |
| 46 | import org.onosproject.net.topology.Topology; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 47 | import org.onosproject.net.topology.TopologyService; |
| 48 | import org.slf4j.Logger; |
| 49 | |
| 50 | import java.nio.ByteBuffer; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 51 | import java.util.Map; |
| 52 | import java.util.Optional; |
| 53 | import java.util.Set; |
| 54 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 55 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 56 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED; |
| 57 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED; |
| 58 | import static org.slf4j.LoggerFactory.getLogger; |
| 59 | |
| 60 | /** |
| 61 | * This is an implementation of the edge net service. |
| 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 65 | public class EdgeManager |
| 66 | extends AbstractListenerManager<EdgePortEvent, EdgePortListener> |
| 67 | implements EdgePortService { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 68 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
| 71 | private Topology topology; |
| 72 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 73 | private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap(); |
| 74 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 75 | private final LinkListener linkListener = new InnerLinkListener(); |
| 76 | |
| 77 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 78 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 80 | protected PacketService packetService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected DeviceService deviceService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected TopologyService topologyService; |
| 87 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected LinkService linkService; |
| 90 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 91 | @Activate |
| 92 | public void activate() { |
| 93 | eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 94 | deviceService.addListener(deviceListener); |
| 95 | linkService.addListener(linkListener); |
| 96 | loadAllEdgePorts(); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
| 102 | eventDispatcher.removeSink(EdgePortEvent.class); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 103 | deviceService.removeListener(deviceListener); |
| 104 | linkService.removeListener(linkListener); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 108 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 109 | public boolean isEdgePoint(ConnectPoint point) { |
| 110 | return !topologyService.isInfrastructure(topologyService.currentTopology(), point); |
| 111 | } |
| 112 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 113 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 114 | public Iterable<ConnectPoint> getEdgePoints() { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 115 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 116 | connectionPoints.forEach((k, v) -> v.forEach(builder::add)); |
| 117 | return builder.build(); |
| 118 | } |
| 119 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 120 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 121 | public Iterable<ConnectPoint> getEdgePoints(DeviceId deviceId) { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 122 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 123 | Set<ConnectPoint> set = connectionPoints.get(deviceId); |
| 124 | if (set != null) { |
| 125 | set.forEach(builder::add); |
| 126 | } |
| 127 | return builder.build(); |
| 128 | } |
| 129 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 130 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 131 | public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) { |
| 132 | TrafficTreatment.Builder builder = treatment.isPresent() ? |
| 133 | DefaultTrafficTreatment.builder(treatment.get()) : |
| 134 | DefaultTrafficTreatment.builder(); |
| 135 | getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data))); |
| 136 | } |
| 137 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 138 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 139 | public void emitPacket(DeviceId deviceId, ByteBuffer data, |
| 140 | Optional<TrafficTreatment> treatment) { |
| 141 | TrafficTreatment.Builder builder = treatment.isPresent() ? |
| 142 | DefaultTrafficTreatment.builder(treatment.get()) : |
| 143 | DefaultTrafficTreatment.builder(); |
| 144 | getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data))); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private OutboundPacket packet(TrafficTreatment.Builder builder, ConnectPoint point, ByteBuffer data) { |
| 148 | builder.setOutput(point.port()); |
| 149 | return new DefaultOutboundPacket(point.deviceId(), builder.build(), data); |
| 150 | } |
| 151 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 152 | private class InnerLinkListener implements LinkListener { |
| 153 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 154 | @Override |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 155 | public void event(LinkEvent event) { |
| 156 | topology = topologyService.currentTopology(); |
| 157 | processLinkEvent(event); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private class InnerDeviceListener implements DeviceListener { |
| 162 | |
| 163 | @Override |
| 164 | public void event(DeviceEvent event) { |
| 165 | topology = topologyService.currentTopology(); |
| 166 | processDeviceEvent(event); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 170 | // Initial loading of the edge port cache. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 171 | private void loadAllEdgePorts() { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 172 | topology = topologyService.currentTopology(); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 173 | deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id()) |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 174 | .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number())))); |
| 175 | } |
| 176 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 177 | // Processes a link event by adding or removing its end-points in our cache. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 178 | private void processLinkEvent(LinkEvent event) { |
| 179 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 180 | removeEdgePort(event.subject().src()); |
| 181 | removeEdgePort(event.subject().dst()); |
| 182 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 183 | addEdgePort(event.subject().src()); |
| 184 | addEdgePort(event.subject().dst()); |
| 185 | } |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 188 | // Processes a device event by adding or removing its end-points in our cache. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 189 | private void processDeviceEvent(DeviceEvent event) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 190 | //FIXME handle the case where a device is suspended, this may or may not come up |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 191 | DeviceEvent.Type type = event.type(); |
| 192 | DeviceId id = event.subject().id(); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 193 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 194 | if (type == DEVICE_ADDED || |
| 195 | type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) { |
| 196 | // When device is added or becomes available, add all its ports |
| 197 | deviceService.getPorts(event.subject().id()) |
| 198 | .forEach(p -> addEdgePort(new ConnectPoint(id, p.number()))); |
| 199 | } else if (type == DEVICE_REMOVED || |
| 200 | type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) { |
| 201 | // When device is removed or becomes unavailable, remove all its ports |
| 202 | deviceService.getPorts(event.subject().id()) |
| 203 | .forEach(p -> removeEdgePort(new ConnectPoint(id, p.number()))); |
| 204 | connectionPoints.remove(id); |
| 205 | |
| 206 | } else if (type == DeviceEvent.Type.PORT_ADDED || |
| 207 | type == PORT_UPDATED && event.port().isEnabled()) { |
| 208 | addEdgePort(new ConnectPoint(id, event.port().number())); |
| 209 | } else if (type == DeviceEvent.Type.PORT_REMOVED || |
| 210 | type == PORT_UPDATED && !event.port().isEnabled()) { |
| 211 | removeEdgePort(new ConnectPoint(id, event.port().number())); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 215 | // Adds the specified connection point to the edge points if needed. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 216 | private void addEdgePort(ConnectPoint point) { |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 217 | if (!topologyService.isInfrastructure(topology, point) && !point.port().isLogical()) { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 218 | Set<ConnectPoint> set = connectionPoints.get(point.deviceId()); |
| 219 | if (set == null) { |
| 220 | set = Sets.newConcurrentHashSet(); |
| 221 | connectionPoints.put(point.deviceId(), set); |
| 222 | } |
| 223 | if (set.add(point)) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 224 | post(new EdgePortEvent(EDGE_PORT_ADDED, point)); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 229 | // Removes the specified connection point from the edge points. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 230 | private void removeEdgePort(ConnectPoint point) { |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 231 | if (!point.port().isLogical()) { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 232 | Set<ConnectPoint> set = connectionPoints.get(point.deviceId()); |
| 233 | if (set == null) { |
| 234 | return; |
| 235 | } |
| 236 | if (set.remove(point)) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 237 | post(new EdgePortEvent(EDGE_PORT_REMOVED, point)); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 238 | } |
| 239 | if (set.isEmpty()) { |
| 240 | connectionPoints.remove(point.deviceId()); |
| 241 | } |
| 242 | } |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 243 | } |
| 244 | } |