Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.mapping.impl; |
| 17 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 18 | import com.google.common.collect.FluentIterable; |
Jian Li | 791dd32 | 2017-04-20 00:15:56 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.mapping.DefaultMappingEntry; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 27 | import org.onosproject.mapping.Mapping; |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 28 | import org.onosproject.mapping.MappingEntry; |
Jian Li | a1f960b | 2017-04-25 19:12:53 +0900 | [diff] [blame] | 29 | import org.onosproject.mapping.MappingEntry.MappingEntryState; |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 30 | import org.onosproject.mapping.MappingEvent; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 31 | import org.onosproject.mapping.MappingId; |
| 32 | import org.onosproject.mapping.MappingStore; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 33 | import org.onosproject.mapping.MappingStoreDelegate; |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 34 | import org.onosproject.mapping.StoredMappingEntry; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.store.AbstractStore; |
| 37 | import org.osgi.service.component.ComponentContext; |
| 38 | import org.slf4j.Logger; |
| 39 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 40 | import java.util.Collections; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.concurrent.ConcurrentMap; |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 43 | import java.util.concurrent.CopyOnWriteArrayList; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 44 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 45 | import static org.onosproject.mapping.MappingEntry.MappingEntryState.PENDING_ADD; |
| 46 | import static org.onosproject.mapping.MappingEntry.MappingEntryState.PENDING_REMOVE; |
| 47 | import static org.onosproject.mapping.MappingEvent.Type.MAPPING_ADDED; |
| 48 | import static org.onosproject.mapping.MappingEvent.Type.MAPPING_REMOVED; |
| 49 | import static org.onosproject.mapping.MappingEvent.Type.MAPPING_UPDATED; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
| 53 | * Manages inventory of mappings using trivial in-memory implementation. |
| 54 | */ |
| 55 | @Component(immediate = true) |
| 56 | @Service |
| 57 | public class SimpleMappingStore |
| 58 | extends AbstractStore<MappingEvent, MappingStoreDelegate> |
| 59 | implements MappingStore { |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 63 | private static final String UNRECOGNIZED_STORE_MSG = "Unrecognized store type {}"; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 64 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 65 | private final ConcurrentMap<DeviceId, ConcurrentMap<MappingId, |
| 66 | List<StoredMappingEntry>>> mapDbStore = Maps.newConcurrentMap(); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 67 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 68 | private final ConcurrentMap<DeviceId, ConcurrentMap<MappingId, |
| 69 | List<StoredMappingEntry>>> mapCacheStore = Maps.newConcurrentMap(); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 70 | |
| 71 | @Activate |
| 72 | public void activate() { |
| 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | public void deactivate() { |
| 78 | mapDbStore.clear(); |
| 79 | mapCacheStore.clear(); |
| 80 | log.info("Stopped"); |
| 81 | } |
| 82 | |
| 83 | @Modified |
| 84 | public void modified(ComponentContext context) { |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int getMappingCount(Type type) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 90 | int sum = 0; |
| 91 | |
Jian Li | 791dd32 | 2017-04-20 00:15:56 +0900 | [diff] [blame] | 92 | for (ConcurrentMap<MappingId, List<StoredMappingEntry>> mapDb : |
| 93 | getMappingStore(type).values()) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 94 | for (List<StoredMappingEntry> mes : mapDb.values()) { |
| 95 | sum += mes.size(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return sum; |
| 100 | } |
| 101 | |
Jian Li | 791dd32 | 2017-04-20 00:15:56 +0900 | [diff] [blame] | 102 | @Override |
| 103 | public Iterable<MappingEntry> getAllMappingEntries(Type type) { |
| 104 | |
| 105 | List<MappingEntry> entries = Lists.newArrayList(); |
| 106 | |
| 107 | for (ConcurrentMap<MappingId, List<StoredMappingEntry>> mapDb : |
| 108 | getMappingStore(type).values()) { |
| 109 | for (List<StoredMappingEntry> mes : mapDb.values()) { |
| 110 | entries.addAll(mes); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return entries; |
| 115 | } |
| 116 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 117 | /** |
| 118 | * Obtains the mapping store for specified device and store type. |
| 119 | * |
| 120 | * @param type mapping store type |
| 121 | * @param deviceId device identifier |
| 122 | * @return Map representing Mapping Store of given device and store type |
| 123 | */ |
| 124 | private ConcurrentMap<MappingId, List<StoredMappingEntry>> |
| 125 | getMappingStoreByDeviceId(Type type, |
| 126 | DeviceId deviceId) { |
| 127 | switch (type) { |
| 128 | case MAP_DATABASE: |
| 129 | return mapDbStore.computeIfAbsent(deviceId, |
| 130 | k -> Maps.newConcurrentMap()); |
| 131 | case MAP_CACHE: |
| 132 | return mapCacheStore.computeIfAbsent(deviceId, |
| 133 | k -> Maps.newConcurrentMap()); |
| 134 | default: |
| 135 | log.error(UNRECOGNIZED_STORE_MSG, type); |
| 136 | return null; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Obtains the mapping store for specified store type. |
| 142 | * |
| 143 | * @param type mapping store type |
| 144 | * @return mapping store |
| 145 | */ |
| 146 | private ConcurrentMap<DeviceId, ConcurrentMap<MappingId, List<StoredMappingEntry>>> |
| 147 | getMappingStore(Type type) { |
| 148 | switch (type) { |
| 149 | case MAP_DATABASE: |
| 150 | return mapDbStore; |
| 151 | case MAP_CACHE: |
| 152 | return mapCacheStore; |
| 153 | default: |
| 154 | log.error(UNRECOGNIZED_STORE_MSG, type); |
| 155 | return null; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Obtains mapping entries for specified device, store type and mapping ID. |
| 161 | * |
| 162 | * @param type mapping store type |
| 163 | * @param deviceId device identifier |
| 164 | * @param mappingId mapping identifier |
| 165 | * @return a collection of mapping entries |
| 166 | */ |
| 167 | private List<StoredMappingEntry> getMappingEntriesInternal(Type type, |
| 168 | DeviceId deviceId, |
| 169 | MappingId mappingId) { |
| 170 | final ConcurrentMap<MappingId, List<StoredMappingEntry>> |
| 171 | store = getMappingStoreByDeviceId(type, deviceId); |
| 172 | List<StoredMappingEntry> r = store.get(mappingId); |
| 173 | if (r == null) { |
| 174 | final List<StoredMappingEntry> concurrentlyAdded; |
| 175 | r = new CopyOnWriteArrayList<>(); |
| 176 | concurrentlyAdded = store.putIfAbsent(mappingId, r); |
| 177 | if (concurrentlyAdded != null) { |
| 178 | return concurrentlyAdded; |
| 179 | } |
| 180 | } |
| 181 | return r; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Obtains a mapping entry for specified device, store type and mapping. |
| 186 | * |
| 187 | * @param type mapping store type |
| 188 | * @param deviceId device identifier |
| 189 | * @param mapping mapping identifier |
| 190 | * @return a mapping entry |
| 191 | */ |
| 192 | private MappingEntry getMappingEntryInternal(Type type, DeviceId deviceId, |
| 193 | Mapping mapping) { |
| 194 | List<StoredMappingEntry> mes = |
| 195 | getMappingEntriesInternal(type, deviceId, mapping.id()); |
| 196 | for (StoredMappingEntry me : mes) { |
| 197 | if (me.equals(mapping)) { |
| 198 | return me; |
| 199 | } |
| 200 | } |
| 201 | return null; |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public MappingEntry getMappingEntry(Type type, Mapping mapping) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 206 | return getMappingEntryInternal(type, mapping.deviceId(), mapping); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Iterable<MappingEntry> getMappingEntries(Type type, DeviceId deviceId) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 211 | // FIXME: a better way is using Java 8 API to return immutable iterables |
| 212 | return FluentIterable.from(getMappingStoreByDeviceId(type, deviceId).values()) |
| 213 | .transformAndConcat(Collections::unmodifiableList); |
| 214 | } |
| 215 | |
| 216 | @Override |
Jian Li | a1f960b | 2017-04-25 19:12:53 +0900 | [diff] [blame] | 217 | public void storeMapping(Type type, MappingEntry mapping) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 218 | |
| 219 | List<StoredMappingEntry> entries = |
| 220 | getMappingEntriesInternal(type, mapping.deviceId(), mapping.id()); |
| 221 | |
| 222 | synchronized (entries) { |
| 223 | if (!entries.contains(mapping)) { |
Jian Li | a1f960b | 2017-04-25 19:12:53 +0900 | [diff] [blame] | 224 | StoredMappingEntry entry = |
| 225 | new DefaultMappingEntry(mapping, mapping.state()); |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 226 | entries.add(entry); |
| 227 | } |
| 228 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 232 | public void pendingDeleteMapping(Type type, Mapping mapping) { |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 233 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 234 | List<StoredMappingEntry> entries = |
| 235 | getMappingEntriesInternal(type, mapping.deviceId(), mapping.id()); |
| 236 | |
| 237 | synchronized (entries) { |
| 238 | for (StoredMappingEntry entry : entries) { |
| 239 | if (entry.equals(mapping)) { |
| 240 | synchronized (entry) { |
| 241 | entry.setState(PENDING_REMOVE); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public MappingEvent addOrUpdateMappingEntry(Type type, MappingEntry entry) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 250 | |
| 251 | List<StoredMappingEntry> entries = |
| 252 | getMappingEntriesInternal(type, entry.deviceId(), entry.id()); |
| 253 | synchronized (entries) { |
| 254 | for (StoredMappingEntry stored : entries) { |
| 255 | if (stored.equals(entry)) { |
| 256 | if (stored.state() == PENDING_ADD) { |
Jian Li | a1f960b | 2017-04-25 19:12:53 +0900 | [diff] [blame] | 257 | stored.setState(MappingEntryState.ADDED); |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 258 | return new MappingEvent(MAPPING_ADDED, entry); |
| 259 | } |
| 260 | return new MappingEvent(MAPPING_UPDATED, entry); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | log.error("Mapping was not found in store {} to update", entry); |
| 266 | |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 267 | return null; |
| 268 | } |
| 269 | |
| 270 | @Override |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 271 | public MappingEvent removeMapping(Type type, Mapping mapping) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 272 | |
| 273 | List<StoredMappingEntry> entries = |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 274 | getMappingEntriesInternal(type, mapping.deviceId(), mapping.id()); |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 275 | synchronized (entries) { |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 276 | if (entries.remove(mapping)) { |
| 277 | return new MappingEvent(MAPPING_REMOVED, mapping); |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 278 | } |
| 279 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 280 | return null; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public MappingEvent pendingMappingEntry(Type type, MappingEntry entry) { |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 285 | List<StoredMappingEntry> entries = |
| 286 | getMappingEntriesInternal(type, entry.deviceId(), entry.id()); |
| 287 | synchronized (entries) { |
| 288 | for (StoredMappingEntry stored : entries) { |
| 289 | if (stored.equals(entry) && (stored.state() != PENDING_ADD)) { |
| 290 | synchronized (stored) { |
| 291 | stored.setState(PENDING_ADD); |
| 292 | return new MappingEvent(MAPPING_UPDATED, entry); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 297 | return null; |
| 298 | } |
| 299 | |
| 300 | @Override |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 301 | public void purgeMappingEntry(Type type, DeviceId deviceId) { |
| 302 | getMappingStore(type).remove(deviceId); |
| 303 | } |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 304 | |
Jian Li | 252750d | 2017-03-01 04:50:13 +0900 | [diff] [blame] | 305 | @Override |
| 306 | public void purgeMappingEntries(Type type) { |
| 307 | getMappingStore(type).clear(); |
Jian Li | 9291959 | 2017-02-27 17:10:47 +0900 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | |