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