blob: 85361859b18f3264f7ee37d883679147d771a1c0 [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;
29import org.onosproject.event.Event;
30import org.onosproject.event.EventDeliveryService;
31import org.onosproject.event.ListenerRegistry;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.edge.EdgePortEvent;
37import org.onosproject.net.edge.EdgePortListener;
38import org.onosproject.net.edge.EdgePortService;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.link.LinkEvent;
42import org.onosproject.net.packet.DefaultOutboundPacket;
43import org.onosproject.net.packet.OutboundPacket;
44import org.onosproject.net.packet.PacketService;
45import org.onosproject.net.topology.Topology;
46import org.onosproject.net.topology.TopologyEvent;
47import org.onosproject.net.topology.TopologyListener;
48import org.onosproject.net.topology.TopologyService;
49import org.slf4j.Logger;
50
51import java.nio.ByteBuffer;
52import java.util.List;
53import java.util.Map;
54import java.util.Optional;
55import java.util.Set;
56
Thomas Vachuska85021922015-06-29 13:29:42 -070057import static org.onosproject.net.device.DeviceEvent.Type.*;
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;
60import static org.slf4j.LoggerFactory.getLogger;
61
62/**
63 * This is an implementation of the edge net service.
64 */
65@Component(immediate = true)
66@Service
67public class EdgeManager implements EdgePortService {
68
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
Thomas Vachuska85021922015-06-29 13:29:42 -070075 private final ListenerRegistry<EdgePortEvent, EdgePortListener>
76 listenerRegistry = new ListenerRegistry<>();
77
78 private final TopologyListener topologyListener = new InnerTopologyListener();
79
Aaron Kruglikova2b59152015-06-24 14:01:41 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected EventDeliveryService eventDispatcher;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected PacketService packetService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected DeviceService deviceService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected TopologyService topologyService;
91
92 @Activate
93 public void activate() {
94 eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry);
95 topologyService.addListener(topologyListener);
96 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
101 eventDispatcher.removeSink(EdgePortEvent.class);
102 topologyService.removeListener(topologyListener);
103 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) {
108 return !topologyService.isInfrastructure(topologyService.currentTopology(), point);
109 }
110
Thomas Vachuska85021922015-06-29 13:29:42 -0700111 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700112 public Iterable<ConnectPoint> getEdgePoints() {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700113 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
114 connectionPoints.forEach((k, v) -> v.forEach(builder::add));
115 return builder.build();
116 }
117
Thomas Vachuska85021922015-06-29 13:29:42 -0700118 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700119 public Iterable<ConnectPoint> getEdgePoints(DeviceId deviceId) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700120 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
121 Set<ConnectPoint> set = connectionPoints.get(deviceId);
122 if (set != null) {
123 set.forEach(builder::add);
124 }
125 return builder.build();
126 }
127
Thomas Vachuska85021922015-06-29 13:29:42 -0700128 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700129 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
130 TrafficTreatment.Builder builder = treatment.isPresent() ?
131 DefaultTrafficTreatment.builder(treatment.get()) :
132 DefaultTrafficTreatment.builder();
133 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
134 }
135
Thomas Vachuska85021922015-06-29 13:29:42 -0700136 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700137 public void emitPacket(DeviceId deviceId, ByteBuffer data,
138 Optional<TrafficTreatment> treatment) {
139 TrafficTreatment.Builder builder = treatment.isPresent() ?
140 DefaultTrafficTreatment.builder(treatment.get()) :
141 DefaultTrafficTreatment.builder();
142 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
Thomas Vachuska85021922015-06-29 13:29:42 -0700150 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700151 public void addListener(EdgePortListener listener) {
152 listenerRegistry.addListener(listener);
153 }
154
Thomas Vachuska85021922015-06-29 13:29:42 -0700155 @Override
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700156 public void removeListener(EdgePortListener listener) {
157 listenerRegistry.removeListener(listener);
158 }
159
160
Thomas Vachuska85021922015-06-29 13:29:42 -0700161 // Internal listener for topo events used to keep our edge-port cache
162 // up to date.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700163 private class InnerTopologyListener implements TopologyListener {
164 @Override
165 public void event(TopologyEvent event) {
166 topology = event.subject();
167 List<Event> triggers = event.reasons();
168 if (triggers != null) {
169 triggers.forEach(reason -> {
170 if (reason instanceof DeviceEvent) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700171 processDeviceEvent((DeviceEvent) reason);
172 } else if (reason instanceof LinkEvent) {
173 processLinkEvent((LinkEvent) reason);
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700174 }
175 });
176 } else {
177 loadAllEdgePorts();
178 }
179 }
180 }
181
Thomas Vachuska85021922015-06-29 13:29:42 -0700182 // Initial loading of the edge port cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700183 private void loadAllEdgePorts() {
184 deviceService.getDevices().forEach(d -> deviceService.getPorts(d.id())
185 .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number()))));
186 }
187
Thomas Vachuska85021922015-06-29 13:29:42 -0700188 // Processes a link event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700189 private void processLinkEvent(LinkEvent event) {
190 if (event.type() == LinkEvent.Type.LINK_ADDED) {
191 removeEdgePort(event.subject().src());
192 removeEdgePort(event.subject().dst());
193 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
194 addEdgePort(event.subject().src());
195 addEdgePort(event.subject().dst());
196 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700197 }
198
Thomas Vachuska85021922015-06-29 13:29:42 -0700199 // Processes a device event by adding or removing its end-points in our cache.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700200 private void processDeviceEvent(DeviceEvent event) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700201 DeviceEvent.Type type = event.type();
202 DeviceId id = event.subject().id();
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700203
Thomas Vachuska85021922015-06-29 13:29:42 -0700204 if (type == DEVICE_ADDED ||
205 type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) {
206 // When device is added or becomes available, add all its ports
207 deviceService.getPorts(event.subject().id())
208 .forEach(p -> addEdgePort(new ConnectPoint(id, p.number())));
209 } else if (type == DEVICE_REMOVED ||
210 type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) {
211 // When device is removed or becomes unavailable, remove all its ports
212 deviceService.getPorts(event.subject().id())
213 .forEach(p -> removeEdgePort(new ConnectPoint(id, p.number())));
214 connectionPoints.remove(id);
215
216 } else if (type == DeviceEvent.Type.PORT_ADDED ||
217 type == PORT_UPDATED && event.port().isEnabled()) {
218 addEdgePort(new ConnectPoint(id, event.port().number()));
219 } else if (type == DeviceEvent.Type.PORT_REMOVED ||
220 type == PORT_UPDATED && !event.port().isEnabled()) {
221 removeEdgePort(new ConnectPoint(id, event.port().number()));
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700222 }
223 }
224
Thomas Vachuska85021922015-06-29 13:29:42 -0700225 // Adds the specified connection point to the edge points if needed.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700226 private void addEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700227 if (!topologyService.isInfrastructure(topology, point) && !point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700228 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
229 if (set == null) {
230 set = Sets.newConcurrentHashSet();
231 connectionPoints.put(point.deviceId(), set);
232 }
233 if (set.add(point)) {
234 eventDispatcher.post(new EdgePortEvent(EDGE_PORT_ADDED, point));
235 }
236 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700237 }
238
Thomas Vachuska85021922015-06-29 13:29:42 -0700239 // Removes the specified connection point from the edge points.
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700240 private void removeEdgePort(ConnectPoint point) {
Thomas Vachuska85021922015-06-29 13:29:42 -0700241 if (!point.port().isLogical()) {
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700242 Set<ConnectPoint> set = connectionPoints.get(point.deviceId());
243 if (set == null) {
244 return;
245 }
246 if (set.remove(point)) {
247 eventDispatcher.post(new EdgePortEvent(EDGE_PORT_REMOVED, point));
248 }
249 if (set.isEmpty()) {
250 connectionPoints.remove(point.deviceId());
251 }
252 }
Aaron Kruglikova2b59152015-06-24 14:01:41 -0700253 }
254}