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