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