blob: e54b0ee5ca8bbe9eaab72cf3dbea18843a5901af [file] [log] [blame]
Madan Jampanif4e724f2015-05-01 19:19:04 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
Madan Jampanif4d58f32015-06-05 17:38:22 -070022import java.util.concurrent.ScheduledExecutorService;
23
Madan Jampanif4e724f2015-05-01 19:19:04 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30import org.onlab.util.KryoNamespace;
Madan Jampania29c6772015-08-17 13:17:07 -070031import org.onlab.util.Tools;
Madan Jampanif4e724f2015-05-01 19:19:04 -070032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.ApplicationIdStore;
34import org.onosproject.core.DefaultApplicationId;
35import org.onosproject.store.serializers.KryoNamespaces;
Madan Jampania29c6772015-08-17 13:17:07 -070036import org.onosproject.store.service.AtomicCounter;
Madan Jampanif4e724f2015-05-01 19:19:04 -070037import org.onosproject.store.service.ConsistentMap;
38import org.onosproject.store.service.Serializer;
Madan Jampania29c6772015-08-17 13:17:07 -070039import org.onosproject.store.service.StorageException;
Madan Jampanif4e724f2015-05-01 19:19:04 -070040import org.onosproject.store.service.StorageService;
41import org.onosproject.store.service.Versioned;
42import org.slf4j.Logger;
43
44import com.google.common.collect.ImmutableSet;
45import com.google.common.collect.Maps;
46
47/**
48 * ApplicationIdStore implementation on top of {@code AtomicCounter}
49 * and {@code ConsistentMap} primitives.
50 */
51@Component(immediate = true, enabled = true)
52@Service
53public class ConsistentApplicationIdStore implements ApplicationIdStore {
54
55 private final Logger log = getLogger(getClass());
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected StorageService storageService;
59
Madan Jampania29c6772015-08-17 13:17:07 -070060 private AtomicCounter appIdCounter;
Madan Jampanif4e724f2015-05-01 19:19:04 -070061 private ConsistentMap<String, ApplicationId> registeredIds;
62 private Map<String, ApplicationId> nameToAppIdCache = Maps.newConcurrentMap();
63 private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap();
Madan Jampanif4d58f32015-06-05 17:38:22 -070064 private ScheduledExecutorService executor;
Madan Jampanif4e724f2015-05-01 19:19:04 -070065
66 private static final Serializer SERIALIZER = Serializer.using(new KryoNamespace.Builder()
67 .register(KryoNamespaces.API)
68 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
69 .build());
70
71 @Activate
72 public void activate() {
73 appIdCounter = storageService.atomicCounterBuilder()
74 .withName("onos-app-id-counter")
75 .withPartitionsDisabled()
Madan Jampania29c6772015-08-17 13:17:07 -070076 .build();
Madan Jampanif4e724f2015-05-01 19:19:04 -070077
78 registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
79 .withName("onos-app-ids")
80 .withPartitionsDisabled()
81 .withSerializer(SERIALIZER)
82 .build();
83
84 primeAppIds();
85
86 log.info("Started");
87 }
88
89 @Deactivate
90 public void deactivate() {
Madan Jampanif4d58f32015-06-05 17:38:22 -070091 executor.shutdown();
Madan Jampanif4e724f2015-05-01 19:19:04 -070092 log.info("Stopped");
93 }
94
95 @Override
96 public Set<ApplicationId> getAppIds() {
97 // TODO: Rework this when we have notification support in ConsistentMap.
98 primeAppIds();
99 return ImmutableSet.copyOf(nameToAppIdCache.values());
100 }
101
102 @Override
103 public ApplicationId getAppId(Short id) {
104 if (!idToAppIdCache.containsKey(id)) {
105 primeAppIds();
106 }
107 return idToAppIdCache.get(id);
108 }
109
110 @Override
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;
115 });
116 if (appId != null) {
117 idToAppIdCache.putIfAbsent(appId.id(), appId);
118 }
119 return appId;
120 }
121
122 @Override
123 public ApplicationId registerApplication(String name) {
124 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
125 Versioned<ApplicationId> existingAppId = registeredIds.get(name);
126 if (existingAppId == null) {
Madan Jampania29c6772015-08-17 13:17:07 -0700127 int id = Tools.retryable(appIdCounter::incrementAndGet, StorageException.class, 1, 2000)
128 .get()
129 .intValue();
Madan Jampanif4e724f2015-05-01 19:19:04 -0700130 DefaultApplicationId newAppId = new DefaultApplicationId(id, name);
131 existingAppId = registeredIds.putIfAbsent(name, newAppId);
132 if (existingAppId != null) {
133 return existingAppId.value();
134 } else {
135 return newAppId;
136 }
137 } else {
138 return existingAppId.value();
139 }
140 });
141 idToAppIdCache.putIfAbsent(appId.id(), appId);
142 return appId;
143 }
144
145 private void primeAppIds() {
146 registeredIds.values()
147 .stream()
148 .map(Versioned::value)
149 .forEach(appId -> {
150 nameToAppIdCache.putIfAbsent(appId.name(), appId);
151 idToAppIdCache.putIfAbsent(appId.id(), appId);
152 });
153 }
154}