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