tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.impl; |
| 2 | |
| 3 | import com.google.common.base.Optional; |
| 4 | import com.google.common.cache.LoadingCache; |
| 5 | import com.hazelcast.core.EntryAdapter; |
| 6 | import com.hazelcast.core.EntryEvent; |
| 7 | import com.hazelcast.core.HazelcastInstance; |
| 8 | import com.hazelcast.core.MapEvent; |
| 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Reference; |
| 12 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 13 | import org.onlab.onos.event.Event; |
| 14 | import org.onlab.onos.store.AbstractStore; |
| 15 | import org.onlab.onos.store.StoreDelegate; |
| 16 | import org.onlab.onos.store.common.StoreService; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 17 | import org.slf4j.Logger; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | /** |
| 23 | * Abstraction of a distributed store based on Hazelcast. |
| 24 | */ |
| 25 | @Component(componentAbstract = true) |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 26 | public abstract class AbstractDistributedStore<E extends Event, D extends StoreDelegate<E>> |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 27 | extends AbstractStore<E, D> { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 28 | |
| 29 | protected final Logger log = getLogger(getClass()); |
| 30 | |
| 31 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 32 | protected StoreService storeService; |
| 33 | |
| 34 | protected HazelcastInstance theInstance; |
| 35 | |
| 36 | @Activate |
| 37 | public void activate() { |
| 38 | theInstance = storeService.getHazelcastInstance(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Serializes the specified object using the backing store service. |
| 43 | * |
| 44 | * @param obj object to be serialized |
| 45 | * @return serialized object |
| 46 | */ |
| 47 | protected byte[] serialize(Object obj) { |
| 48 | return storeService.serialize(obj); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Deserializes the specified object using the backing store service. |
| 53 | * |
| 54 | * @param bytes bytes to be deserialized |
| 55 | * @param <T> type of object |
| 56 | * @return deserialized object |
| 57 | */ |
| 58 | protected <T> T deserialize(byte[] bytes) { |
| 59 | return storeService.deserialize(bytes); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * An IMap entry listener, which reflects each remote event to the cache. |
| 65 | * |
| 66 | * @param <K> IMap key type after deserialization |
| 67 | * @param <V> IMap value type after deserialization |
| 68 | */ |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 69 | public class RemoteEventHandler<K, V> extends EntryAdapter<byte[], byte[]> { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 70 | |
| 71 | private LoadingCache<K, Optional<V>> cache; |
| 72 | |
| 73 | /** |
| 74 | * Constructor. |
| 75 | * |
| 76 | * @param cache cache to update |
| 77 | */ |
| 78 | public RemoteEventHandler(LoadingCache<K, Optional<V>> cache) { |
| 79 | this.cache = checkNotNull(cache); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void mapCleared(MapEvent event) { |
| 84 | cache.invalidateAll(); |
| 85 | } |
| 86 | |
| 87 | @Override |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 88 | public void entryAdded(EntryEvent<byte[], byte[]> event) { |
| 89 | K key = deserialize(event.getKey()); |
| 90 | V newVal = deserialize(event.getValue()); |
| 91 | Optional<V> newValue = Optional.of(newVal); |
| 92 | cache.asMap().putIfAbsent(key, newValue); |
| 93 | onAdd(key, newVal); |
| 94 | } |
| 95 | |
| 96 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 97 | public void entryUpdated(EntryEvent<byte[], byte[]> event) { |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 98 | K key = deserialize(event.getKey()); |
| 99 | V oldVal = deserialize(event.getOldValue()); |
Yuta HIGUCHI | bb1fc72 | 2014-09-24 00:00:13 -0700 | [diff] [blame] | 100 | Optional<V> oldValue = Optional.fromNullable(oldVal); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 101 | V newVal = deserialize(event.getValue()); |
Yuta HIGUCHI | bb1fc72 | 2014-09-24 00:00:13 -0700 | [diff] [blame] | 102 | Optional<V> newValue = Optional.of(newVal); |
| 103 | cache.asMap().replace(key, oldValue, newValue); |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame] | 104 | onUpdate(key, oldVal, newVal); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void entryRemoved(EntryEvent<byte[], byte[]> event) { |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 109 | K key = deserialize(event.getKey()); |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 110 | V val = deserialize(event.getOldValue()); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 111 | cache.invalidate(key); |
| 112 | onRemove(key, val); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 113 | } |
| 114 | |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Cache entry addition hook. |
| 117 | * |
| 118 | * @param key new key |
| 119 | * @param newVal new value |
| 120 | */ |
| 121 | protected void onAdd(K key, V newVal) { |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Cache entry update hook. |
| 126 | * |
| 127 | * @param key new key |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame] | 128 | * @param oldValue old value |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 129 | * @param newVal new value |
| 130 | */ |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame] | 131 | protected void onUpdate(K key, V oldValue, V newVal) { |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Cache entry remove hook. |
| 136 | * |
| 137 | * @param key new key |
| 138 | * @param val old value |
| 139 | */ |
| 140 | protected void onRemove(K key, V val) { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | } |