blob: 34aee9b852ca2827dba671cecb8bd3fb32ba9353 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
16package org.onlab.onos.store.core.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.hazelcast.core.EntryEvent;
20import com.hazelcast.core.EntryListener;
21import com.hazelcast.core.IAtomicLong;
22import com.hazelcast.core.MapEvent;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.onos.core.ApplicationId;
28import org.onlab.onos.core.ApplicationIdStore;
29import org.onlab.onos.core.DefaultApplicationId;
30import org.onlab.onos.store.hz.AbstractHazelcastStore;
31import org.onlab.onos.store.hz.SMap;
32import org.onlab.onos.store.serializers.KryoNamespaces;
33import org.onlab.onos.store.serializers.KryoSerializer;
34import org.onlab.util.KryoNamespace;
35
36import java.util.Map;
37import java.util.Set;
38import java.util.concurrent.ConcurrentHashMap;
39
40/**
41 * Simple implementation of the application ID registry using in-memory
42 * structures.
43 */
44@Component(immediate = true)
45@Service
46public class DistributedApplicationIdStore
47 extends AbstractHazelcastStore<AppIdEvent, AppIdStoreDelegate>
48 implements ApplicationIdStore {
49
50 protected IAtomicLong lastAppId;
51 protected SMap<String, DefaultApplicationId> appIdsByName;
52
53 protected Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
54
55
56 @Override
57 @Activate
58 public void activate() {
59 super.activate();
60
61 this.serializer = new KryoSerializer() {
62 @Override
63 protected void setupKryoPool() {
64 serializerPool = KryoNamespace.newBuilder()
65 .register(KryoNamespaces.API)
66 .build()
67 .populate(1);
68 }
69 };
70
71 lastAppId = theInstance.getAtomicLong("applicationId");
72
73 appIdsByName = new SMap<>(theInstance.<byte[], byte[]>getMap("appIdsByName"), this.serializer);
74 appIdsByName.addEntryListener((new RemoteAppIdEventHandler()), true);
75
76 primeAppIds();
77
78 log.info("Started");
79 }
80
81 @Deactivate
82 public void deactivate() {
83 log.info("Stopped");
84 }
85
86 @Override
87 public Set<ApplicationId> getAppIds() {
88 return ImmutableSet.<ApplicationId>copyOf(appIds.values());
89 }
90
91 @Override
92 public ApplicationId getAppId(Short id) {
93 ApplicationId appId = appIds.get(id);
94 if (appId == null) {
95 primeAppIds();
96 }
97 return appId;
98 }
99
100 private synchronized void primeAppIds() {
101 for (DefaultApplicationId appId : appIdsByName.values()) {
102 appIds.put(appId.id(), appId);
103 }
104 }
105
106 @Override
107 public synchronized ApplicationId registerApplication(String name) {
108 DefaultApplicationId appId = appIdsByName.get(name);
109 if (appId == null) {
110 short id = (short) lastAppId.getAndIncrement();
111 appId = new DefaultApplicationId(id, name);
112 appIds.put(id, appId);
113 appIdsByName.put(name, appId);
114 }
115 return appId;
116 }
117
118 private class RemoteAppIdEventHandler implements EntryListener<String, DefaultApplicationId> {
119 @Override
120 public void entryAdded(EntryEvent<String, DefaultApplicationId> event) {
121 DefaultApplicationId appId = event.getValue();
122 appIds.put(appId.id(), appId);
123 }
124
125 @Override
126 public void entryRemoved(EntryEvent<String, DefaultApplicationId> event) {
127 }
128
129 @Override
130 public void entryUpdated(EntryEvent<String, DefaultApplicationId> event) {
131 entryAdded(event);
132 }
133
134 @Override
135 public void entryEvicted(EntryEvent<String, DefaultApplicationId> event) {
136 }
137
138 @Override
139 public void mapEvicted(MapEvent event) {
140 }
141
142 @Override
143 public void mapCleared(MapEvent event) {
144 }
145 }
146}