blob: 354ef49093f48514bcd346849818ec2f7672b5b7 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.onlab.onos.store.core.impl;
20
21import com.google.common.collect.ImmutableSet;
22import com.hazelcast.core.EntryEvent;
23import com.hazelcast.core.EntryListener;
24import com.hazelcast.core.IAtomicLong;
25import com.hazelcast.core.MapEvent;
26import 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;
30import org.onlab.onos.core.ApplicationId;
31import org.onlab.onos.core.ApplicationIdStore;
32import org.onlab.onos.core.DefaultApplicationId;
33import org.onlab.onos.store.hz.AbstractHazelcastStore;
34import org.onlab.onos.store.hz.SMap;
35import org.onlab.onos.store.serializers.KryoNamespaces;
36import org.onlab.onos.store.serializers.KryoSerializer;
37import 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 */
47@Component(immediate = true)
48@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
58
59 @Override
60 @Activate
61 public void activate() {
62 super.activate();
63
64 this.serializer = new KryoSerializer() {
65 @Override
66 protected void setupKryoPool() {
67 serializerPool = KryoNamespace.newBuilder()
68 .register(KryoNamespaces.API)
69 .build()
70 .populate(1);
71 }
72 };
73
74 lastAppId = theInstance.getAtomicLong("applicationId");
75
76 appIdsByName = new SMap<>(theInstance.<byte[], byte[]>getMap("appIdsByName"), this.serializer);
77 appIdsByName.addEntryListener((new RemoteAppIdEventHandler()), true);
78
79 primeAppIds();
80
81 log.info("Started");
82 }
83
84 @Deactivate
85 public void deactivate() {
86 log.info("Stopped");
87 }
88
89 @Override
90 public Set<ApplicationId> getAppIds() {
91 return ImmutableSet.<ApplicationId>copyOf(appIds.values());
92 }
93
94 @Override
95 public ApplicationId getAppId(Short id) {
96 ApplicationId appId = appIds.get(id);
97 if (appId == null) {
98 primeAppIds();
99 }
100 return appId;
101 }
102
103 private synchronized void primeAppIds() {
104 for (DefaultApplicationId appId : appIdsByName.values()) {
105 appIds.put(appId.id(), appId);
106 }
107 }
108
109 @Override
110 public synchronized ApplicationId registerApplication(String name) {
111 DefaultApplicationId appId = appIdsByName.get(name);
112 if (appId == null) {
113 short id = (short) lastAppId.getAndIncrement();
114 appId = new DefaultApplicationId(id, name);
115 appIds.put(id, appId);
116 appIdsByName.put(name, appId);
117 }
118 return appId;
119 }
120
121 private class RemoteAppIdEventHandler implements EntryListener<String, DefaultApplicationId> {
122 @Override
123 public void entryAdded(EntryEvent<String, DefaultApplicationId> event) {
124 DefaultApplicationId appId = event.getValue();
125 appIds.put(appId.id(), appId);
126 }
127
128 @Override
129 public void entryRemoved(EntryEvent<String, DefaultApplicationId> event) {
130 }
131
132 @Override
133 public void entryUpdated(EntryEvent<String, DefaultApplicationId> event) {
134 entryAdded(event);
135 }
136
137 @Override
138 public void entryEvicted(EntryEvent<String, DefaultApplicationId> event) {
139 }
140
141 @Override
142 public void mapEvicted(MapEvent event) {
143 }
144
145 @Override
146 public void mapCleared(MapEvent event) {
147 }
148 }
149}