alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.flow.impl; |
| 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; |
| 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 | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.flow.FlowRule; |
| 21 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 22 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 23 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 24 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 25 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 26 | import org.onlab.onos.net.flow.FlowRuleService; |
| 27 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 28 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | @Component(immediate = true) |
| 32 | @Service |
| 33 | public class SimpleFlowRuleManager |
| 34 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 35 | implements FlowRuleService, FlowRuleProviderRegistry { |
| 36 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 37 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
| 40 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
| 41 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 42 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 43 | private final SimpleFlowRuleStore store = new SimpleFlowRuleStore(); |
| 44 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 46 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 49 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 50 | |
| 51 | @Activate |
| 52 | public void activate() { |
| 53 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 54 | log.info("Started"); |
| 55 | } |
| 56 | |
| 57 | @Deactivate |
| 58 | public void deactivate() { |
| 59 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 60 | log.info("Stopped"); |
| 61 | } |
| 62 | |
| 63 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 64 | public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 65 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 69 | public List<FlowRule> applyFlowRules(FlowRule... flowRules) { |
| 70 | List<FlowRule> entries = new ArrayList<FlowRule>(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 71 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 72 | for (int i = 0; i < flowRules.length; i++) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 73 | FlowRule f = flowRules[i]; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 74 | final Device device = deviceService.getDevice(f.deviceId()); |
| 75 | final FlowRuleProvider frp = getProvider(device.providerId()); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 76 | entries.add(store.storeFlowRule(f)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 77 | frp.applyFlowRule(f); |
| 78 | } |
| 79 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 80 | return entries; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void removeFlowRules(FlowRule... flowRules) { |
| 85 | for (int i = 0; i < flowRules.length; i++) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 86 | FlowRule f = flowRules[i]; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 87 | final Device device = deviceService.getDevice(f.deviceId()); |
| 88 | final FlowRuleProvider frp = getProvider(device.providerId()); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 89 | store.removeFlowRule(f); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 90 | frp.removeFlowRule(f); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void addListener(FlowRuleListener listener) { |
| 97 | listenerRegistry.addListener(listener); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void removeListener(FlowRuleListener listener) { |
| 102 | listenerRegistry.removeListener(listener); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected FlowRuleProviderService createProviderService( |
| 107 | FlowRuleProvider provider) { |
| 108 | return new InternalFlowRuleProviderService(provider); |
| 109 | } |
| 110 | |
| 111 | private class InternalFlowRuleProviderService |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 112 | extends AbstractProviderService<FlowRuleProvider> |
| 113 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 114 | |
| 115 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 116 | super(provider); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void flowRemoved(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 121 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 122 | checkValidity(); |
| 123 | FlowRuleEvent event = store.removeFlowRule(flowRule); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 124 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 125 | if (event != null) { |
| 126 | log.debug("Flow {} removed", flowRule); |
| 127 | post(event); |
| 128 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void flowMissing(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 133 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 134 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 135 | // TODO Auto-generated method stub |
| 136 | |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void flowAdded(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 141 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 142 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 143 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 144 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule); |
| 145 | if (event == null) { |
| 146 | log.debug("Flow {} updated", flowRule); |
| 147 | } else { |
| 148 | log.debug("Flow {} added", flowRule); |
| 149 | post(event); |
| 150 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 153 | // Posts the specified event to the local event dispatcher. |
| 154 | private void post(FlowRuleEvent event) { |
| 155 | if (event != null) { |
| 156 | eventDispatcher.post(event); |
| 157 | } |
| 158 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 159 | |
| 160 | @Override |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 161 | public void pushFlowMetrics(Iterable<FlowRule> flowEntries) { |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 162 | // TODO Auto-generated method stub |
| 163 | |
| 164 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } |