blob: 1046bbabfb31d7ea6c469511a97b93d5b8a101c8 [file] [log] [blame]
Madan Jampanif4e724f2015-05-01 19:19:04 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampanif4e724f2015-05-01 19:19:04 -07003 *
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
7 *
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.
15 */
16package org.onosproject.store.core.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Map;
21import java.util.Set;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
28import org.onlab.util.KryoNamespace;
Madan Jampania29c6772015-08-17 13:17:07 -070029import org.onlab.util.Tools;
Madan Jampanif4e724f2015-05-01 19:19:04 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.ApplicationIdStore;
32import org.onosproject.core.DefaultApplicationId;
33import org.onosproject.store.serializers.KryoNamespaces;
Madan Jampania29c6772015-08-17 13:17:07 -070034import org.onosproject.store.service.AtomicCounter;
Madan Jampanif4e724f2015-05-01 19:19:04 -070035import org.onosproject.store.service.ConsistentMap;
36import org.onosproject.store.service.Serializer;
Madan Jampania29c6772015-08-17 13:17:07 -070037import org.onosproject.store.service.StorageException;
Madan Jampanif4e724f2015-05-01 19:19:04 -070038import org.onosproject.store.service.StorageService;
39import org.onosproject.store.service.Versioned;
40import org.slf4j.Logger;
41
42import com.google.common.collect.ImmutableSet;
43import com.google.common.collect.Maps;
44
45/**
46 * ApplicationIdStore implementation on top of {@code AtomicCounter}
47 * and {@code ConsistentMap} primitives.
48 */
49@Component(immediate = true, enabled = true)
50@Service
51public class ConsistentApplicationIdStore implements ApplicationIdStore {
52
53 private final Logger log = getLogger(getClass());
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected StorageService storageService;
57
Madan Jampania29c6772015-08-17 13:17:07 -070058 private AtomicCounter appIdCounter;
Madan Jampanif4e724f2015-05-01 19:19:04 -070059 private ConsistentMap<String, ApplicationId> registeredIds;
60 private Map<String, ApplicationId> nameToAppIdCache = Maps.newConcurrentMap();
61 private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap();
62
63 private static final Serializer SERIALIZER = Serializer.using(new KryoNamespace.Builder()
64 .register(KryoNamespaces.API)
65 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
66 .build());
67
68 @Activate
69 public void activate() {
70 appIdCounter = storageService.atomicCounterBuilder()
71 .withName("onos-app-id-counter")
Madan Jampanie17d3282016-02-03 15:30:57 -080072 .build()
73 .asAtomicCounter();
Madan Jampanif4e724f2015-05-01 19:19:04 -070074
75 registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
76 .withName("onos-app-ids")
Madan Jampanif4e724f2015-05-01 19:19:04 -070077 .withSerializer(SERIALIZER)
78 .build();
79
80 primeAppIds();
81
82 log.info("Started");
83 }
84
85 @Deactivate
86 public void deactivate() {
87 log.info("Stopped");
88 }
89
90 @Override
91 public Set<ApplicationId> getAppIds() {
92 // TODO: Rework this when we have notification support in ConsistentMap.
93 primeAppIds();
94 return ImmutableSet.copyOf(nameToAppIdCache.values());
95 }
96
97 @Override
98 public ApplicationId getAppId(Short id) {
99 if (!idToAppIdCache.containsKey(id)) {
100 primeAppIds();
101 }
102 return idToAppIdCache.get(id);
103 }
104
105 @Override
106 public ApplicationId getAppId(String name) {
107 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
108 Versioned<ApplicationId> existingAppId = registeredIds.get(key);
109 return existingAppId != null ? existingAppId.value() : null;
110 });
111 if (appId != null) {
112 idToAppIdCache.putIfAbsent(appId.id(), appId);
113 }
114 return appId;
115 }
116
117 @Override
118 public ApplicationId registerApplication(String name) {
119 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
120 Versioned<ApplicationId> existingAppId = registeredIds.get(name);
121 if (existingAppId == null) {
Madan Jampania29c6772015-08-17 13:17:07 -0700122 int id = Tools.retryable(appIdCounter::incrementAndGet, StorageException.class, 1, 2000)
123 .get()
124 .intValue();
Madan Jampanif4e724f2015-05-01 19:19:04 -0700125 DefaultApplicationId newAppId = new DefaultApplicationId(id, name);
126 existingAppId = registeredIds.putIfAbsent(name, newAppId);
127 if (existingAppId != null) {
128 return existingAppId.value();
129 } else {
130 return newAppId;
131 }
132 } else {
133 return existingAppId.value();
134 }
135 });
136 idToAppIdCache.putIfAbsent(appId.id(), appId);
137 return appId;
138 }
139
140 private void primeAppIds() {
141 registeredIds.values()
142 .stream()
143 .map(Versioned::value)
144 .forEach(appId -> {
145 nameToAppIdCache.putIfAbsent(appId.name(), appId);
146 idToAppIdCache.putIfAbsent(appId.id(), appId);
147 });
148 }
149}