blob: c98dd496cddec083d179043df2512101d17ad229 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.core.impl;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070017
Yuta HIGUCHI72569d62014-10-28 22:40:01 -070018import static org.apache.commons.lang3.concurrent.ConcurrentUtils.putIfAbsent;
19
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070020import com.google.common.collect.ImmutableSet;
21import com.hazelcast.core.EntryEvent;
22import com.hazelcast.core.EntryListener;
23import com.hazelcast.core.IAtomicLong;
24import com.hazelcast.core.MapEvent;
Yuta HIGUCHId1a63e92014-12-02 13:14:28 -080025
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.ApplicationIdStore;
32import org.onosproject.core.DefaultApplicationId;
33import org.onosproject.store.hz.AbstractHazelcastStore;
34import org.onosproject.store.hz.SMap;
35import org.onosproject.store.serializers.KryoNamespaces;
36import org.onosproject.store.serializers.KryoSerializer;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070037import org.onlab.util.KryoNamespace;
38
39import java.util.Map;
40import java.util.Set;
41import java.util.concurrent.ConcurrentHashMap;
42
43/**
44 * Simple implementation of the application ID registry using in-memory
45 * structures.
46 */
Madan Jampanif4e724f2015-05-01 19:19:04 -070047@Component(immediate = false, enabled = false)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070048@Service
49public class DistributedApplicationIdStore
50 extends AbstractHazelcastStore<AppIdEvent, AppIdStoreDelegate>
51 implements ApplicationIdStore {
52
53 protected IAtomicLong lastAppId;
54 protected SMap<String, DefaultApplicationId> appIdsByName;
55
56 protected Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
57
Yuta HIGUCHId1a63e92014-12-02 13:14:28 -080058 private String listenerId;
59
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070060
61 @Override
62 @Activate
63 public void activate() {
64 super.activate();
65
66 this.serializer = new KryoSerializer() {
67 @Override
68 protected void setupKryoPool() {
69 serializerPool = KryoNamespace.newBuilder()
70 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080071 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
72 .build();
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070073 }
74 };
75
76 lastAppId = theInstance.getAtomicLong("applicationId");
77
78 appIdsByName = new SMap<>(theInstance.<byte[], byte[]>getMap("appIdsByName"), this.serializer);
Yuta HIGUCHId1a63e92014-12-02 13:14:28 -080079 listenerId = appIdsByName.addEntryListener((new RemoteAppIdEventHandler()), true);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070080
81 primeAppIds();
82
83 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
Yuta HIGUCHId1a63e92014-12-02 13:14:28 -080088 appIdsByName.removeEntryListener(listenerId);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070089 log.info("Stopped");
90 }
91
92 @Override
93 public Set<ApplicationId> getAppIds() {
94 return ImmutableSet.<ApplicationId>copyOf(appIds.values());
95 }
96
97 @Override
98 public ApplicationId getAppId(Short id) {
99 ApplicationId appId = appIds.get(id);
100 if (appId == null) {
101 primeAppIds();
HIGUCHI Yuta90543152015-02-26 22:03:50 -0800102 return appIds.get(id);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700103 }
104 return appId;
105 }
106
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800107 @Override
108 public ApplicationId getAppId(String name) {
109 return appIdsByName.get(name);
110 }
111
Yuta HIGUCHI72569d62014-10-28 22:40:01 -0700112 private void primeAppIds() {
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700113 for (DefaultApplicationId appId : appIdsByName.values()) {
HIGUCHI Yuta90543152015-02-26 22:03:50 -0800114 appIds.putIfAbsent(appId.id(), appId);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700115 }
116 }
117
118 @Override
Yuta HIGUCHI72569d62014-10-28 22:40:01 -0700119 public ApplicationId registerApplication(String name) {
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700120 DefaultApplicationId appId = appIdsByName.get(name);
121 if (appId == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800122 int id = (int) lastAppId.getAndIncrement();
Yuta HIGUCHI72569d62014-10-28 22:40:01 -0700123 appId = putIfAbsent(appIdsByName, name,
124 new DefaultApplicationId(id, name));
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700125 }
126 return appId;
127 }
128
129 private class RemoteAppIdEventHandler implements EntryListener<String, DefaultApplicationId> {
130 @Override
131 public void entryAdded(EntryEvent<String, DefaultApplicationId> event) {
132 DefaultApplicationId appId = event.getValue();
133 appIds.put(appId.id(), appId);
134 }
135
136 @Override
137 public void entryRemoved(EntryEvent<String, DefaultApplicationId> event) {
138 }
139
140 @Override
141 public void entryUpdated(EntryEvent<String, DefaultApplicationId> event) {
142 entryAdded(event);
143 }
144
145 @Override
146 public void entryEvicted(EntryEvent<String, DefaultApplicationId> event) {
147 }
148
149 @Override
150 public void mapEvicted(MapEvent event) {
151 }
152
153 @Override
154 public void mapCleared(MapEvent event) {
155 }
156 }
157}