blob: 8b028683b43218b504420b01a431755b46b80f10 [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 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 Li92919592017-02-27 17:10:47 +090021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Jian Li92919592017-02-27 17:10:47 +090024import org.apache.felix.scr.annotations.Modified;
Jian Li252750d2017-03-01 04:50:13 +090025import org.apache.felix.scr.annotations.Service;
26import org.onosproject.mapping.DefaultMappingEntry;
Jian Li92919592017-02-27 17:10:47 +090027import org.onosproject.mapping.Mapping;
Jian Li252750d2017-03-01 04:50:13 +090028import org.onosproject.mapping.MappingEntry;
29import org.onosproject.mapping.MappingEvent;
Jian Li92919592017-02-27 17:10:47 +090030import org.onosproject.mapping.MappingId;
31import org.onosproject.mapping.MappingStore;
Jian Li92919592017-02-27 17:10:47 +090032import org.onosproject.mapping.MappingStoreDelegate;
Jian Li252750d2017-03-01 04:50:13 +090033import org.onosproject.mapping.StoredMappingEntry;
Jian Li92919592017-02-27 17:10:47 +090034import org.onosproject.net.DeviceId;
35import org.onosproject.store.AbstractStore;
36import org.osgi.service.component.ComponentContext;
37import 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 */
54@Component(immediate = true)
55@Service
56public class SimpleMappingStore
57 extends AbstractStore<MappingEvent, MappingStoreDelegate>
58 implements MappingStore {
59
60 private final Logger log = getLogger(getClass());
61
Jian Li252750d2017-03-01 04:50:13 +090062 private static final String UNRECOGNIZED_STORE_MSG = "Unrecognized store type {}";
Jian Li92919592017-02-27 17:10:47 +090063
Jian Li252750d2017-03-01 04:50:13 +090064 private final ConcurrentMap<DeviceId, ConcurrentMap<MappingId,
65 List<StoredMappingEntry>>> mapDbStore = Maps.newConcurrentMap();
Jian Li92919592017-02-27 17:10:47 +090066
Jian Li252750d2017-03-01 04:50:13 +090067 private final ConcurrentMap<DeviceId, ConcurrentMap<MappingId,
68 List<StoredMappingEntry>>> mapCacheStore = Maps.newConcurrentMap();
Jian Li92919592017-02-27 17:10:47 +090069
70 @Activate
71 public void activate() {
72 log.info("Started");
73 }
74
75 @Deactivate
76 public void deactivate() {
77 mapDbStore.clear();
78 mapCacheStore.clear();
79 log.info("Stopped");
80 }
81
82 @Modified
83 public void modified(ComponentContext context) {
84
85 }
86
87 @Override
88 public int getMappingCount(Type type) {
Jian Li252750d2017-03-01 04:50:13 +090089 int sum = 0;
90
Jian Li791dd322017-04-20 00:15:56 +090091 for (ConcurrentMap<MappingId, List<StoredMappingEntry>> mapDb :
92 getMappingStore(type).values()) {
Jian Li252750d2017-03-01 04:50:13 +090093 for (List<StoredMappingEntry> mes : mapDb.values()) {
94 sum += mes.size();
95 }
96 }
97
98 return sum;
99 }
100
Jian Li791dd322017-04-20 00:15:56 +0900101 @Override
102 public Iterable<MappingEntry> getAllMappingEntries(Type type) {
103
104 List<MappingEntry> entries = Lists.newArrayList();
105
106 for (ConcurrentMap<MappingId, List<StoredMappingEntry>> mapDb :
107 getMappingStore(type).values()) {
108 for (List<StoredMappingEntry> mes : mapDb.values()) {
109 entries.addAll(mes);
110 }
111 }
112
113 return entries;
114 }
115
Jian Li252750d2017-03-01 04:50:13 +0900116 /**
117 * Obtains the mapping store for specified device and store type.
118 *
119 * @param type mapping store type
120 * @param deviceId device identifier
121 * @return Map representing Mapping Store of given device and store type
122 */
123 private ConcurrentMap<MappingId, List<StoredMappingEntry>>
124 getMappingStoreByDeviceId(Type type,
125 DeviceId deviceId) {
126 switch (type) {
127 case MAP_DATABASE:
128 return mapDbStore.computeIfAbsent(deviceId,
129 k -> Maps.newConcurrentMap());
130 case MAP_CACHE:
131 return mapCacheStore.computeIfAbsent(deviceId,
132 k -> Maps.newConcurrentMap());
133 default:
134 log.error(UNRECOGNIZED_STORE_MSG, type);
135 return null;
136 }
137 }
138
139 /**
140 * Obtains the mapping store for specified store type.
141 *
142 * @param type mapping store type
143 * @return mapping store
144 */
145 private ConcurrentMap<DeviceId, ConcurrentMap<MappingId, List<StoredMappingEntry>>>
146 getMappingStore(Type type) {
147 switch (type) {
148 case MAP_DATABASE:
149 return mapDbStore;
150 case MAP_CACHE:
151 return mapCacheStore;
152 default:
153 log.error(UNRECOGNIZED_STORE_MSG, type);
154 return null;
155 }
156 }
157
158 /**
159 * Obtains mapping entries for specified device, store type and mapping ID.
160 *
161 * @param type mapping store type
162 * @param deviceId device identifier
163 * @param mappingId mapping identifier
164 * @return a collection of mapping entries
165 */
166 private List<StoredMappingEntry> getMappingEntriesInternal(Type type,
167 DeviceId deviceId,
168 MappingId mappingId) {
169 final ConcurrentMap<MappingId, List<StoredMappingEntry>>
170 store = getMappingStoreByDeviceId(type, deviceId);
171 List<StoredMappingEntry> r = store.get(mappingId);
172 if (r == null) {
173 final List<StoredMappingEntry> concurrentlyAdded;
174 r = new CopyOnWriteArrayList<>();
175 concurrentlyAdded = store.putIfAbsent(mappingId, r);
176 if (concurrentlyAdded != null) {
177 return concurrentlyAdded;
178 }
179 }
180 return r;
181 }
182
183 /**
184 * Obtains a mapping entry for specified device, store type and mapping.
185 *
186 * @param type mapping store type
187 * @param deviceId device identifier
188 * @param mapping mapping identifier
189 * @return a mapping entry
190 */
191 private MappingEntry getMappingEntryInternal(Type type, DeviceId deviceId,
192 Mapping mapping) {
193 List<StoredMappingEntry> mes =
194 getMappingEntriesInternal(type, deviceId, mapping.id());
195 for (StoredMappingEntry me : mes) {
196 if (me.equals(mapping)) {
197 return me;
198 }
199 }
200 return null;
Jian Li92919592017-02-27 17:10:47 +0900201 }
202
203 @Override
204 public MappingEntry getMappingEntry(Type type, Mapping mapping) {
Jian Li252750d2017-03-01 04:50:13 +0900205 return getMappingEntryInternal(type, mapping.deviceId(), mapping);
Jian Li92919592017-02-27 17:10:47 +0900206 }
207
208 @Override
209 public Iterable<MappingEntry> getMappingEntries(Type type, DeviceId deviceId) {
Jian Li252750d2017-03-01 04:50:13 +0900210 // FIXME: a better way is using Java 8 API to return immutable iterables
211 return FluentIterable.from(getMappingStoreByDeviceId(type, deviceId).values())
212 .transformAndConcat(Collections::unmodifiableList);
213 }
214
215 @Override
216 public void storeMapping(Type type, Mapping mapping) {
217
218 List<StoredMappingEntry> entries =
219 getMappingEntriesInternal(type, mapping.deviceId(), mapping.id());
220
221 synchronized (entries) {
222 if (!entries.contains(mapping)) {
223 StoredMappingEntry entry = new DefaultMappingEntry(mapping);
224 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) {
255 stored.setState(MappingEntry.MappingEntryState.ADDED);
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 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