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