Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 15 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.ApplicationIdStore; |
| 23 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.ConcurrentHashMap; |
| 28 | import 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 |
| 36 | public 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 |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 54 | public ApplicationId getAppId(String name) { |
| 55 | return appIdsByName.get(name); |
| 56 | } |
| 57 | |
| 58 | @Override |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 59 | 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 | } |