blob: cd7335d62e522b729d59f4ca561a8b8859e45deb [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) {
132 TrafficTreatment.Builder builder = treatment.isPresent() ?
133 DefaultTrafficTreatment.builder(treatment.get()) :
134 DefaultTrafficTreatment.builder();
135 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
136 }
137
Thomas Vachuska85021922015-06-29 13:29:42 -0700138 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700139 public void emitPacket(DeviceId deviceId, ByteBuffer data,
140 Optional<TrafficTreatment> treatment) {
141 TrafficTreatment.Builder builder = treatment.isPresent() ?
142 DefaultTrafficTreatment.builder(treatment.get()) :
143 DefaultTrafficTreatment.builder();
144 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
alshabib8a4a6002015-11-25 14:31:16 -0800152 private class InnerLinkListener implements LinkListener {
153
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700154 @Override
alshabib8a4a6002015-11-25 14:31:16 -0800155 public void event(LinkEvent event) {
156 topology = topologyService.currentTopology();
157 processLinkEvent(event);
158 }
159 }
160
161 private class InnerDeviceListener implements DeviceListener {
162
163 @Override
164 public void event(DeviceEvent event) {
165 topology = topologyService.currentTopology();
166 processDeviceEvent(event);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700167 }
168 }
169
Thomas Vachuska85021922015-06-29 13:29:42 -0700170 // Initial loading of the edge port cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700171 private void loadAllEdgePorts() {
alshabib8a4a6002015-11-25 14:31:16 -0800172 topology = topologyService.currentTopology();
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) {
179 if (event.type() == LinkEvent.Type.LINK_ADDED) {
180 removeEdgePort(event.subject().src());
181 removeEdgePort(event.subject().dst());
182 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
183 addEdgePort(event.subject().src());
184 addEdgePort(event.subject().dst());
185 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700186 }
187
Thomas Vachuska85021922015-06-29 13:29:42 -0700188 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700189 private void processDeviceEvent(DeviceEvent event) {
Aaron Kruglikovd8123832015-07-06 14:20:25 -0700190 //FIXME handle the case where a device is suspended, this may or may not come up
Thomas Vachuska85021922015-06-29 13:29:42 -0700191 DeviceEvent.Type type = event.type();
192 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700193
Thomas Vachuska85021922015-06-29 13:29:42 -0700194 if (type == DEVICE_ADDED ||
195 type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) {
196 // When device is added or becomes available, add all its ports
197 deviceService.getPorts(event.subject().id())
198 .forEach(p -> addEdgePort(new ConnectPoint(id, p.number())));
199 } else if (type == DEVICE_REMOVED ||
200 type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) {
201 // When device is removed or becomes unavailable, remove all its ports
202 deviceService.getPorts(event.subject().id())
203 .forEach(p -> removeEdgePort(new ConnectPoint(id, p.number())));
204 connectionPoints.remove(id);
205
206 } else if (type == DeviceEvent.Type.PORT_ADDED ||
207 type == PORT_UPDATED && event.port().isEnabled()) {
208 addEdgePort(new ConnectPoint(id, event.port().number()));
209 } else if (type == DeviceEvent.Type.PORT_REMOVED ||
210 type == PORT_UPDATED && !event.port().isEnabled()) {
211 removeEdgePort(new ConnectPoint(id, event.port().number()));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700212 }
213 }
214
Thomas Vachuska85021922015-06-29 13:29:42 -0700215 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700216 private void addEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700217 if (!topologyService.isInfrastructure(topology, point) && !point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700218 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
219 if (set == null) {
220 set = Sets.newConcurrentHashSet();
221 connectionPoints.put(point.deviceId(), set);
222 }
223 if (set.add(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700224 post(new EdgePortEvent(EDGE_PORT_ADDED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700225 }
226 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700227 }
228
Thomas Vachuska85021922015-06-29 13:29:42 -0700229 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700230 private void removeEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700231 if (!point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700232 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
233 if (set == null) {
234 return;
235 }
236 if (set.remove(point)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700237 post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700238 }
239 if (set.isEmpty()) {
240 connectionPoints.remove(point.deviceId());
241 }
242 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700243 }
244}