blob: acbaa2a477df73ad203002eaf750b5b83ed2345b [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
sangyun-han6d33e802016-08-05 13:36:33 +090019import org.onosproject.app.ApplicationIdStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.DefaultApplicationId;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.osgi.service.component.annotations.Component;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070023
24import java.util.Map;
25import java.util.Set;
26import java.util.concurrent.ConcurrentHashMap;
27import java.util.concurrent.atomic.AtomicInteger;
28
29/**
30 * Simple implementation of the application ID registry using in-memory
31 * structures.
32 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033@Component(immediate = true, service = ApplicationIdStore.class)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070034public class SimpleApplicationIdStore implements ApplicationIdStore {
35
36 private static final AtomicInteger ID_DISPENSER = new AtomicInteger(1);
37
38 private final Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
39 private final Map<String, DefaultApplicationId> appIdsByName = new ConcurrentHashMap<>();
40
41 @Override
42 public Set<ApplicationId> getAppIds() {
Sho SHIMIZU21d00692016-08-15 11:15:28 -070043 return ImmutableSet.copyOf(appIds.values());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070044 }
45
46 @Override
47 public ApplicationId getAppId(Short id) {
48 return appIds.get(id);
49 }
50
51 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -080052 public ApplicationId getAppId(String name) {
53 return appIdsByName.get(name);
54 }
55
56 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070057 public ApplicationId registerApplication(String name) {
58 DefaultApplicationId appId = appIdsByName.get(name);
59 if (appId == null) {
60 short id = (short) ID_DISPENSER.getAndIncrement();
61 appId = new DefaultApplicationId(id, name);
62 appIds.put(id, appId);
63 appIdsByName.put(name, appId);
64 }
65 return appId;
66 }
67
68}