blob: a2428220a317ba570511fb31ccda9798dcde4803 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.onlab.onos.store.trivial.impl;
20
21import com.google.common.collect.ImmutableSet;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Service;
24import org.onlab.onos.core.ApplicationId;
25import org.onlab.onos.core.ApplicationIdStore;
26import org.onlab.onos.core.DefaultApplicationId;
27
28import java.util.Map;
29import java.util.Set;
30import java.util.concurrent.ConcurrentHashMap;
31import java.util.concurrent.atomic.AtomicInteger;
32
33/**
34 * Simple implementation of the application ID registry using in-memory
35 * structures.
36 */
37@Component(immediate = true)
38@Service
39public class SimpleApplicationIdStore implements ApplicationIdStore {
40
41 private static final AtomicInteger ID_DISPENSER = new AtomicInteger(1);
42
43 private final Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
44 private final Map<String, DefaultApplicationId> appIdsByName = new ConcurrentHashMap<>();
45
46 @Override
47 public Set<ApplicationId> getAppIds() {
48 return ImmutableSet.<ApplicationId>copyOf(appIds.values());
49 }
50
51 @Override
52 public ApplicationId getAppId(Short id) {
53 return appIds.get(id);
54 }
55
56 @Override
57 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}