blob: 17b403f42148ff0b80d12069f1569d5cdb0c70b1 [file] [log] [blame]
Jian Li92919592017-02-27 17:10:47 +09001/*
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 */
16package org.onosproject.mapping.impl;
17
Jian Li252750d2017-03-01 04:50:13 +090018import com.google.common.collect.FluentIterable;
Jian Li92919592017-02-27 17:10:47 +090019import com.google.common.collect.Maps;
Jian Li92919592017-02-27 17:10:47 +090020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Jian Li92919592017-02-27 17:10:47 +090023import org.apache.felix.scr.annotations.Modified;
Jian Li252750d2017-03-01 04:50:13 +090024import org.apache.felix.scr.annotations.Service;
25import org.onosproject.mapping.DefaultMappingEntry;
Jian Li92919592017-02-27 17:10:47 +090026import org.onosproject.mapping.Mapping;
Jian Li252750d2017-03-01 04:50:13 +090027import org.onosproject.mapping.MappingEntry;
28import org.onosproject.mapping.MappingEvent;
Jian Li92919592017-02-27 17:10:47 +090029import org.onosproject.mapping.MappingId;
30import org.onosproject.mapping.MappingStore;
Jian Li92919592017-02-27 17:10:47 +090031import org.onosproject.mapping.MappingStoreDelegate;
Jian Li252750d2017-03-01 04:50:13 +090032import org.onosproject.mapping.StoredMappingEntry;
Jian Li92919592017-02-27 17:10:47 +090033import org.onosproject.net.DeviceId;
34import org.onosproject.store.AbstractStore;
35import org.osgi.service.component.ComponentContext;
36import org.slf4j.Logger;
37
Jian Li252750d2017-03-01 04:50:13 +090038import java.util.Collections;
Jian Li92919592017-02-27 17:10:47 +090039import java.util.List;
40import java.util.concurrent.ConcurrentMap;
Jian Li252750d2017-03-01 04:50:13 +090041import java.util.concurrent.CopyOnWriteArrayList;
Jian Li92919592017-02-27 17:10:47 +090042
Jian Li252750d2017-03-01 04:50:13 +090043import static org.onosproject.mapping.MappingEntry.MappingEntryState.PENDING_ADD;
44import static org.onosproject.mapping.MappingEntry.MappingEntryState.PENDING_REMOVE;
45import static org.onosproject.mapping.MappingEvent.Type.MAPPING_ADDED;
46import static org.onosproject.mapping.MappingEvent.Type.MAPPING_REMOVED;
47import static org.onosproject.mapping.MappingEvent.Type.MAPPING_UPDATED;
Jian Li92919592017-02-27 17:10:47 +090048import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Manages inventory of mappings using trivial in-memory implementation.
52 */
53@Component(immediate = true)
54@Service
55public 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
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 Li92919592017-02-27 17:10:47 +0900199 }
200
201 @Override
202 public MappingEntry getMappingEntry(Type type, Mapping mapping) {
Jian Li252750d2017-03-01 04:50:13 +0900203 return getMappingEntryInternal(type, mapping.deviceId(), mapping);
Jian Li92919592017-02-27 17:10:47 +0900204 }
205
206 @Override
207 public Iterable<MappingEntry> getMappingEntries(Type type, DeviceId deviceId) {
Jian Li252750d2017-03-01 04:50:13 +0900208 // 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 Li92919592017-02-27 17:10:47 +0900225 }
226
227 @Override
228 public void deleteMapping(Type type, Mapping mapping) {
229
Jian Li252750d2017-03-01 04:50:13 +0900230 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 Li92919592017-02-27 17:10:47 +0900242 }
243
244 @Override
245 public MappingEvent addOrUpdateMappingEntry(Type type, MappingEntry entry) {
Jian Li252750d2017-03-01 04:50:13 +0900246
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 Li92919592017-02-27 17:10:47 +0900263 return null;
264 }
265
266 @Override
267 public MappingEvent removeMappingEntry(Type type, MappingEntry entry) {
Jian Li252750d2017-03-01 04:50:13 +0900268
269 List<StoredMappingEntry> entries =
270 getMappingEntriesInternal(type, entry.deviceId(), entry.id());
271 synchronized (entries) {
272 if (entries.remove(entry)) {
273 return new MappingEvent(MAPPING_REMOVED, entry);
274 }
275 }
Jian Li92919592017-02-27 17:10:47 +0900276 return null;
277 }
278
279 @Override
280 public MappingEvent pendingMappingEntry(Type type, MappingEntry entry) {
Jian Li252750d2017-03-01 04:50:13 +0900281 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 Li92919592017-02-27 17:10:47 +0900293 return null;
294 }
295
296 @Override
Jian Li252750d2017-03-01 04:50:13 +0900297 public void purgeMappingEntry(Type type, DeviceId deviceId) {
298 getMappingStore(type).remove(deviceId);
299 }
Jian Li92919592017-02-27 17:10:47 +0900300
Jian Li252750d2017-03-01 04:50:13 +0900301 @Override
302 public void purgeMappingEntries(Type type) {
303 getMappingStore(type).clear();
Jian Li92919592017-02-27 17:10:47 +0900304 }
305}
306
307