blob: 88b3a5c9e2b78312a3ea42aafa21d0b51f60238e [file] [log] [blame]
alshabib4906fab2014-09-29 23:58:12 -07001package org.onlab.onos.mobility;
2import static org.slf4j.LoggerFactory.getLogger;
3
4import java.util.Collection;
5import java.util.List;
6
7import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Reference;
11import org.apache.felix.scr.annotations.ReferenceCardinality;
12import org.onlab.onos.ApplicationId;
alshabib92c65ad2014-10-08 21:56:05 -070013import org.onlab.onos.CoreService;
alshabib4906fab2014-09-29 23:58:12 -070014import org.onlab.onos.net.Device;
15import org.onlab.onos.net.Host;
16import org.onlab.onos.net.device.DeviceService;
17import org.onlab.onos.net.flow.FlowRule;
18import org.onlab.onos.net.flow.FlowRuleService;
19import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
20import org.onlab.onos.net.flow.criteria.Criterion;
21import org.onlab.onos.net.flow.criteria.Criterion.Type;
22import org.onlab.onos.net.host.HostEvent;
23import org.onlab.onos.net.host.HostListener;
24import org.onlab.onos.net.host.HostService;
25import org.onlab.packet.MacAddress;
26import org.slf4j.Logger;
27
28import com.google.common.collect.Lists;
29
30
31/**
alshabib0ff17ad2014-09-30 09:46:40 -070032 * Sample mobility application. Cleans up flowmods when a host moves.
alshabib4906fab2014-09-29 23:58:12 -070033 */
34@Component(immediate = true)
35public class HostMobility {
36
37 private final Logger log = getLogger(getClass());
38
39 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
40 protected HostService hostService;
41
42 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected FlowRuleService flowRuleService;
44
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected DeviceService deviceService;
47
alshabib92c65ad2014-10-08 21:56:05 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected CoreService coreService;
50
alshabib4906fab2014-09-29 23:58:12 -070051 private ApplicationId appId;
52
53 @Activate
54 public void activate() {
alshabib92c65ad2014-10-08 21:56:05 -070055 appId = coreService.registerApplication("org.onlab.onos.mobility");
alshabib4906fab2014-09-29 23:58:12 -070056 hostService.addListener(new InternalHostListener());
57 log.info("Started with Application ID {}", appId.id());
58 }
59
60 @Deactivate
61 public void deactivate() {
62 flowRuleService.removeFlowRulesById(appId);
63 log.info("Stopped");
64 }
65
66 public class InternalHostListener
67 implements HostListener {
68
69 @Override
70 public void event(HostEvent event) {
71 switch (event.type()) {
72 case HOST_ADDED:
73 case HOST_REMOVED:
74 case HOST_UPDATED:
75 // don't care if a host has been added, removed.
76 break;
77 case HOST_MOVED:
78 log.info("Host {} has moved; cleaning up.", event.subject());
79 cleanup(event.subject());
80 break;
81
82 default:
83 break;
84
85 }
86
87 }
88
alshabib0ff17ad2014-09-30 09:46:40 -070089 /**
90 * For a given host, remove any flow rule which references it's addresses.
91 * @param host the host to clean up for
92 */
alshabib4906fab2014-09-29 23:58:12 -070093 private void cleanup(Host host) {
94 Iterable<Device> devices = deviceService.getDevices();
95 List<FlowRule> flowRules = Lists.newLinkedList();
96 for (Device device : devices) {
97 flowRules.addAll(cleanupDevice(device, host));
98 }
99 FlowRule[] flows = new FlowRule[flowRules.size()];
100 flows = flowRules.toArray(flows);
101 flowRuleService.removeFlowRules(flows);
102 }
103
104 private Collection<? extends FlowRule> cleanupDevice(Device device, Host host) {
105 List<FlowRule> flowRules = Lists.newLinkedList();
106 MacAddress mac = host.mac();
107 for (FlowRule rule : flowRuleService.getFlowEntries(device.id())) {
108 for (Criterion c : rule.selector().criteria()) {
109 if (c.type() == Type.ETH_DST || c.type() == Type.ETH_SRC) {
110 EthCriterion eth = (EthCriterion) c;
111 if (eth.mac().equals(mac)) {
112 flowRules.add(rule);
alshabib4906fab2014-09-29 23:58:12 -0700113 }
114 }
115 }
116 }
117 //TODO: handle ip cleanup
118 return flowRules;
119 }
120
121 }
122
123}
124
125