Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.core.impl; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 72569d6 | 2014-10-28 22:40:01 -0700 | [diff] [blame] | 18 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.putIfAbsent; |
| 19 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.hazelcast.core.EntryEvent; |
| 22 | import com.hazelcast.core.EntryListener; |
| 23 | import com.hazelcast.core.IAtomicLong; |
| 24 | import com.hazelcast.core.MapEvent; |
Yuta HIGUCHI | d1a63e9 | 2014-12-02 13:14:28 -0800 | [diff] [blame] | 25 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.ApplicationIdStore; |
| 32 | import org.onosproject.core.DefaultApplicationId; |
| 33 | import org.onosproject.store.hz.AbstractHazelcastStore; |
| 34 | import org.onosproject.store.hz.SMap; |
| 35 | import org.onosproject.store.serializers.KryoNamespaces; |
| 36 | import org.onosproject.store.serializers.KryoSerializer; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 37 | import org.onlab.util.KryoNamespace; |
| 38 | |
| 39 | import java.util.Map; |
| 40 | import java.util.Set; |
| 41 | import java.util.concurrent.ConcurrentHashMap; |
| 42 | |
| 43 | /** |
| 44 | * Simple implementation of the application ID registry using in-memory |
| 45 | * structures. |
| 46 | */ |
Madan Jampani | f4e724f | 2015-05-01 19:19:04 -0700 | [diff] [blame] | 47 | @Component(immediate = false, enabled = false) |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 48 | @Service |
| 49 | public 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 HIGUCHI | d1a63e9 | 2014-12-02 13:14:28 -0800 | [diff] [blame] | 58 | private String listenerId; |
| 59 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 60 | |
| 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 HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 71 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 72 | .build(); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | lastAppId = theInstance.getAtomicLong("applicationId"); |
| 77 | |
| 78 | appIdsByName = new SMap<>(theInstance.<byte[], byte[]>getMap("appIdsByName"), this.serializer); |
Yuta HIGUCHI | d1a63e9 | 2014-12-02 13:14:28 -0800 | [diff] [blame] | 79 | listenerId = appIdsByName.addEntryListener((new RemoteAppIdEventHandler()), true); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 80 | |
| 81 | primeAppIds(); |
| 82 | |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
Yuta HIGUCHI | d1a63e9 | 2014-12-02 13:14:28 -0800 | [diff] [blame] | 88 | appIdsByName.removeEntryListener(listenerId); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 89 | 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 Yuta | 9054315 | 2015-02-26 22:03:50 -0800 | [diff] [blame] | 102 | return appIds.get(id); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 103 | } |
| 104 | return appId; |
| 105 | } |
| 106 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 107 | @Override |
| 108 | public ApplicationId getAppId(String name) { |
| 109 | return appIdsByName.get(name); |
| 110 | } |
| 111 | |
Yuta HIGUCHI | 72569d6 | 2014-10-28 22:40:01 -0700 | [diff] [blame] | 112 | private void primeAppIds() { |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 113 | for (DefaultApplicationId appId : appIdsByName.values()) { |
HIGUCHI Yuta | 9054315 | 2015-02-26 22:03:50 -0800 | [diff] [blame] | 114 | appIds.putIfAbsent(appId.id(), appId); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | @Override |
Yuta HIGUCHI | 72569d6 | 2014-10-28 22:40:01 -0700 | [diff] [blame] | 119 | public ApplicationId registerApplication(String name) { |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 120 | DefaultApplicationId appId = appIdsByName.get(name); |
| 121 | if (appId == null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 122 | int id = (int) lastAppId.getAndIncrement(); |
Yuta HIGUCHI | 72569d6 | 2014-10-28 22:40:01 -0700 | [diff] [blame] | 123 | appId = putIfAbsent(appIdsByName, name, |
| 124 | new DefaultApplicationId(id, name)); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 125 | } |
| 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 | } |