blob: 9411a6388a378a855b1888b410fe68467851c919 [file] [log] [blame]
Aaron Kruglikova2b59152015-06-24 14:01:41 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070023import org.onosproject.event.AbstractListenerManager;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070026import org.onosproject.net.Link.Type;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070027import org.onosproject.net.device.DeviceEvent;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070028import org.onosproject.net.device.DeviceListener;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070029import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.edge.EdgePortEvent;
31import org.onosproject.net.edge.EdgePortListener;
32import org.onosproject.net.edge.EdgePortService;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.link.LinkEvent;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070036import org.onosproject.net.link.LinkListener;
37import org.onosproject.net.link.LinkService;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070038import org.onosproject.net.packet.DefaultOutboundPacket;
39import org.onosproject.net.packet.OutboundPacket;
40import org.onosproject.net.packet.PacketService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import org.osgi.service.component.annotations.Activate;
42import org.osgi.service.component.annotations.Component;
43import org.osgi.service.component.annotations.Deactivate;
44import org.osgi.service.component.annotations.Reference;
45import org.osgi.service.component.annotations.ReferenceCardinality;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070046import org.slf4j.Logger;
47
48import java.nio.ByteBuffer;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070049import java.util.Map;
50import java.util.Optional;
51import java.util.Set;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070052
53import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
54import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
55import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
56import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
57import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070058import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
59import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
Heedo Kang4a47a302016-02-29 17:40:23 +090060import static org.onosproject.security.AppGuard.checkPermission;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070061import static org.onosproject.security.AppPermission.Type.PACKET_WRITE;
62import static org.onosproject.security.AppPermission.Type.TOPOLOGY_READ;
63import static org.slf4j.LoggerFactory.getLogger;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070064
65/**
66 * This is an implementation of the edge net service.
67 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068@Component(immediate = true, service = EdgePortService.class)
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070069public class EdgeManager
70 extends AbstractListenerManager<EdgePortEvent, EdgePortListener>
71 implements EdgePortService {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070072
Aaron Kruglikova2b59152015-06-24 14:01:41 -070073 private final Logger log = getLogger(getClass());
74
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070075 // Set of edge ConnectPoints per Device.
Aaron Kruglikova2b59152015-06-24 14:01:41 -070076 private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap();
77
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070078 private final DeviceListener deviceListener = new InnerDeviceListener();
79 private final LinkListener linkListener = new InnerLinkListener();
Thomas Vachuska85021922015-06-29 13:29:42 -070080
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070082 protected PacketService packetService;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070085 protected DeviceService deviceService;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070088 protected LinkService linkService;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070089
90 @Activate
91 public void activate() {
92 eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070093 deviceService.addListener(deviceListener);
94 linkService.addListener(linkListener);
alshabib8a4a6002015-11-25 14:31:16 -080095 loadAllEdgePorts();
Aaron Kruglikova2b59152015-06-24 14:01:41 -070096 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700101 deviceService.removeListener(deviceListener);
102 linkService.removeListener(linkListener);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700103 eventDispatcher.removeSink(EdgePortEvent.class);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700104 log.info("Stopped");
105 }
106
Thomas Vachuska85021922015-06-29 13:29:42 -0700107 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700108 public boolean isEdgePoint(ConnectPoint point) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900109 checkPermission(TOPOLOGY_READ);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700110 Set<ConnectPoint> connectPoints = connectionPoints.get(point.deviceId());
111 return connectPoints != null && connectPoints.contains(point);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700112 }
113
Thomas Vachuska85021922015-06-29 13:29:42 -0700114 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700115 public Iterable<ConnectPoint> getEdgePoints() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900116 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700117 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
118 connectionPoints.forEach((k, v) -> v.forEach(builder::add));
119 return builder.build();
120 }
121
Thomas Vachuska85021922015-06-29 13:29:42 -0700122 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700123 public Iterable<ConnectPoint> getEdgePoints(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900124 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700125 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
126 Set<ConnectPoint> set = connectionPoints.get(deviceId);
127 if (set != null) {
128 set.forEach(builder::add);
129 }
130 return builder.build();
131 }
132
Thomas Vachuska85021922015-06-29 13:29:42 -0700133 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700134 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900135 checkPermission(PACKET_WRITE);
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800136 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
137 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700138 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
139 }
140
Thomas Vachuska85021922015-06-29 13:29:42 -0700141 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700142 public void emitPacket(DeviceId deviceId, ByteBuffer data,
143 Optional<TrafficTreatment> treatment) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800144 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
145 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700146 getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700147 }
148
149 private OutboundPacket packet(TrafficTreatment.Builder builder, ConnectPoint point, ByteBuffer data) {
150 builder.setOutput(point.port());
151 return new DefaultOutboundPacket(point.deviceId(), builder.build(), data);
152 }
153
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700154 private class InnerLinkListener implements LinkListener {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700155 @Override
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700156 public void event(LinkEvent event) {
157 processLinkEvent(event);
158 }
159 }
160
161 private class InnerDeviceListener implements DeviceListener {
162 @Override
163 public void event(DeviceEvent event) {
164 if (event.type() == PORT_STATS_UPDATED) {
165 return;
166 }
167 processDeviceEvent(event);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700168 }
169 }
170
Thomas Vachuska85021922015-06-29 13:29:42 -0700171 // Initial loading of the edge port cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700172 private void loadAllEdgePorts() {
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700173 deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id())
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700174 .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number()))));
175 }
176
Thomas Vachuska85021922015-06-29 13:29:42 -0700177 // Processes a link event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700178 private void processLinkEvent(LinkEvent event) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700179 // negative Link event can result in increase of edge ports
180 boolean addEdgePort = event.type() == LinkEvent.Type.LINK_REMOVED;
181
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700182 // but if the Link is an Edge type, it will be the opposite
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700183 if (event.subject().type() == Type.EDGE) {
184 addEdgePort = !addEdgePort;
185 }
186
187 if (addEdgePort) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700188 addEdgePort(event.subject().src());
189 addEdgePort(event.subject().dst());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700190 } else {
191 removeEdgePort(event.subject().src());
192 removeEdgePort(event.subject().dst());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700193 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700194 }
195
Thomas Vachuska85021922015-06-29 13:29:42 -0700196 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700197 private void processDeviceEvent(DeviceEvent event) {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700198 //FIXME handle the case where a device is suspended, this may or may not come up
Thomas Vachuska85021922015-06-29 13:29:42 -0700199 DeviceEvent.Type type = event.type();
200 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700201
Thomas Vachuska85021922015-06-29 13:29:42 -0700202 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 HIGUCHIb440ef42016-07-15 09:09:09 -0700209 // 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 Vachuska85021922015-06-29 13:29:42 -0700214
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 Kruglikova2b59152015-06-24 14:01:41 -0700221 }
222 }
223
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700224 private boolean isEdgePort(ConnectPoint point) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700225 // Logical ports are not counted as edge ports nor are infrastructure
226 // ports. Ports that have only edge links are considered edge ports.
227 return !point.port().isLogical() &&
Yuta HIGUCHI37dca332016-11-14 18:43:22 -0800228 deviceService.getPort(point) != null &&
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700229 linkService.getLinks(point).stream()
230 .allMatch(link -> link.type() == Type.EDGE);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700231 }
232
Thomas Vachuska85021922015-06-29 13:29:42 -0700233 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700234 private void addEdgePort(ConnectPoint point) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700235 if (isEdgePort(point)) {
236 Set<ConnectPoint> set = connectionPoints.computeIfAbsent(point.deviceId(),
237 (k) -> Sets.newConcurrentHashSet());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700238 if (set.add(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700239 post(new EdgePortEvent(EDGE_PORT_ADDED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700240 }
241 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700242 }
243
Thomas Vachuska85021922015-06-29 13:29:42 -0700244 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700245 private void removeEdgePort(ConnectPoint point) {
Yuta HIGUCHI0b5b82e2016-07-21 07:54:19 -0700246 // trying to remove edge ports, so we shouldn't check if it's EdgePoint
247 if (!point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700248 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
249 if (set == null) {
250 return;
251 }
252 if (set.remove(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700253 post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700254 }
255 if (set.isEmpty()) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700256 connectionPoints.computeIfPresent(point.deviceId(), (k, v) -> {
257 if (v.isEmpty()) {
Yuta HIGUCHI0b5b82e2016-07-21 07:54:19 -0700258 return null;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700259 } else {
260 return v;
261 }
262 });
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700263 }
264 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700265 }
266}