2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.core.impl;
18 import static org.slf4j.LoggerFactory.getLogger;
22 import java.util.concurrent.ScheduledExecutorService;
24 import org.apache.felix.scr.annotations.Activate;
25 import org.apache.felix.scr.annotations.Component;
26 import org.apache.felix.scr.annotations.Deactivate;
27 import org.apache.felix.scr.annotations.Reference;
28 import org.apache.felix.scr.annotations.ReferenceCardinality;
29 import org.apache.felix.scr.annotations.Service;
30 import org.onlab.util.KryoNamespace;
31 import org.onlab.util.Tools;
32 import org.onosproject.core.ApplicationId;
33 import org.onosproject.core.ApplicationIdStore;
34 import org.onosproject.core.DefaultApplicationId;
35 import org.onosproject.store.serializers.KryoNamespaces;
36 import org.onosproject.store.service.AtomicCounter;
37 import org.onosproject.store.service.ConsistentMap;
38 import org.onosproject.store.service.Serializer;
39 import org.onosproject.store.service.StorageException;
40 import org.onosproject.store.service.StorageService;
41 import org.onosproject.store.service.Versioned;
42 import org.slf4j.Logger;
44 import com.google.common.collect.ImmutableSet;
45 import com.google.common.collect.Maps;
48 * ApplicationIdStore implementation on top of {@code AtomicCounter}
49 * and {@code ConsistentMap} primitives.
51 @Component(immediate = true, enabled = true)
53 public class ConsistentApplicationIdStore implements ApplicationIdStore {
55 private final Logger log = getLogger(getClass());
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected StorageService storageService;
60 private AtomicCounter appIdCounter;
61 private ConsistentMap<String, ApplicationId> registeredIds;
62 private Map<String, ApplicationId> nameToAppIdCache = Maps.newConcurrentMap();
63 private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap();
64 private ScheduledExecutorService executor;
66 private static final Serializer SERIALIZER = Serializer.using(new KryoNamespace.Builder()
67 .register(KryoNamespaces.API)
68 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
72 public void activate() {
73 appIdCounter = storageService.atomicCounterBuilder()
74 .withName("onos-app-id-counter")
75 .withPartitionsDisabled()
78 registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
79 .withName("onos-app-ids")
80 .withPartitionsDisabled()
81 .withSerializer(SERIALIZER)
90 public void deactivate() {
96 public Set<ApplicationId> getAppIds() {
97 // TODO: Rework this when we have notification support in ConsistentMap.
99 return ImmutableSet.copyOf(nameToAppIdCache.values());
103 public ApplicationId getAppId(Short id) {
104 if (!idToAppIdCache.containsKey(id)) {
107 return idToAppIdCache.get(id);
111 public ApplicationId getAppId(String name) {
112 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
113 Versioned<ApplicationId> existingAppId = registeredIds.get(key);
114 return existingAppId != null ? existingAppId.value() : null;
117 idToAppIdCache.putIfAbsent(appId.id(), appId);
123 public ApplicationId registerApplication(String name) {
124 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
125 Versioned<ApplicationId> existingAppId = registeredIds.get(name);
126 if (existingAppId == null) {
127 int id = Tools.retryable(appIdCounter::incrementAndGet, StorageException.class, 1, 2000)
130 DefaultApplicationId newAppId = new DefaultApplicationId(id, name);
131 existingAppId = registeredIds.putIfAbsent(name, newAppId);
132 if (existingAppId != null) {
133 return existingAppId.value();
138 return existingAppId.value();
141 idToAppIdCache.putIfAbsent(appId.id(), appId);
145 private void primeAppIds() {
146 registeredIds.values()
148 .map(Versioned::value)
150 nameToAppIdCache.putIfAbsent(appId.name(), appId);
151 idToAppIdCache.putIfAbsent(appId.id(), appId);