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; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame^] | 13 | import org.onlab.onos.CoreService; |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.Device; |
| 15 | import org.onlab.onos.net.Host; |
| 16 | import org.onlab.onos.net.device.DeviceService; |
| 17 | import org.onlab.onos.net.flow.FlowRule; |
| 18 | import org.onlab.onos.net.flow.FlowRuleService; |
| 19 | import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion; |
| 20 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 21 | import org.onlab.onos.net.flow.criteria.Criterion.Type; |
| 22 | import org.onlab.onos.net.host.HostEvent; |
| 23 | import org.onlab.onos.net.host.HostListener; |
| 24 | import org.onlab.onos.net.host.HostService; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import com.google.common.collect.Lists; |
| 29 | |
| 30 | |
| 31 | /** |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 32 | * Sample mobility application. Cleans up flowmods when a host moves. |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 33 | */ |
| 34 | @Component(immediate = true) |
| 35 | public 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 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame^] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected CoreService coreService; |
| 50 | |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 51 | private ApplicationId appId; |
| 52 | |
| 53 | @Activate |
| 54 | public void activate() { |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame^] | 55 | appId = coreService.registerApplication("org.onlab.onos.mobility"); |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 56 | 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 | |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 89 | /** |
| 90 | * For a given host, remove any flow rule which references it's addresses. |
| 91 | * @param host the host to clean up for |
| 92 | */ |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 93 | 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); |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | //TODO: handle ip cleanup |
| 118 | return flowRules; |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
| 125 | |