blob: e992f7a4079599427acaacce2d2ef29e3148031f [file] [log] [blame]
Aaron Kruglikova2b59152015-06-24 14:01:41 -07001/*
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
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.event.Event;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070031import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DeviceId;
33import 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;
45import org.onosproject.net.topology.TopologyEvent;
46import org.onosproject.net.topology.TopologyListener;
47import org.onosproject.net.topology.TopologyService;
48import org.slf4j.Logger;
49
50import java.nio.ByteBuffer;
51import java.util.List;
52import java.util.Map;
53import java.util.Optional;
54import java.util.Set;
55
Thomas Vachuska85021922015-06-29 13:29:42 -070056import static org.onosproject.net.device.DeviceEvent.Type.*;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070057import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
58import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
59import 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 Vachuska42e8cce2015-07-29 19:25:18 -070066public class EdgeManager
67 extends AbstractListenerManager<EdgePortEvent, EdgePortListener>
68 implements EdgePortService {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070069
Aaron Kruglikova2b59152015-06-24 14:01:41 -070070 private final Logger log = getLogger(getClass());
71
72 private Topology topology;
73
Aaron Kruglikova2b59152015-06-24 14:01:41 -070074 private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap();
75
Thomas Vachuska85021922015-06-29 13:29:42 -070076 private final TopologyListener topologyListener = new InnerTopologyListener();
77
Aaron Kruglikova2b59152015-06-24 14:01:41 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070079 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 Vachuska85021922015-06-29 13:29:42 -0700101 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700102 public boolean isEdgePoint(ConnectPoint point) {
103 return !topologyService.isInfrastructure(topologyService.currentTopology(), point);
104 }
105
Thomas Vachuska85021922015-06-29 13:29:42 -0700106 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700107 public Iterable<ConnectPoint> getEdgePoints() {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700108 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
109 connectionPoints.forEach((k, v) -> v.forEach(builder::add));
110 return builder.build();
111 }
112
Thomas Vachuska85021922015-06-29 13:29:42 -0700113 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700114 public Iterable<ConnectPoint> getEdgePoints(DeviceId deviceId) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700115 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 Vachuska85021922015-06-29 13:29:42 -0700123 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700124 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 Vachuska85021922015-06-29 13:29:42 -0700131 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700132 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 Kruglikova2b59152015-06-24 14:01:41 -0700138 }
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 Vachuska85021922015-06-29 13:29:42 -0700145 // Internal listener for topo events used to keep our edge-port cache
146 // up to date.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700147 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 Kruglikova2b59152015-06-24 14:01:41 -0700155 processDeviceEvent((DeviceEvent) reason);
156 } else if (reason instanceof LinkEvent) {
157 processLinkEvent((LinkEvent) reason);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700158 }
159 });
160 } else {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700161 //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 Kruglikova2b59152015-06-24 14:01:41 -0700163 loadAllEdgePorts();
164 }
165 }
166 }
167
Thomas Vachuska85021922015-06-29 13:29:42 -0700168 // Initial loading of the edge port cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700169 private void loadAllEdgePorts() {
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) {
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 Kruglikova2b59152015-06-24 14:01:41 -0700183 }
184
Thomas Vachuska85021922015-06-29 13:29:42 -0700185 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700186 private void processDeviceEvent(DeviceEvent event) {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700187 //FIXME handle the case where a device is suspended, this may or may not come up
Thomas Vachuska85021922015-06-29 13:29:42 -0700188 DeviceEvent.Type type = event.type();
189 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700190
Thomas Vachuska85021922015-06-29 13:29:42 -0700191 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 Kruglikova2b59152015-06-24 14:01:41 -0700209 }
210 }
211
Thomas Vachuska85021922015-06-29 13:29:42 -0700212 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700213 private void addEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700214 if (!topologyService.isInfrastructure(topology, point) && !point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700215 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 Vachuska42e8cce2015-07-29 19:25:18 -0700221 post(new EdgePortEvent(EDGE_PORT_ADDED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700222 }
223 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700224 }
225
Thomas Vachuska85021922015-06-29 13:29:42 -0700226 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700227 private void removeEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700228 if (!point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700229 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
230 if (set == null) {
231 return;
232 }
233 if (set.remove(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700234 post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700235 }
236 if (set.isEmpty()) {
237 connectionPoints.remove(point.deviceId());
238 }
239 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700240 }
241}