blob: 7c3769a33d654e8f0c484394c08bae0a5e71e4f3 [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;
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")
72 .withPartitionsDisabled()
Madan Jampania29c6772015-08-17 13:17:07 -070073 .build();
Madan Jampanif4e724f2015-05-01 19:19:04 -070074
75 registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
76 .withName("onos-app-ids")
77 .withPartitionsDisabled()
78 .withSerializer(SERIALIZER)
79 .build();
80
81 primeAppIds();
82
83 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
88 log.info("Stopped");
89 }
90
91 @Override
92 public Set<ApplicationId> getAppIds() {
93 // TODO: Rework this when we have notification support in ConsistentMap.
94 primeAppIds();
95 return ImmutableSet.copyOf(nameToAppIdCache.values());
96 }
97
98 @Override
99 public ApplicationId getAppId(Short id) {
100 if (!idToAppIdCache.containsKey(id)) {
101 primeAppIds();
102 }
103 return idToAppIdCache.get(id);
104 }
105
106 @Override
107 public ApplicationId getAppId(String name) {
108 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
109 Versioned<ApplicationId> existingAppId = registeredIds.get(key);
110 return existingAppId != null ? existingAppId.value() : null;
111 });
112 if (appId != null) {
113 idToAppIdCache.putIfAbsent(appId.id(), appId);
114 }
115 return appId;
116 }
117
118 @Override
119 public ApplicationId registerApplication(String name) {
120 ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
121 Versioned<ApplicationId> existingAppId = registeredIds.get(name);
122 if (existingAppId == null) {
Madan Jampania29c6772015-08-17 13:17:07 -0700123 int id = Tools.retryable(appIdCounter::incrementAndGet, StorageException.class, 1, 2000)
124 .get()
125 .intValue();
Madan Jampanif4e724f2015-05-01 19:19:04 -0700126 DefaultApplicationId newAppId = new DefaultApplicationId(id, name);
127 existingAppId = registeredIds.putIfAbsent(name, newAppId);
128 if (existingAppId != null) {
129 return existingAppId.value();
130 } else {
131 return newAppId;
132 }
133 } else {
134 return existingAppId.value();
135 }
136 });
137 idToAppIdCache.putIfAbsent(appId.id(), appId);
138 return appId;
139 }
140
141 private void primeAppIds() {
142 registeredIds.values()
143 .stream()
144 .map(Versioned::value)
145 .forEach(appId -> {
146 nameToAppIdCache.putIfAbsent(appId.name(), appId);
147 idToAppIdCache.putIfAbsent(appId.id(), appId);
148 });
149 }
150}