blob: ed5fa2c52e2a6bf575e7e75d492f40465f2bfbf9 [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;
34import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.edge.EdgePortEvent;
36import org.onosproject.net.edge.EdgePortListener;
37import org.onosproject.net.edge.EdgePortService;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.link.LinkEvent;
41import org.onosproject.net.packet.DefaultOutboundPacket;
42import org.onosproject.net.packet.OutboundPacket;
43import org.onosproject.net.packet.PacketService;
44import org.onosproject.net.topology.Topology;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070045import org.onosproject.net.topology.TopologyEvent;
46import org.onosproject.net.topology.TopologyListener;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070047import org.onosproject.net.topology.TopologyService;
48import org.slf4j.Logger;
49
50import java.nio.ByteBuffer;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070051import java.util.Map;
52import java.util.Optional;
53import java.util.Set;
54
Thomas Vachuska85021922015-06-29 13:29:42 -070055import static org.onosproject.net.device.DeviceEvent.Type.*;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070056import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
57import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
58import static org.slf4j.LoggerFactory.getLogger;
Heedo Kang4a47a302016-02-29 17:40:23 +090059import static org.onosproject.security.AppGuard.checkPermission;
60import static org.onosproject.security.AppPermission.Type.*;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070061
62/**
63 * This is an implementation of the edge net service.
64 */
65@Component(immediate = true)
66@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070067public class EdgeManager
68 extends AbstractListenerManager<EdgePortEvent, EdgePortListener>
69 implements EdgePortService {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070070
Aaron Kruglikova2b59152015-06-24 14:01:41 -070071 private final Logger log = getLogger(getClass());
72
73 private Topology topology;
74
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070075 /**
76 * Set of edge ConnectPoints per Device.
77 */
Aaron Kruglikova2b59152015-06-24 14:01:41 -070078 private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap();
79
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070080 private final TopologyListener topologyListener = new InnerTopologyListener();
Thomas Vachuska85021922015-06-29 13:29:42 -070081
Aaron Kruglikova2b59152015-06-24 14:01:41 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070083 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 HIGUCHIb440ef42016-07-15 09:09:09 -070094 topologyService.addListener(topologyListener);
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() {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700101 topologyService.removeListener(topologyListener);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700102 eventDispatcher.removeSink(EdgePortEvent.class);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700103 log.info("Stopped");
104 }
105
Thomas Vachuska85021922015-06-29 13:29:42 -0700106 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700107 public boolean isEdgePoint(ConnectPoint point) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900108 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700109 return !topologyService.isInfrastructure(topologyService.currentTopology(), point);
110 }
111
Thomas Vachuska85021922015-06-29 13:29:42 -0700112 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700113 public Iterable<ConnectPoint> getEdgePoints() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900114 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700115 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
116 connectionPoints.forEach((k, v) -> v.forEach(builder::add));
117 return builder.build();
118 }
119
Thomas Vachuska85021922015-06-29 13:29:42 -0700120 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700121 public Iterable<ConnectPoint> getEdgePoints(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900122 checkPermission(TOPOLOGY_READ);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700123 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 Vachuska85021922015-06-29 13:29:42 -0700131 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700132 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900133 checkPermission(PACKET_WRITE);
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800134 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
135 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700136 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
137 }
138
Thomas Vachuska85021922015-06-29 13:29:42 -0700139 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700140 public void emitPacket(DeviceId deviceId, ByteBuffer data,
141 Optional<TrafficTreatment> treatment) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800142 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
143 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700144 getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700145 }
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 HIGUCHIb440ef42016-07-15 09:09:09 -0700152 private class InnerTopologyListener implements TopologyListener {
alshabib8a4a6002015-11-25 14:31:16 -0800153
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700154 @Override
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700155 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 Kruglikova2b59152015-06-24 14:01:41 -0700164 }
165 }
166
Thomas Vachuska85021922015-06-29 13:29:42 -0700167 // Initial loading of the edge port cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700168 private void loadAllEdgePorts() {
alshabib8a4a6002015-11-25 14:31:16 -0800169 topology = topologyService.currentTopology();
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700170 deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id())
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700171 .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number()))));
172 }
173
Thomas Vachuska85021922015-06-29 13:29:42 -0700174 // Processes a link event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700175 private void processLinkEvent(LinkEvent event) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700176 // 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 Kruglikova2b59152015-06-24 14:01:41 -0700186 addEdgePort(event.subject().src());
187 addEdgePort(event.subject().dst());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700188 } else {
189 removeEdgePort(event.subject().src());
190 removeEdgePort(event.subject().dst());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700191 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700192 }
193
Thomas Vachuska85021922015-06-29 13:29:42 -0700194 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700195 private void processDeviceEvent(DeviceEvent event) {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700196 //FIXME handle the case where a device is suspended, this may or may not come up
Thomas Vachuska85021922015-06-29 13:29:42 -0700197 DeviceEvent.Type type = event.type();
198 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700199
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700200 // FIXME there's still chance that Topology and Device Service
201 // view is out-of-sync
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) {
225 return !topologyService.isInfrastructure(topology, point) &&
226 !point.port().isLogical();
227 }
228
Thomas Vachuska85021922015-06-29 13:29:42 -0700229 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700230 private void addEdgePort(ConnectPoint point) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700231 if (isEdgePort(point)) {
232 Set<ConnectPoint> set = connectionPoints.computeIfAbsent(point.deviceId(),
233 (k) -> Sets.newConcurrentHashSet());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700234 if (set.add(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700235 post(new EdgePortEvent(EDGE_PORT_ADDED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700236 }
237 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700238 }
239
Thomas Vachuska85021922015-06-29 13:29:42 -0700240 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700241 private void removeEdgePort(ConnectPoint point) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700242 if (isEdgePort(point)) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700243 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
244 if (set == null) {
245 return;
246 }
247 if (set.remove(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700248 post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700249 }
250 if (set.isEmpty()) {
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700251 connectionPoints.computeIfPresent(point.deviceId(), (k, v) -> {
252 if (v.isEmpty()) {
253 return v;
254 } else {
255 return v;
256 }
257 });
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700258 }
259 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700260 }
261}