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