Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 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; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 32 | import org.onosproject.net.Link.Type; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 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; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 45 | import org.onosproject.net.topology.TopologyEvent; |
| 46 | import org.onosproject.net.topology.TopologyListener; |
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; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 59 | import static org.onosproject.security.AppGuard.checkPermission; |
| 60 | import static org.onosproject.security.AppPermission.Type.*; |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * This is an implementation of the edge net service. |
| 64 | */ |
| 65 | @Component(immediate = true) |
| 66 | @Service |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 67 | public class EdgeManager |
| 68 | extends AbstractListenerManager<EdgePortEvent, EdgePortListener> |
| 69 | implements EdgePortService { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 70 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 71 | private final Logger log = getLogger(getClass()); |
| 72 | |
| 73 | private Topology topology; |
| 74 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 75 | /** |
| 76 | * Set of edge ConnectPoints per Device. |
| 77 | */ |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 78 | private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap(); |
| 79 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 80 | private final TopologyListener topologyListener = new InnerTopologyListener(); |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 81 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 83 | protected PacketService packetService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected DeviceService deviceService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected TopologyService topologyService; |
| 90 | |
| 91 | @Activate |
| 92 | public void activate() { |
| 93 | eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 94 | topologyService.addListener(topologyListener); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 95 | loadAllEdgePorts(); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 96 | log.info("Started"); |
| 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 101 | topologyService.removeListener(topologyListener); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 102 | eventDispatcher.removeSink(EdgePortEvent.class); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 103 | log.info("Stopped"); |
| 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 boolean isEdgePoint(ConnectPoint point) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 108 | checkPermission(TOPOLOGY_READ); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 109 | return !topologyService.isInfrastructure(topologyService.currentTopology(), point); |
| 110 | } |
| 111 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 112 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 113 | public Iterable<ConnectPoint> getEdgePoints() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 114 | checkPermission(TOPOLOGY_READ); |
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) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 122 | checkPermission(TOPOLOGY_READ); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 123 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 124 | Set<ConnectPoint> set = connectionPoints.get(deviceId); |
| 125 | if (set != null) { |
| 126 | set.forEach(builder::add); |
| 127 | } |
| 128 | return builder.build(); |
| 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(ByteBuffer data, Optional<TrafficTreatment> treatment) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 133 | checkPermission(PACKET_WRITE); |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 134 | TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder) |
| 135 | .orElse(DefaultTrafficTreatment.builder()); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 136 | getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data))); |
| 137 | } |
| 138 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 139 | @Override |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 140 | public void emitPacket(DeviceId deviceId, ByteBuffer data, |
| 141 | Optional<TrafficTreatment> treatment) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 142 | TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder) |
| 143 | .orElse(DefaultTrafficTreatment.builder()); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 152 | private class InnerTopologyListener implements TopologyListener { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 153 | |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 154 | @Override |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 155 | public void event(TopologyEvent event) { |
| 156 | topology = event.subject(); |
| 157 | event.reasons().forEach(reason -> { |
| 158 | if (reason instanceof DeviceEvent) { |
| 159 | processDeviceEvent((DeviceEvent) reason); |
| 160 | } else if (reason instanceof LinkEvent) { |
| 161 | processLinkEvent((LinkEvent) reason); |
| 162 | } |
| 163 | }); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 167 | // Initial loading of the edge port cache. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 168 | private void loadAllEdgePorts() { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 169 | topology = topologyService.currentTopology(); |
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) { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 176 | // negative Link event can result in increase of edge ports |
| 177 | boolean addEdgePort = event.type() == LinkEvent.Type.LINK_REMOVED; |
| 178 | |
| 179 | // but if the Link is an Edge type, |
| 180 | // it will be the opposite |
| 181 | if (event.subject().type() == Type.EDGE) { |
| 182 | addEdgePort = !addEdgePort; |
| 183 | } |
| 184 | |
| 185 | if (addEdgePort) { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 186 | addEdgePort(event.subject().src()); |
| 187 | addEdgePort(event.subject().dst()); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 188 | } else { |
| 189 | removeEdgePort(event.subject().src()); |
| 190 | removeEdgePort(event.subject().dst()); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 191 | } |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 194 | // 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] | 195 | private void processDeviceEvent(DeviceEvent event) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 196 | //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] | 197 | DeviceEvent.Type type = event.type(); |
| 198 | DeviceId id = event.subject().id(); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 199 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 200 | // FIXME there's still chance that Topology and Device Service |
| 201 | // view is out-of-sync |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 202 | if (type == DEVICE_ADDED || |
| 203 | type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) { |
| 204 | // When device is added or becomes available, add all its ports |
| 205 | deviceService.getPorts(event.subject().id()) |
| 206 | .forEach(p -> addEdgePort(new ConnectPoint(id, p.number()))); |
| 207 | } else if (type == DEVICE_REMOVED || |
| 208 | type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 209 | // When device is removed or becomes unavailable, remove all its ports. |
| 210 | // Note: cannot rely on Device subsystem, ports may be gone. |
| 211 | Optional.ofNullable(connectionPoints.remove(id)) |
| 212 | .orElse(ImmutableSet.of()) |
| 213 | .forEach(point -> post(new EdgePortEvent(EDGE_PORT_REMOVED, point))); |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 214 | |
| 215 | } else if (type == DeviceEvent.Type.PORT_ADDED || |
| 216 | type == PORT_UPDATED && event.port().isEnabled()) { |
| 217 | addEdgePort(new ConnectPoint(id, event.port().number())); |
| 218 | } else if (type == DeviceEvent.Type.PORT_REMOVED || |
| 219 | type == PORT_UPDATED && !event.port().isEnabled()) { |
| 220 | removeEdgePort(new ConnectPoint(id, event.port().number())); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 224 | private boolean isEdgePort(ConnectPoint point) { |
| 225 | return !topologyService.isInfrastructure(topology, point) && |
| 226 | !point.port().isLogical(); |
| 227 | } |
| 228 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 229 | // Adds the specified connection point to the edge points if needed. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 230 | private void addEdgePort(ConnectPoint point) { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 231 | if (isEdgePort(point)) { |
| 232 | Set<ConnectPoint> set = connectionPoints.computeIfAbsent(point.deviceId(), |
| 233 | (k) -> Sets.newConcurrentHashSet()); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 234 | if (set.add(point)) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 235 | post(new EdgePortEvent(EDGE_PORT_ADDED, point)); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Thomas Vachuska | 8502192 | 2015-06-29 13:29:42 -0700 | [diff] [blame] | 240 | // Removes the specified connection point from the edge points. |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 241 | private void removeEdgePort(ConnectPoint point) { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 242 | if (isEdgePort(point)) { |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 243 | Set<ConnectPoint> set = connectionPoints.get(point.deviceId()); |
| 244 | if (set == null) { |
| 245 | return; |
| 246 | } |
| 247 | if (set.remove(point)) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 248 | post(new EdgePortEvent(EDGE_PORT_REMOVED, point)); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 249 | } |
| 250 | if (set.isEmpty()) { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame^] | 251 | connectionPoints.computeIfPresent(point.deviceId(), (k, v) -> { |
| 252 | if (v.isEmpty()) { |
| 253 | return v; |
| 254 | } else { |
| 255 | return v; |
| 256 | } |
| 257 | }); |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Aaron Kruglikov | a2b5915 | 2015-06-24 14:01:41 -0700 | [diff] [blame] | 260 | } |
| 261 | } |