alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 1 | package org.onlab.onos.mobility; |
| 2 | import static org.slf4j.LoggerFactory.getLogger; |
| 3 | |
| 4 | import java.util.Collection; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 12 | import org.onlab.onos.ApplicationId; |
| 13 | import org.onlab.onos.net.Device; |
| 14 | import org.onlab.onos.net.Host; |
| 15 | import org.onlab.onos.net.device.DeviceService; |
| 16 | import org.onlab.onos.net.flow.FlowRule; |
| 17 | import org.onlab.onos.net.flow.FlowRuleService; |
| 18 | import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion; |
| 19 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 20 | import org.onlab.onos.net.flow.criteria.Criterion.Type; |
| 21 | import org.onlab.onos.net.host.HostEvent; |
| 22 | import org.onlab.onos.net.host.HostListener; |
| 23 | import org.onlab.onos.net.host.HostService; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import com.google.common.collect.Lists; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Sample reactive forwarding application. |
| 32 | */ |
| 33 | @Component(immediate = true) |
| 34 | public 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 | |