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