blob: 68a58c2af5d3a216df7055c27dcdf93a5641b766 [file] [log] [blame]
Aaron Kruglikova2b59152015-06-24 14:01:41 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Aaron Kruglikova2b59152015-06-24 14:01:41 -07003 *
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
18package org.onosproject.net.edgeservice.impl;
19
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Sets;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070029import org.onosproject.event.AbstractListenerManager;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070032import org.onosproject.net.Link.Type;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070033import org.onosproject.net.device.DeviceEvent;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070034import org.onosproject.net.device.DeviceListener;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070035import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.edge.EdgePortEvent;
37import org.onosproject.net.edge.EdgePortListener;
38import org.onosproject.net.edge.EdgePortService;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.link.LinkEvent;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070042import org.onosproject.net.link.LinkListener;
43import org.onosproject.net.link.LinkService;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070044import org.onosproject.net.packet.DefaultOutboundPacket;
45import org.onosproject.net.packet.OutboundPacket;
46import org.onosproject.net.packet.PacketService;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070047import org.slf4j.Logger;
48
49import java.nio.ByteBuffer;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070050import java.util.Map;
51import java.util.Optional;
52import java.util.Set;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070053
54import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
55import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
56import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
57import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
58import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070059import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
60import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
Heedo Kang4a47a302016-02-29 17:40:23 +090061import static org.onosproject.security.AppGuard.checkPermission;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070062import static org.onosproject.security.AppPermission.Type.PACKET_WRITE;
63import static org.onosproject.security.AppPermission.Type.TOPOLOGY_READ;
64import static org.slf4j.LoggerFactory.getLogger;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070065
66/**
67 * This is an implementation of the edge net service.
68 */
69@Component(immediate = true)
70@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070071public class EdgeManager
72 extends AbstractListenerManager<EdgePortEvent, EdgePortListener>
73 implements EdgePortService {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070074
Aaron Kruglikova2b59152015-06-24 14:01:41 -070075 private final Logger log = getLogger(getClass());
76
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070077 // Set of edge ConnectPoints per Device.
Aaron Kruglikova2b59152015-06-24 14:01:41 -070078 private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap();
79
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070080 private final DeviceListener deviceListener = new InnerDeviceListener();
81 private final LinkListener linkListener = new InnerLinkListener();
Thomas Vachuska85021922015-06-29 13:29:42 -070082
Aaron Kruglikova2b59152015-06-24 14:01:41 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070084 protected PacketService packetService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected DeviceService deviceService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070090 protected LinkService linkService;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070091
92 @Activate
93 public void activate() {
94 eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070095 deviceService.addListener(deviceListener);
96 linkService.addListener(linkListener);
alshabib8a4a6002015-11-25 14:31:16 -080097 loadAllEdgePorts();
Aaron Kruglikova2b59152015-06-24 14:01:41 -070098 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700103 deviceService.removeListener(deviceListener);
104 linkService.removeListener(linkListener);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700105 eventDispatcher.removeSink(EdgePortEvent.class);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700106 log.info("Stopped");
107 }
108
Thomas Vachuska85021922015-06-29 13:29:42 -0700109 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700110 public boolean isEdgePoint(ConnectPoint point) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900111 checkPermission(TOPOLOGY_READ);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700112 Set<ConnectPoint> connectPoints = connectionPoints.get(point.deviceId());
113 return connectPoints != null && connectPoints.contains(point);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700114 }
115
Thomas Vachuska85021922015-06-29 13:29:42 -0700116 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700117 public Iterable<ConnectPoint> getEdgePoints() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900118 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700119 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
120 connectionPoints.forEach((k, v) -> v.forEach(builder::add));
121 return builder.build();
122 }
123
Thomas Vachuska85021922015-06-29 13:29:42 -0700124 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700125 public Iterable<ConnectPoint> getEdgePoints(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900126 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700127 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
128 Set<ConnectPoint> set = connectionPoints.get(deviceId);
129 if (set != null) {
130 set.forEach(builder::add);
131 }
132 return builder.build();
133 }
134
Thomas Vachuska85021922015-06-29 13:29:42 -0700135 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700136 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900137 checkPermission(PACKET_WRITE);
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800138 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
139 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700140 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
141 }
142
Thomas Vachuska85021922015-06-29 13:29:42 -0700143 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700144 public void emitPacket(DeviceId deviceId, ByteBuffer data,
145 Optional<TrafficTreatment> treatment) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800146 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
147 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700148 getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700149 }
150
151 private OutboundPacket packet(TrafficTreatment.Builder builder, ConnectPoint point, ByteBuffer data) {
152 builder.setOutput(point.port());
153 return new DefaultOutboundPacket(point.deviceId(), builder.build(), data);
154 }
155
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700156 private class InnerLinkListener implements LinkListener {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700157 @Override
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700158 public void event(LinkEvent event) {
159 processLinkEvent(event);
160 }
161 }
162
163 private class InnerDeviceListener implements DeviceListener {
164 @Override
165 public void event(DeviceEvent event) {
166 if (event.type() == PORT_STATS_UPDATED) {
167 return;
168 }
169 processDeviceEvent(event);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700170 }
171 }
172
Thomas Vachuska85021922015-06-29 13:29:42 -0700173 // Initial loading of the edge port cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700174 private void loadAllEdgePorts() {
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700175 deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id())
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700176 .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number()))));
177 }
178
Thomas Vachuska85021922015-06-29 13:29:42 -0700179 // Processes a link event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700180 private void processLinkEvent(LinkEvent event) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700181 // negative Link event can result in increase of edge ports
182 boolean addEdgePort = event.type() == LinkEvent.Type.LINK_REMOVED;
183
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700184 // but if the Link is an Edge type, it will be the opposite
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700185 if (event.subject().type() == Type.EDGE) {
186 addEdgePort = !addEdgePort;
187 }
188
189 if (addEdgePort) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700190 addEdgePort(event.subject().src());
191 addEdgePort(event.subject().dst());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700192 } else {
193 removeEdgePort(event.subject().src());
194 removeEdgePort(event.subject().dst());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700195 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700196 }
197
Thomas Vachuska85021922015-06-29 13:29:42 -0700198 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700199 private void processDeviceEvent(DeviceEvent event) {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700200 //FIXME handle the case where a device is suspended, this may or may not come up
Thomas Vachuska85021922015-06-29 13:29:42 -0700201 DeviceEvent.Type type = event.type();
202 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700203
Thomas Vachuska85021922015-06-29 13:29:42 -0700204 if (type == DEVICE_ADDED ||
205 type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) {
206 // When device is added or becomes available, add all its ports
207 deviceService.getPorts(event.subject().id())
208 .forEach(p -> addEdgePort(new ConnectPoint(id, p.number())));
209 } else if (type == DEVICE_REMOVED ||
210 type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700211 // When device is removed or becomes unavailable, remove all its ports.
212 // Note: cannot rely on Device subsystem, ports may be gone.
213 Optional.ofNullable(connectionPoints.remove(id))
214 .orElse(ImmutableSet.of())
215 .forEach(point -> post(new EdgePortEvent(EDGE_PORT_REMOVED, point)));
Thomas Vachuska85021922015-06-29 13:29:42 -0700216
217 } else if (type == DeviceEvent.Type.PORT_ADDED ||
218 type == PORT_UPDATED && event.port().isEnabled()) {
219 addEdgePort(new ConnectPoint(id, event.port().number()));
220 } else if (type == DeviceEvent.Type.PORT_REMOVED ||
221 type == PORT_UPDATED && !event.port().isEnabled()) {
222 removeEdgePort(new ConnectPoint(id, event.port().number()));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700223 }
224 }
225
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700226 private boolean isEdgePort(ConnectPoint point) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700227 // Logical ports are not counted as edge ports nor are infrastructure
228 // ports. Ports that have only edge links are considered edge ports.
229 return !point.port().isLogical() &&
230 linkService.getLinks(point).stream()
231 .allMatch(link -> link.type() == Type.EDGE);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700232 }
233
Thomas Vachuska85021922015-06-29 13:29:42 -0700234 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700235 private void addEdgePort(ConnectPoint point) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700236 if (isEdgePort(point)) {
237 Set<ConnectPoint> set = connectionPoints.computeIfAbsent(point.deviceId(),
238 (k) -> Sets.newConcurrentHashSet());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700239 if (set.add(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700240 post(new EdgePortEvent(EDGE_PORT_ADDED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700241 }
242 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700243 }
244
Thomas Vachuska85021922015-06-29 13:29:42 -0700245 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700246 private void removeEdgePort(ConnectPoint point) {
Yuta HIGUCHI0b5b82e2016-07-21 07:54:19 -0700247 // trying to remove edge ports, so we shouldn't check if it's EdgePoint
248 if (!point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700249 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
250 if (set == null) {
251 return;
252 }
253 if (set.remove(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700254 post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700255 }
256 if (set.isEmpty()) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700257 connectionPoints.computeIfPresent(point.deviceId(), (k, v) -> {
258 if (v.isEmpty()) {
Yuta HIGUCHI0b5b82e2016-07-21 07:54:19 -0700259 return null;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700260 } else {
261 return v;
262 }
263 });
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700264 }
265 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700266 }
267}