blob: b8524650f784aad2abfd71ba509ce3e00662b206 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
16package org.onlab.onos.store.trivial.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Service;
21import org.onlab.onos.core.ApplicationId;
22import org.onlab.onos.core.ApplicationIdStore;
23import org.onlab.onos.core.DefaultApplicationId;
24
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() {
45 return ImmutableSet.<ApplicationId>copyOf(appIds.values());
46 }
47
48 @Override
49 public ApplicationId getAppId(Short id) {
50 return appIds.get(id);
51 }
52
53 @Override
54 public ApplicationId registerApplication(String name) {
55 DefaultApplicationId appId = appIdsByName.get(name);
56 if (appId == null) {
57 short id = (short) ID_DISPENSER.getAndIncrement();
58 appId = new DefaultApplicationId(id, name);
59 appIds.put(id, appId);
60 appIdsByName.put(name, appId);
61 }
62 return appId;
63 }
64
65}