e54b0ee5ca8bbe9eaab72cf3dbea18843a5901af
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.store.core.impl;
17
18 import static org.slf4j.LoggerFactory.getLogger;
19
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ScheduledExecutorService;
23
24 import org.apache.felix.scr.annotations.Activate;
25 import org.apache.felix.scr.annotations.Component;
26 import org.apache.felix.scr.annotations.Deactivate;
27 import org.apache.felix.scr.annotations.Reference;
28 import org.apache.felix.scr.annotations.ReferenceCardinality;
29 import org.apache.felix.scr.annotations.Service;
30 import org.onlab.util.KryoNamespace;
31 import org.onlab.util.Tools;
32 import org.onosproject.core.ApplicationId;
33 import org.onosproject.core.ApplicationIdStore;
34 import org.onosproject.core.DefaultApplicationId;
35 import org.onosproject.store.serializers.KryoNamespaces;
36 import org.onosproject.store.service.AtomicCounter;
37 import org.onosproject.store.service.ConsistentMap;
38 import org.onosproject.store.service.Serializer;
39 import org.onosproject.store.service.StorageException;
40 import org.onosproject.store.service.StorageService;
41 import org.onosproject.store.service.Versioned;
42 import org.slf4j.Logger;
43
44 import com.google.common.collect.ImmutableSet;
45 import 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
53 public class ConsistentApplicationIdStore implements ApplicationIdStore {
54
55     private final Logger log = getLogger(getClass());
56
57     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58     protected StorageService storageService;
59
60     private AtomicCounter appIdCounter;
61     private ConsistentMap<String, ApplicationId> registeredIds;
62     private Map<String, ApplicationId> nameToAppIdCache = Maps.newConcurrentMap();
63     private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap();
64     private ScheduledExecutorService executor;
65
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()
76                                       .build();
77
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() {
91         executor.shutdown();
92         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) {
127                 int id = Tools.retryable(appIdCounter::incrementAndGet, StorageException.class, 1, 2000)
128                               .get()
129                               .intValue();
130                 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 }