tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 2 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 6 | import java.util.Iterator; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 7 | import java.util.List; |
| 8 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 14 | import org.apache.felix.scr.annotations.Service; |
| 15 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 16 | import org.onlab.onos.event.EventDeliveryService; |
| 17 | import org.onlab.onos.net.Device; |
| 18 | import org.onlab.onos.net.DeviceId; |
| 19 | import org.onlab.onos.net.device.DeviceService; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.flow.DefaultFlowRule; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 24 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 25 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 26 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 27 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 28 | import org.onlab.onos.net.flow.FlowRuleService; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.FlowRuleStore; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 31 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 32 | import org.slf4j.Logger; |
| 33 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 34 | import com.google.common.collect.Lists; |
| 35 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 36 | @Component(immediate = true) |
| 37 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 38 | public class FlowRuleManager |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 39 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 40 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 41 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 42 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 46 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 47 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 50 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 52 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 55 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 56 | |
| 57 | @Activate |
| 58 | public void activate() { |
| 59 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 60 | log.info("Started"); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | public void deactivate() { |
| 65 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 66 | log.info("Stopped"); |
| 67 | } |
| 68 | |
| 69 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 70 | public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 71 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 75 | public void applyFlowRules(FlowRule... flowRules) { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 76 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 77 | FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_ADD); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 78 | final Device device = deviceService.getDevice(f.deviceId()); |
| 79 | final FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 80 | store.storeFlowRule(f); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 81 | frp.applyFlowRule(f); |
| 82 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void removeFlowRules(FlowRule... flowRules) { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 87 | FlowRule f; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 88 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 89 | f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_REMOVE); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 90 | final Device device = deviceService.getDevice(f.deviceId()); |
| 91 | final FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 92 | store.deleteFlowRule(f); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 93 | frp.removeFlowRule(f); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void addListener(FlowRuleListener listener) { |
| 100 | listenerRegistry.addListener(listener); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void removeListener(FlowRuleListener listener) { |
| 105 | listenerRegistry.removeListener(listener); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected FlowRuleProviderService createProviderService( |
| 110 | FlowRuleProvider provider) { |
| 111 | return new InternalFlowRuleProviderService(provider); |
| 112 | } |
| 113 | |
| 114 | private class InternalFlowRuleProviderService |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 115 | extends AbstractProviderService<FlowRuleProvider> |
| 116 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 117 | |
| 118 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 119 | super(provider); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void flowRemoved(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 124 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 125 | checkValidity(); |
| 126 | FlowRuleEvent event = store.removeFlowRule(flowRule); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 127 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 128 | if (event != null) { |
| 129 | log.debug("Flow {} removed", flowRule); |
| 130 | post(event); |
| 131 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void flowMissing(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 136 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 137 | checkValidity(); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 138 | log.info("Flow {} has not been installed.", flowRule); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 139 | |
| 140 | } |
| 141 | |
| 142 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 143 | public void extraneousFlow(FlowRule flowRule) { |
| 144 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 145 | checkValidity(); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 146 | log.info("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 150 | public void flowAdded(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 151 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 152 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 153 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 154 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule); |
| 155 | if (event == null) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 156 | log.debug("No flow store event generated."); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 157 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 158 | log.debug("Flow {} {}", flowRule, event.type()); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 159 | post(event); |
| 160 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 161 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 164 | // Posts the specified event to the local event dispatcher. |
| 165 | private void post(FlowRuleEvent event) { |
| 166 | if (event != null) { |
| 167 | eventDispatcher.post(event); |
| 168 | } |
| 169 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 170 | |
| 171 | @Override |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 172 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) { |
| 173 | List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame^] | 174 | |
| 175 | Iterator<FlowRule> switchRulesIterator = flowEntries.iterator(); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 176 | |
| 177 | while (switchRulesIterator.hasNext()) { |
| 178 | FlowRule rule = switchRulesIterator.next(); |
| 179 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 180 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 181 | flowAdded(rule); |
| 182 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 183 | // the device has a rule the store does not have |
| 184 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | for (FlowRule rule : storedRules) { |
| 188 | // there are rules in the store that aren't on the switch |
| 189 | flowMissing(rule); |
| 190 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 191 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | } |