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