blob: 7340fc5b8be87bd7dcc7ab4045e90e1d27ac68e9 [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.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.device.DeviceEvent;
alshabib8a4a6002015-11-25 14:31:16 -080033import org.onosproject.net.device.DeviceListener;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070034import 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;
alshabib8a4a6002015-11-25 14:31:16 -080041import org.onosproject.net.link.LinkListener;
42import org.onosproject.net.link.LinkService;
Aaron Kruglikova2b59152015-06-24 14:01:41 -070043import org.onosproject.net.packet.DefaultOutboundPacket;
44import org.onosproject.net.packet.OutboundPacket;
45import org.onosproject.net.packet.PacketService;
46import org.onosproject.net.topology.Topology;
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;
59
60/**
61 * This is an implementation of the edge net service.
62 */
63@Component(immediate = true)
64@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070065public class EdgeManager
66 extends AbstractListenerManager<EdgePortEvent, EdgePortListener>
67 implements EdgePortService {
Aaron Kruglikova2b59152015-06-24 14:01:41 -070068
Aaron Kruglikova2b59152015-06-24 14:01:41 -070069 private final Logger log = getLogger(getClass());
70
71 private Topology topology;
72
Aaron Kruglikova2b59152015-06-24 14:01:41 -070073 private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap();
74
alshabib8a4a6002015-11-25 14:31:16 -080075 private final LinkListener linkListener = new InnerLinkListener();
76
77 private final DeviceListener deviceListener = new InnerDeviceListener();
Thomas Vachuska85021922015-06-29 13:29:42 -070078
Aaron Kruglikova2b59152015-06-24 14:01:41 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Aaron Kruglikova2b59152015-06-24 14:01:41 -070080 protected PacketService packetService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected DeviceService deviceService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected TopologyService topologyService;
87
alshabib8a4a6002015-11-25 14:31:16 -080088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected LinkService linkService;
90
Aaron Kruglikova2b59152015-06-24 14:01:41 -070091 @Activate
92 public void activate() {
93 eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry);
alshabib8a4a6002015-11-25 14:31:16 -080094 deviceService.addListener(deviceListener);
95 linkService.addListener(linkListener);
96 loadAllEdgePorts();
Aaron Kruglikova2b59152015-06-24 14:01:41 -070097 log.info("Started");
98 }
99
100 @Deactivate
101 public void deactivate() {
102 eventDispatcher.removeSink(EdgePortEvent.class);
alshabib8a4a6002015-11-25 14:31:16 -0800103 deviceService.removeListener(deviceListener);
104 linkService.removeListener(linkListener);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700105 log.info("Stopped");
106 }
107
Thomas Vachuska85021922015-06-29 13:29:42 -0700108 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700109 public boolean isEdgePoint(ConnectPoint point) {
110 return !topologyService.isInfrastructure(topologyService.currentTopology(), point);
111 }
112
Thomas Vachuska85021922015-06-29 13:29:42 -0700113 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700114 public Iterable<ConnectPoint> getEdgePoints() {
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) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700122 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
123 Set<ConnectPoint> set = connectionPoints.get(deviceId);
124 if (set != null) {
125 set.forEach(builder::add);
126 }
127 return builder.build();
128 }
129
Thomas Vachuska85021922015-06-29 13:29:42 -0700130 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700131 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800132 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
133 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700134 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
135 }
136
Thomas Vachuska85021922015-06-29 13:29:42 -0700137 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700138 public void emitPacket(DeviceId deviceId, ByteBuffer data,
139 Optional<TrafficTreatment> treatment) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800140 TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
141 .orElse(DefaultTrafficTreatment.builder());
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700142 getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700143 }
144
145 private OutboundPacket packet(TrafficTreatment.Builder builder, ConnectPoint point, ByteBuffer data) {
146 builder.setOutput(point.port());
147 return new DefaultOutboundPacket(point.deviceId(), builder.build(), data);
148 }
149
alshabib8a4a6002015-11-25 14:31:16 -0800150 private class InnerLinkListener implements LinkListener {
151
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700152 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800153 public void event(LinkEvent event) {
154 topology = topologyService.currentTopology();
155 processLinkEvent(event);
156 }
157 }
158
159 private class InnerDeviceListener implements DeviceListener {
160
161 @Override
162 public void event(DeviceEvent event) {
163 topology = topologyService.currentTopology();
164 processDeviceEvent(event);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700165 }
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() {
alshabib8a4a6002015-11-25 14:31:16 -0800170 topology = topologyService.currentTopology();
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700171 deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id())
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700172 .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number()))));
173 }
174
Thomas Vachuska85021922015-06-29 13:29:42 -0700175 // Processes a link event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700176 private void processLinkEvent(LinkEvent event) {
177 if (event.type() == LinkEvent.Type.LINK_ADDED) {
178 removeEdgePort(event.subject().src());
179 removeEdgePort(event.subject().dst());
180 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
181 addEdgePort(event.subject().src());
182 addEdgePort(event.subject().dst());
183 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700184 }
185
Thomas Vachuska85021922015-06-29 13:29:42 -0700186 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700187 private void processDeviceEvent(DeviceEvent event) {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700188 //FIXME handle the case where a device is suspended, this may or may not come up
Thomas Vachuska85021922015-06-29 13:29:42 -0700189 DeviceEvent.Type type = event.type();
190 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700191
Thomas Vachuska85021922015-06-29 13:29:42 -0700192 if (type == DEVICE_ADDED ||
193 type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) {
194 // When device is added or becomes available, add all its ports
195 deviceService.getPorts(event.subject().id())
196 .forEach(p -> addEdgePort(new ConnectPoint(id, p.number())));
197 } else if (type == DEVICE_REMOVED ||
198 type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) {
199 // When device is removed or becomes unavailable, remove all its ports
200 deviceService.getPorts(event.subject().id())
201 .forEach(p -> removeEdgePort(new ConnectPoint(id, p.number())));
202 connectionPoints.remove(id);
203
204 } else if (type == DeviceEvent.Type.PORT_ADDED ||
205 type == PORT_UPDATED && event.port().isEnabled()) {
206 addEdgePort(new ConnectPoint(id, event.port().number()));
207 } else if (type == DeviceEvent.Type.PORT_REMOVED ||
208 type == PORT_UPDATED && !event.port().isEnabled()) {
209 removeEdgePort(new ConnectPoint(id, event.port().number()));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700210 }
211 }
212
Thomas Vachuska85021922015-06-29 13:29:42 -0700213 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700214 private void addEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700215 if (!topologyService.isInfrastructure(topology, point) && !point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700216 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
217 if (set == null) {
218 set = Sets.newConcurrentHashSet();
219 connectionPoints.put(point.deviceId(), set);
220 }
221 if (set.add(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700222 post(new EdgePortEvent(EDGE_PORT_ADDED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700223 }
224 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700225 }
226
Thomas Vachuska85021922015-06-29 13:29:42 -0700227 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700228 private void removeEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700229 if (!point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700230 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
231 if (set == null) {
232 return;
233 }
234 if (set.remove(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700235 post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700236 }
237 if (set.isEmpty()) {
238 connectionPoints.remove(point.deviceId());
239 }
240 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700241 }
242}