tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.trivial.impl; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 2 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 5 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 6 | import static java.util.Collections.unmodifiableCollection; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 7 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 8 | import java.util.HashSet; |
| 9 | import java.util.Set; |
| 10 | import java.util.concurrent.ConcurrentHashMap; |
| 11 | import java.util.concurrent.ConcurrentMap; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 12 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 13 | import org.apache.felix.scr.annotations.Activate; |
| 14 | import org.apache.felix.scr.annotations.Component; |
| 15 | import org.apache.felix.scr.annotations.Deactivate; |
| 16 | import org.apache.felix.scr.annotations.Service; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 17 | import org.onlab.onos.ApplicationId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.DeviceId; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 20 | import org.onlab.onos.net.flow.FlowEntry; |
| 21 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.flow.FlowId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.FlowRule; |
| 24 | import org.onlab.onos.net.flow.FlowRuleEvent; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
| 28 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 29 | import org.onlab.util.NewConcurrentHashMap; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 31 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 34 | */ |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 35 | @Component(immediate = true) |
| 36 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 37 | public class SimpleFlowRuleStore |
| 38 | extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate> |
| 39 | implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 40 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 43 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 44 | // inner Map is Device flow table |
| 45 | // Assumption: FlowId cannot have synonyms |
| 46 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, FlowEntry>> |
| 47 | flowEntries = new ConcurrentHashMap<>(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 48 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 49 | @Activate |
| 50 | public void activate() { |
| 51 | log.info("Started"); |
| 52 | } |
| 53 | |
| 54 | @Deactivate |
| 55 | public void deactivate() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 56 | flowEntries.clear(); |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 57 | log.info("Stopped"); |
| 58 | } |
| 59 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 60 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 61 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 62 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 63 | int sum = 0; |
| 64 | for (ConcurrentMap<FlowId, FlowEntry> ft : flowEntries.values()) { |
| 65 | sum += ft.size(); |
| 66 | } |
| 67 | return sum; |
| 68 | } |
| 69 | |
| 70 | private static NewConcurrentHashMap<FlowId, FlowEntry> lazyEmptyFlowTable() { |
| 71 | return NewConcurrentHashMap.<FlowId, FlowEntry>ifNeeded(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the flow table for specified device. |
| 76 | * |
| 77 | * @param deviceId identifier of the device |
| 78 | * @return Map representing Flow Table of given device. |
| 79 | */ |
| 80 | private ConcurrentMap<FlowId, FlowEntry> getFlowTable(DeviceId deviceId) { |
| 81 | return createIfAbsentUnchecked(flowEntries, |
| 82 | deviceId, lazyEmptyFlowTable()); |
| 83 | } |
| 84 | |
| 85 | private FlowEntry getFlowEntry(DeviceId deviceId, FlowId flowId) { |
| 86 | return getFlowTable(deviceId).get(flowId); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 90 | public FlowEntry getFlowEntry(FlowRule rule) { |
| 91 | return getFlowEntry(rule.deviceId(), rule.id()); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 96 | return unmodifiableCollection(getFlowTable(deviceId).values()); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) { |
| 101 | |
| 102 | Set<FlowRule> rules = new HashSet<>(); |
| 103 | for (DeviceId did : flowEntries.keySet()) { |
| 104 | ConcurrentMap<FlowId, FlowEntry> ft = getFlowTable(did); |
| 105 | for (FlowEntry fe : ft.values()) { |
| 106 | if (fe.appId() == appId.id()) { |
| 107 | rules.add(fe); |
| 108 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 111 | return rules; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 114 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 115 | public void storeFlowRule(FlowRule rule) { |
| 116 | final boolean added = storeFlowRuleInternal(rule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 119 | private boolean storeFlowRuleInternal(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 120 | FlowEntry f = new DefaultFlowEntry(rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 121 | final DeviceId did = f.deviceId(); |
| 122 | final FlowId fid = f.id(); |
| 123 | FlowEntry existing = getFlowTable(did).putIfAbsent(fid, f); |
| 124 | if (existing != null) { |
| 125 | // was already there? ignore |
| 126 | return false; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 127 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 128 | // new flow rule added |
| 129 | // TODO: notify through delegate about remote event? |
| 130 | return true; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 134 | public void deleteFlowRule(FlowRule rule) { |
| 135 | |
| 136 | FlowEntry entry = getFlowEntry(rule.deviceId(), rule.id()); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 137 | if (entry == null) { |
Yuta HIGUCHI | 58e4043 | 2014-10-19 15:43:41 -0700 | [diff] [blame^] | 138 | //log.warn("Cannot find rule {}", rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 139 | return; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 140 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 141 | synchronized (entry) { |
| 142 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 143 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 144 | } |
| 145 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 146 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 147 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 148 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 149 | FlowEntry stored = getFlowEntry(rule.deviceId(), rule.id()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 150 | if (stored != null) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 151 | synchronized (stored) { |
| 152 | stored.setBytes(rule.bytes()); |
| 153 | stored.setLife(rule.life()); |
| 154 | stored.setPackets(rule.packets()); |
| 155 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 156 | stored.setState(FlowEntryState.ADDED); |
| 157 | // TODO: Do we need to change `rule` state? |
| 158 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 159 | } |
| 160 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 161 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 162 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 163 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 164 | // should not reach here |
| 165 | // storeFlowRule was expected to be called |
| 166 | log.error("FlowRule was not found in store {} to update", rule); |
| 167 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 168 | //flowEntries.put(did, rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 169 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 170 | } |
| 171 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 172 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 173 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 174 | // This is where one could mark a rule as removed and still keep it in the store. |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 175 | final DeviceId did = rule.deviceId(); |
| 176 | |
| 177 | ConcurrentMap<FlowId, FlowEntry> ft = getFlowTable(did); |
| 178 | if (ft.remove(rule.id(), rule)) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 179 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 180 | } else { |
| 181 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 184 | } |