blob: 16f599b14003b4a31452e970c1c04725dacbcc01 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070015 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070017
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
sangyun-han6d33e802016-08-05 13:36:33 +090022import org.onosproject.app.ApplicationIdStore;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.core.DefaultApplicationId;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070024
25import java.util.Map;
26import java.util.Set;
27import java.util.concurrent.ConcurrentHashMap;
28import java.util.concurrent.atomic.AtomicInteger;
29
30/**
31 * Simple implementation of the application ID registry using in-memory
32 * structures.
33 */
34@Component(immediate = true)
35@Service
36public class SimpleApplicationIdStore implements ApplicationIdStore {
37
38 private static final AtomicInteger ID_DISPENSER = new AtomicInteger(1);
39
40 private final Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
41 private final Map<String, DefaultApplicationId> appIdsByName = new ConcurrentHashMap<>();
42
43 @Override
44 public Set<ApplicationId> getAppIds() {
Sho SHIMIZU21d00692016-08-15 11:15:28 -070045 return ImmutableSet.copyOf(appIds.values());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070046 }
47
48 @Override
49 public ApplicationId getAppId(Short id) {
50 return appIds.get(id);
51 }
52
53 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -080054 public ApplicationId getAppId(String name) {
55 return appIdsByName.get(name);
56 }
57
58 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070059 public ApplicationId registerApplication(String name) {
60 DefaultApplicationId appId = appIdsByName.get(name);
61 if (appId == null) {
62 short id = (short) ID_DISPENSER.getAndIncrement();
63 appId = new DefaultApplicationId(id, name);
64 appIds.put(id, appId);
65 appIdsByName.put(name, appId);
66 }
67 return appId;
68 }
69
70}