blob: c4ba877c92926f6fcbe7a44d9f616512acc303a5 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabib4906fab2014-09-29 23:58:12 -070016package org.onlab.onos.mobility;
17import static org.slf4j.LoggerFactory.getLogger;
18
19import java.util.Collection;
20import java.util.List;
21
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070027import org.onlab.onos.core.ApplicationId;
28import org.onlab.onos.core.CoreService;
alshabib4906fab2014-09-29 23:58:12 -070029import org.onlab.onos.net.Device;
30import org.onlab.onos.net.Host;
31import org.onlab.onos.net.device.DeviceService;
32import org.onlab.onos.net.flow.FlowRule;
33import org.onlab.onos.net.flow.FlowRuleService;
34import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
35import org.onlab.onos.net.flow.criteria.Criterion;
36import org.onlab.onos.net.flow.criteria.Criterion.Type;
37import org.onlab.onos.net.host.HostEvent;
38import org.onlab.onos.net.host.HostListener;
39import org.onlab.onos.net.host.HostService;
40import org.onlab.packet.MacAddress;
41import org.slf4j.Logger;
42
43import com.google.common.collect.Lists;
44
45
46/**
alshabib0ff17ad2014-09-30 09:46:40 -070047 * Sample mobility application. Cleans up flowmods when a host moves.
alshabib4906fab2014-09-29 23:58:12 -070048 */
49@Component(immediate = true)
50public 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
alshabib92c65ad2014-10-08 21:56:05 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected CoreService coreService;
65
alshabib4906fab2014-09-29 23:58:12 -070066 private ApplicationId appId;
67
68 @Activate
69 public void activate() {
alshabib92c65ad2014-10-08 21:56:05 -070070 appId = coreService.registerApplication("org.onlab.onos.mobility");
alshabib4906fab2014-09-29 23:58:12 -070071 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
alshabib0ff17ad2014-09-30 09:46:40 -0700104 /**
105 * For a given host, remove any flow rule which references it's addresses.
106 * @param host the host to clean up for
107 */
alshabib4906fab2014-09-29 23:58:12 -0700108 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);
alshabib4906fab2014-09-29 23:58:12 -0700128 }
129 }
130 }
131 }
132 //TODO: handle ip cleanup
133 return flowRules;
134 }
135
136 }
137
138}
139
140