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