blob: eed42ee8568cbe8c0bce3c561399cd9628305a14 [file] [log] [blame]
Jian Li92919592017-02-27 17:10:47 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li92919592017-02-27 17:10:47 +09003 *
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 */
16package org.onosproject.mapping.impl;
17
Jian Li252750d2017-03-01 04:50:13 +090018import com.google.common.collect.FluentIterable;
Jian Li791dd322017-04-20 00:15:56 +090019import com.google.common.collect.Lists;
Jian Li92919592017-02-27 17:10:47 +090020import com.google.common.collect.Maps;
Jian Li252750d2017-03-01 04:50:13 +090021import org.onosproject.mapping.DefaultMappingEntry;
Jian Li92919592017-02-27 17:10:47 +090022import org.onosproject.mapping.Mapping;
Jian Li252750d2017-03-01 04:50:13 +090023import org.onosproject.mapping.MappingEntry;
Jian Lia1f960b2017-04-25 19:12:53 +090024import org.onosproject.mapping.MappingEntry.MappingEntryState;
Jian Li252750d2017-03-01 04:50:13 +090025import org.onosproject.mapping.MappingEvent;
Jian Li92919592017-02-27 17:10:47 +090026import org.onosproject.mapping.MappingId;
27import org.onosproject.mapping.MappingStore;
Jian Li92919592017-02-27 17:10:47 +090028import org.onosproject.mapping.MappingStoreDelegate;
Jian Li252750d2017-03-01 04:50:13 +090029import org.onosproject.mapping.StoredMappingEntry;
Jian Li92919592017-02-27 17:10:47 +090030import org.onosproject.net.DeviceId;
31import org.onosproject.store.AbstractStore;
32import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import org.osgi.service.component.annotations.Activate;
34import org.osgi.service.component.annotations.Component;
35import org.osgi.service.component.annotations.Deactivate;
36import org.osgi.service.component.annotations.Modified;
Jian Li92919592017-02-27 17:10:47 +090037import org.slf4j.Logger;
38
Jian Li252750d2017-03-01 04:50:13 +090039import java.util.Collections;
Jian Li92919592017-02-27 17:10:47 +090040import java.util.List;
41import java.util.concurrent.ConcurrentMap;
Jian Li252750d2017-03-01 04:50:13 +090042import java.util.concurrent.CopyOnWriteArrayList;
Jian Li92919592017-02-27 17:10:47 +090043
Jian Li252750d2017-03-01 04:50:13 +090044import static org.onosproject.mapping.MappingEntry.MappingEntryState.PENDING_ADD;
45import static org.onosproject.mapping.MappingEntry.MappingEntryState.PENDING_REMOVE;
46import static org.onosproject.mapping.MappingEvent.Type.MAPPING_ADDED;
47import static org.onosproject.mapping.MappingEvent.Type.MAPPING_REMOVED;
48import static org.onosproject.mapping.MappingEvent.Type.MAPPING_UPDATED;
Jian Li92919592017-02-27 17:10:47 +090049import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Manages inventory of mappings using trivial in-memory implementation.
53 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054@Component(immediate = true, service = MappingStore.class)
Jian Li92919592017-02-27 17:10:47 +090055public class SimpleMappingStore
56 extends AbstractStore<MappingEvent, MappingStoreDelegate>
57 implements MappingStore {
58
59 private final Logger log = getLogger(getClass());
60
Jian Li252750d2017-03-01 04:50:13 +090061 private static final String UNRECOGNIZED_STORE_MSG = "Unrecognized store type {}";
Jian Li92919592017-02-27 17:10:47 +090062
Jian Li252750d2017-03-01 04:50:13 +090063 private final ConcurrentMap<DeviceId, ConcurrentMap<MappingId,
64 List<StoredMappingEntry>>> mapDbStore = Maps.newConcurrentMap();
Jian Li92919592017-02-27 17:10:47 +090065
Jian Li252750d2017-03-01 04:50:13 +090066 private final ConcurrentMap<DeviceId, ConcurrentMap<MappingId,
67 List<StoredMappingEntry>>> mapCacheStore = Maps.newConcurrentMap();
Jian Li92919592017-02-27 17:10:47 +090068
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 Li252750d2017-03-01 04:50:13 +090088 int sum = 0;
89
Jian Li791dd322017-04-20 00:15:56 +090090 for (ConcurrentMap<MappingId, List<StoredMappingEntry>> mapDb :
91 getMappingStore(type).values()) {
Jian Li252750d2017-03-01 04:50:13 +090092 for (List<StoredMappingEntry> mes : mapDb.values()) {
93 sum += mes.size();
94 }
95 }
96
97 return sum;
98 }
99
Jian Li791dd322017-04-20 00:15:56 +0900100 @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 Li252750d2017-03-01 04:50:13 +0900115 /**
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 Li92919592017-02-27 17:10:47 +0900200 }
201
202 @Override
203 public MappingEntry getMappingEntry(Type type, Mapping mapping) {
Jian Li252750d2017-03-01 04:50:13 +0900204 return getMappingEntryInternal(type, mapping.deviceId(), mapping);
Jian Li92919592017-02-27 17:10:47 +0900205 }
206
207 @Override
208 public Iterable<MappingEntry> getMappingEntries(Type type, DeviceId deviceId) {
Jian Li252750d2017-03-01 04:50:13 +0900209 // 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 Lia1f960b2017-04-25 19:12:53 +0900215 public void storeMapping(Type type, MappingEntry mapping) {
Jian Li252750d2017-03-01 04:50:13 +0900216
217 List<StoredMappingEntry> entries =
218 getMappingEntriesInternal(type, mapping.deviceId(), mapping.id());
219
220 synchronized (entries) {
221 if (!entries.contains(mapping)) {
Jian Lia1f960b2017-04-25 19:12:53 +0900222 StoredMappingEntry entry =
223 new DefaultMappingEntry(mapping, mapping.state());
Jian Li252750d2017-03-01 04:50:13 +0900224 entries.add(entry);
225 }
226 }
Jian Li92919592017-02-27 17:10:47 +0900227 }
228
229 @Override
Jian Li2dc9f002017-03-03 04:26:31 +0900230 public void pendingDeleteMapping(Type type, Mapping mapping) {
Jian Li92919592017-02-27 17:10:47 +0900231
Jian Li252750d2017-03-01 04:50:13 +0900232 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 Li92919592017-02-27 17:10:47 +0900244 }
245
246 @Override
247 public MappingEvent addOrUpdateMappingEntry(Type type, MappingEntry entry) {
Jian Li252750d2017-03-01 04:50:13 +0900248
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 Lia1f960b2017-04-25 19:12:53 +0900255 stored.setState(MappingEntryState.ADDED);
Jian Li252750d2017-03-01 04:50:13 +0900256 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 Li92919592017-02-27 17:10:47 +0900265 return null;
266 }
267
268 @Override
Jian Li2dc9f002017-03-03 04:26:31 +0900269 public MappingEvent removeMapping(Type type, Mapping mapping) {
Jian Li252750d2017-03-01 04:50:13 +0900270
271 List<StoredMappingEntry> entries =
Jian Li2dc9f002017-03-03 04:26:31 +0900272 getMappingEntriesInternal(type, mapping.deviceId(), mapping.id());
Jian Li252750d2017-03-01 04:50:13 +0900273 synchronized (entries) {
Jian Li2dc9f002017-03-03 04:26:31 +0900274 if (entries.remove(mapping)) {
275 return new MappingEvent(MAPPING_REMOVED, mapping);
Jian Li252750d2017-03-01 04:50:13 +0900276 }
277 }
Jian Li92919592017-02-27 17:10:47 +0900278 return null;
279 }
280
281 @Override
282 public MappingEvent pendingMappingEntry(Type type, MappingEntry entry) {
Jian Li252750d2017-03-01 04:50:13 +0900283 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 Li92919592017-02-27 17:10:47 +0900295 return null;
296 }
297
298 @Override
Jian Li252750d2017-03-01 04:50:13 +0900299 public void purgeMappingEntry(Type type, DeviceId deviceId) {
300 getMappingStore(type).remove(deviceId);
301 }
Jian Li92919592017-02-27 17:10:47 +0900302
Jian Li252750d2017-03-01 04:50:13 +0900303 @Override
304 public void purgeMappingEntries(Type type) {
305 getMappingStore(type).clear();
Jian Li92919592017-02-27 17:10:47 +0900306 }
307}
308
309