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