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