Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.mobility; |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 17 | import static org.slf4j.LoggerFactory.getLogger; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.Host; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.flow.FlowRule; |
| 33 | import org.onosproject.net.flow.FlowRuleService; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.criteria.EthCriterion; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.criteria.Criterion; |
| 36 | import org.onosproject.net.flow.criteria.Criterion.Type; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostListener; |
| 39 | import org.onosproject.net.host.HostService; |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 40 | import org.onlab.packet.MacAddress; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import com.google.common.collect.Lists; |
| 44 | |
| 45 | |
| 46 | /** |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 47 | * Sample mobility application. Cleans up flowmods when a host moves. |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | public class HostMobility { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected HostService hostService; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected FlowRuleService flowRuleService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected DeviceService deviceService; |
| 62 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected CoreService coreService; |
| 65 | |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 66 | private ApplicationId appId; |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 70 | appId = coreService.registerApplication("org.onosproject.mobility"); |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 71 | hostService.addListener(new InternalHostListener()); |
| 72 | log.info("Started with Application ID {}", appId.id()); |
| 73 | } |
| 74 | |
| 75 | @Deactivate |
| 76 | public void deactivate() { |
| 77 | flowRuleService.removeFlowRulesById(appId); |
| 78 | log.info("Stopped"); |
| 79 | } |
| 80 | |
| 81 | public class InternalHostListener |
| 82 | implements HostListener { |
| 83 | |
| 84 | @Override |
| 85 | public void event(HostEvent event) { |
| 86 | switch (event.type()) { |
| 87 | case HOST_ADDED: |
| 88 | case HOST_REMOVED: |
| 89 | case HOST_UPDATED: |
| 90 | // don't care if a host has been added, removed. |
| 91 | break; |
| 92 | case HOST_MOVED: |
| 93 | log.info("Host {} has moved; cleaning up.", event.subject()); |
| 94 | cleanup(event.subject()); |
| 95 | break; |
| 96 | |
| 97 | default: |
| 98 | break; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 104 | /** |
| 105 | * For a given host, remove any flow rule which references it's addresses. |
| 106 | * @param host the host to clean up for |
| 107 | */ |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 108 | private void cleanup(Host host) { |
| 109 | Iterable<Device> devices = deviceService.getDevices(); |
| 110 | List<FlowRule> flowRules = Lists.newLinkedList(); |
| 111 | for (Device device : devices) { |
| 112 | flowRules.addAll(cleanupDevice(device, host)); |
| 113 | } |
| 114 | FlowRule[] flows = new FlowRule[flowRules.size()]; |
| 115 | flows = flowRules.toArray(flows); |
| 116 | flowRuleService.removeFlowRules(flows); |
| 117 | } |
| 118 | |
| 119 | private Collection<? extends FlowRule> cleanupDevice(Device device, Host host) { |
| 120 | List<FlowRule> flowRules = Lists.newLinkedList(); |
| 121 | MacAddress mac = host.mac(); |
| 122 | for (FlowRule rule : flowRuleService.getFlowEntries(device.id())) { |
| 123 | for (Criterion c : rule.selector().criteria()) { |
| 124 | if (c.type() == Type.ETH_DST || c.type() == Type.ETH_SRC) { |
| 125 | EthCriterion eth = (EthCriterion) c; |
| 126 | if (eth.mac().equals(mac)) { |
| 127 | flowRules.add(rule); |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 132 | |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 133 | return flowRules; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | |