blob: cb60405c79fd6c709b1f7374564a94f5e8da7879 [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;
13import org.onlab.onos.net.Device;
14import org.onlab.onos.net.Host;
15import org.onlab.onos.net.device.DeviceService;
16import org.onlab.onos.net.flow.FlowRule;
17import org.onlab.onos.net.flow.FlowRuleService;
18import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
19import org.onlab.onos.net.flow.criteria.Criterion;
20import org.onlab.onos.net.flow.criteria.Criterion.Type;
21import org.onlab.onos.net.host.HostEvent;
22import org.onlab.onos.net.host.HostListener;
23import org.onlab.onos.net.host.HostService;
24import org.onlab.packet.MacAddress;
25import org.slf4j.Logger;
26
27import com.google.common.collect.Lists;
28
29
30/**
31 * Sample reactive forwarding application.
32 */
33@Component(immediate = true)
34public class HostMobility {
35
36 private final Logger log = getLogger(getClass());
37
38 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
39 protected HostService hostService;
40
41 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected FlowRuleService flowRuleService;
43
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected DeviceService deviceService;
46
47 private ApplicationId appId;
48
49 @Activate
50 public void activate() {
51 appId = ApplicationId.getAppId();
52 hostService.addListener(new InternalHostListener());
53 log.info("Started with Application ID {}", appId.id());
54 }
55
56 @Deactivate
57 public void deactivate() {
58 flowRuleService.removeFlowRulesById(appId);
59 log.info("Stopped");
60 }
61
62 public class InternalHostListener
63 implements HostListener {
64
65 @Override
66 public void event(HostEvent event) {
67 switch (event.type()) {
68 case HOST_ADDED:
69 case HOST_REMOVED:
70 case HOST_UPDATED:
71 // don't care if a host has been added, removed.
72 break;
73 case HOST_MOVED:
74 log.info("Host {} has moved; cleaning up.", event.subject());
75 cleanup(event.subject());
76 break;
77
78 default:
79 break;
80
81 }
82
83 }
84
85 private void cleanup(Host host) {
86 Iterable<Device> devices = deviceService.getDevices();
87 List<FlowRule> flowRules = Lists.newLinkedList();
88 for (Device device : devices) {
89 flowRules.addAll(cleanupDevice(device, host));
90 }
91 FlowRule[] flows = new FlowRule[flowRules.size()];
92 flows = flowRules.toArray(flows);
93 flowRuleService.removeFlowRules(flows);
94 }
95
96 private Collection<? extends FlowRule> cleanupDevice(Device device, Host host) {
97 List<FlowRule> flowRules = Lists.newLinkedList();
98 MacAddress mac = host.mac();
99 for (FlowRule rule : flowRuleService.getFlowEntries(device.id())) {
100 for (Criterion c : rule.selector().criteria()) {
101 if (c.type() == Type.ETH_DST || c.type() == Type.ETH_SRC) {
102 EthCriterion eth = (EthCriterion) c;
103 if (eth.mac().equals(mac)) {
104 flowRules.add(rule);
105 break;
106 }
107 }
108 }
109 }
110 //TODO: handle ip cleanup
111 return flowRules;
112 }
113
114 }
115
116}
117
118