blob: 4ec6a23b765160579962607b299ce64bc70548b0 [file] [log] [blame]
Madan Jampani78be2492016-06-03 23:27:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Madan Jampani78be2492016-06-03 23:27:07 -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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
sangyun-han6d33e802016-08-05 13:36:33 +090020import org.onosproject.app.ApplicationIdStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.onosproject.core.ApplicationId;
Madan Jampani78be2492016-06-03 23:27:07 -070022import org.onosproject.core.DefaultApplicationId;
23import org.onosproject.store.serializers.KryoNamespaces;
24import org.onosproject.store.service.AtomicCounter;
25import org.onosproject.store.service.ConsistentMap;
26import org.onosproject.store.service.MapEvent;
27import org.onosproject.store.service.MapEventListener;
28import org.onosproject.store.service.Serializer;
29import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
Madan Jampani78be2492016-06-03 23:27:07 -070035import org.slf4j.Logger;
36
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037import java.util.Map;
38import java.util.Set;
Madan Jampani78be2492016-06-03 23:27:07 -070039
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani78be2492016-06-03 23:27:07 -070041
42/**
43 * ApplicationIdStore implementation on top of {@code AtomicCounter}
44 * and {@code ConsistentMap} primitives.
45 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046@Component(immediate = true, service = ApplicationIdStore.class)
Madan Jampani78be2492016-06-03 23:27:07 -070047public class DistributedApplicationIdStore implements ApplicationIdStore {
48
49 private final Logger log = getLogger(getClass());
50
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Madan Jampani78be2492016-06-03 23:27:07 -070052 protected StorageService storageService;
53
54 private AtomicCounter appIdCounter;
55 private ConsistentMap<String, ApplicationId> registeredIds;
56 private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap();
57 private MapEventListener<String, ApplicationId> mapEventListener = event -> {
58 if (event.type() == MapEvent.Type.INSERT) {
59 idToAppIdCache.put(event.newValue().value().id(), event.newValue().value());
60 }
61 };
62
63 @Activate
64 public void activate() {
65 appIdCounter = storageService.getAtomicCounter("onos-app-id-counter");
66
67 registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
68 .withName("onos-app-ids")
69 .withSerializer(Serializer.using(KryoNamespaces.API))
70 .withRelaxedReadConsistency()
71 .build();
72
73 primeIdToAppIdCache();
74 registeredIds.addListener(mapEventListener);
75
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 registeredIds.removeListener(mapEventListener);
82 log.info("Stopped");
83 }
84
85 @Override
86 public Set<ApplicationId> getAppIds() {
87 return ImmutableSet.copyOf(registeredIds.asJavaMap().values());
88 }
89
90 @Override
91 public ApplicationId getAppId(Short id) {
92 if (!idToAppIdCache.containsKey(id)) {
93 primeIdToAppIdCache();
94 }
95 return idToAppIdCache.get(id);
96 }
97
98 @Override
99 public ApplicationId getAppId(String name) {
100 return registeredIds.asJavaMap().get(name);
101 }
102
103 @Override
104 public ApplicationId registerApplication(String name) {
Madan Jampani6c02d9e2016-05-24 15:09:47 -0700105 ApplicationId exisitingAppId = registeredIds.asJavaMap().get(name);
106 if (exisitingAppId == null) {
107 ApplicationId newAppId = new DefaultApplicationId((int) appIdCounter.incrementAndGet(), name);
108 exisitingAppId = registeredIds.asJavaMap().putIfAbsent(name, newAppId);
109 return exisitingAppId == null ? newAppId : exisitingAppId;
110 } else {
111 return exisitingAppId;
112 }
Madan Jampani78be2492016-06-03 23:27:07 -0700113 }
114
115 private void primeIdToAppIdCache() {
116 registeredIds.asJavaMap()
117 .values()
118 .forEach(appId -> {
119 idToAppIdCache.putIfAbsent(appId.id(), appId);
120 });
121 }
122}