blob: 207036c551ba53f9bd9f78d9e042364f8a8f3eaa [file] [log] [blame]
tom8cc5aa72014-09-19 15:14:43 -07001package org.onlab.onos.store.device.impl;
2
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -07003import com.google.common.base.Optional;
4import com.google.common.cache.CacheBuilder;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -07005import com.google.common.cache.LoadingCache;
6import com.google.common.collect.ImmutableList;
7import com.google.common.collect.ImmutableSet;
8import com.google.common.collect.ImmutableSet.Builder;
9import com.hazelcast.core.EntryAdapter;
10import com.hazelcast.core.EntryEvent;
11import com.hazelcast.core.HazelcastInstance;
12import com.hazelcast.core.IMap;
13import com.hazelcast.core.ISet;
14import com.hazelcast.core.MapEvent;
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070015
tom0872a172014-09-23 11:24:26 -070016import org.apache.felix.scr.annotations.Activate;
17import org.apache.felix.scr.annotations.Component;
18import org.apache.felix.scr.annotations.Deactivate;
19import org.apache.felix.scr.annotations.Reference;
20import org.apache.felix.scr.annotations.ReferenceCardinality;
21import org.apache.felix.scr.annotations.Service;
22import org.onlab.onos.net.DefaultDevice;
23import org.onlab.onos.net.DefaultPort;
24import org.onlab.onos.net.Device;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.MastershipRole;
27import org.onlab.onos.net.Port;
28import org.onlab.onos.net.PortNumber;
29import org.onlab.onos.net.device.DeviceDescription;
30import org.onlab.onos.net.device.DeviceEvent;
31import org.onlab.onos.net.device.DeviceStore;
32import org.onlab.onos.net.device.PortDescription;
33import org.onlab.onos.net.provider.ProviderId;
34import org.onlab.onos.store.StoreService;
35import org.onlab.onos.store.impl.AbsentInvalidatingLoadingCache;
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070036import org.onlab.onos.store.impl.OptionalCacheLoader;
tom0872a172014-09-23 11:24:26 -070037import org.slf4j.Logger;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070038
tom0872a172014-09-23 11:24:26 -070039import java.util.ArrayList;
40import java.util.Collections;
41import java.util.HashMap;
42import java.util.HashSet;
43import java.util.Iterator;
44import java.util.List;
45import java.util.Map;
46import java.util.Objects;
47import java.util.Set;
48
49import static com.google.common.base.Preconditions.checkArgument;
50import static com.google.common.base.Preconditions.checkNotNull;
51import static org.onlab.onos.net.device.DeviceEvent.Type.*;
52import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070053
tom8cc5aa72014-09-19 15:14:43 -070054
55/**
56 * Manages inventory of infrastructure devices using Hazelcast-backed map.
57 */
58@Component(immediate = true)
59@Service
60public class DistributedDeviceStore implements DeviceStore {
61
62 private final Logger log = getLogger(getClass());
63
64 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
65
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070066 // private IMap<DeviceId, DefaultDevice> cache;
67 private IMap<byte[], byte[]> rawDevices;
68 private LoadingCache<DeviceId, Optional<DefaultDevice>> devices;
69
70 // private IMap<DeviceId, MastershipRole> roles;
71 private IMap<byte[], byte[]> rawRoles;
72 private LoadingCache<DeviceId, Optional<MastershipRole>> roles;
73
74 // private ISet<DeviceId> availableDevices;
75 private ISet<byte[]> availableDevices;
76
77 // TODO DevicePorts is very inefficient consider restructuring.
78 // private IMap<DeviceId, Map<PortNumber, Port>> devicePorts;
79 private IMap<byte[], byte[]> rawDevicePorts;
80 private LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> devicePorts;
81
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomdc66b382014-09-22 17:05:47 -070083 protected StoreService storeService;
84
Yuta HIGUCHIc7052012014-09-22 19:11:00 -070085 protected HazelcastInstance theInstance;
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070086
tom8cc5aa72014-09-19 15:14:43 -070087
88 @Activate
89 public void activate() {
90 log.info("Started");
tomdc66b382014-09-22 17:05:47 -070091 theInstance = storeService.getHazelcastInstance();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -070092
93 // IMap event handler needs value
94 final boolean includeValue = true;
95
96 // TODO decide on Map name scheme to avoid collision
97 rawDevices = theInstance.getMap("devices");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -070098 final OptionalCacheLoader<DeviceId, DefaultDevice> deviceLoader
99 = new OptionalCacheLoader<>(storeService, rawDevices);
tom0872a172014-09-23 11:24:26 -0700100 devices = new AbsentInvalidatingLoadingCache<>(
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700101 CacheBuilder.newBuilder()
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700102 .build(deviceLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700103 // refresh/populate cache based on notification from other instance
104 rawDevices.addEntryListener(
tom0872a172014-09-23 11:24:26 -0700105 new RemoteEventHandler<>(devices),
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700106 includeValue);
107
108 rawRoles = theInstance.getMap("roles");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700109 final OptionalCacheLoader<DeviceId, MastershipRole> rolesLoader
110 = new OptionalCacheLoader<>(storeService, rawRoles);
tom0872a172014-09-23 11:24:26 -0700111 roles = new AbsentInvalidatingLoadingCache<>(
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700112 CacheBuilder.newBuilder()
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700113 .build(rolesLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700114 // refresh/populate cache based on notification from other instance
115 rawRoles.addEntryListener(
tom0872a172014-09-23 11:24:26 -0700116 new RemoteEventHandler<>(roles),
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700117 includeValue);
118
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700119 // TODO cache availableDevices
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700120 availableDevices = theInstance.getSet("availableDevices");
121
122 rawDevicePorts = theInstance.getMap("devicePorts");
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700123 final OptionalCacheLoader<DeviceId, Map<PortNumber, Port>> devicePortLoader
124 = new OptionalCacheLoader<>(storeService, rawDevicePorts);
tom0872a172014-09-23 11:24:26 -0700125 devicePorts = new AbsentInvalidatingLoadingCache<>(
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700126 CacheBuilder.newBuilder()
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700127 .build(devicePortLoader));
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700128 // refresh/populate cache based on notification from other instance
129 rawDevicePorts.addEntryListener(
tom0872a172014-09-23 11:24:26 -0700130 new RemoteEventHandler<>(devicePorts),
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700131 includeValue);
132
tom8cc5aa72014-09-19 15:14:43 -0700133 }
134
135 @Deactivate
136 public void deactivate() {
137 log.info("Stopped");
138 }
139
140 @Override
141 public int getDeviceCount() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700142 // TODO IMap size or cache size?
143 return rawDevices.size();
tom8cc5aa72014-09-19 15:14:43 -0700144 }
145
146 @Override
147 public Iterable<Device> getDevices() {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700148// TODO Revisit if we ever need to do this.
149// log.info("{}:{}", rawMap.size(), cache.size());
150// if (rawMap.size() != cache.size()) {
151// for (Entry<byte[], byte[]> e : rawMap.entrySet()) {
152// final DeviceId key = deserialize(e.getKey());
153// final DefaultDevice val = deserialize(e.getValue());
154// cache.put(key, val);
155// }
156// }
157
158 // TODO builder v.s. copyOf. Guava semms to be using copyOf?
tom0872a172014-09-23 11:24:26 -0700159 Builder<Device> builder = ImmutableSet.builder();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700160 for (Optional<DefaultDevice> e : devices.asMap().values()) {
161 if (e.isPresent()) {
162 builder.add(e.get());
163 }
164 }
165 return builder.build();
tom8cc5aa72014-09-19 15:14:43 -0700166 }
167
168 @Override
169 public Device getDevice(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700170 // TODO revisit if ignoring exception is safe.
171 return devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700172 }
173
174 @Override
175 public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700176 DeviceDescription deviceDescription) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700177 DefaultDevice device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700178 if (device == null) {
179 return createDevice(providerId, deviceId, deviceDescription);
180 }
181 return updateDevice(providerId, device, deviceDescription);
182 }
183
184 // Creates the device and returns the appropriate event if necessary.
185 private DeviceEvent createDevice(ProviderId providerId, DeviceId deviceId,
186 DeviceDescription desc) {
187 DefaultDevice device = new DefaultDevice(providerId, deviceId, desc.type(),
188 desc.manufacturer(),
189 desc.hwVersion(), desc.swVersion(),
190 desc.serialNumber());
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700191
tom8cc5aa72014-09-19 15:14:43 -0700192 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700193 final byte[] deviceIdBytes = serialize(deviceId);
194 rawDevices.put(deviceIdBytes, serialize(device));
195 devices.put(deviceId, Optional.of(device));
196
197 availableDevices.add(deviceIdBytes);
tom8cc5aa72014-09-19 15:14:43 -0700198
199 // For now claim the device as a master automatically.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700200 rawRoles.put(deviceIdBytes, serialize(MastershipRole.MASTER));
tom0872a172014-09-23 11:24:26 -0700201 roles.put(deviceId, Optional.of(MastershipRole.MASTER));
tom8cc5aa72014-09-19 15:14:43 -0700202 }
203 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device, null);
204 }
205
206 // Updates the device and returns the appropriate event if necessary.
207 private DeviceEvent updateDevice(ProviderId providerId, DefaultDevice device,
208 DeviceDescription desc) {
209 // We allow only certain attributes to trigger update
210 if (!Objects.equals(device.hwVersion(), desc.hwVersion()) ||
tom0872a172014-09-23 11:24:26 -0700211 !Objects.equals(device.swVersion(), desc.swVersion())) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700212
tom8cc5aa72014-09-19 15:14:43 -0700213 DefaultDevice updated = new DefaultDevice(providerId, device.id(),
214 desc.type(),
215 desc.manufacturer(),
216 desc.hwVersion(),
217 desc.swVersion(),
218 desc.serialNumber());
219 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700220 devices.put(device.id(), Optional.of(updated));
221 availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700222 }
223 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
224 }
225
226 // Otherwise merely attempt to change availability
227 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700228 boolean added = availableDevices.add(serialize(device.id()));
tom8cc5aa72014-09-19 15:14:43 -0700229 return !added ? null :
230 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
231 }
232 }
233
234 @Override
235 public DeviceEvent markOffline(DeviceId deviceId) {
236 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700237 Device device = devices.getUnchecked(deviceId).orNull();
238 boolean removed = device != null && availableDevices.remove(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700239 return !removed ? null :
240 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
241 }
242 }
243
244 @Override
245 public List<DeviceEvent> updatePorts(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700246 List<PortDescription> portDescriptions) {
tom8cc5aa72014-09-19 15:14:43 -0700247 List<DeviceEvent> events = new ArrayList<>();
248 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700249 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700250 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
251 Map<PortNumber, Port> ports = getPortMap(deviceId);
252
253 // Add new ports
254 Set<PortNumber> processed = new HashSet<>();
255 for (PortDescription portDescription : portDescriptions) {
256 Port port = ports.get(portDescription.portNumber());
257 events.add(port == null ?
258 createPort(device, portDescription, ports) :
259 updatePort(device, port, portDescription, ports));
260 processed.add(portDescription.portNumber());
261 }
262
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700263 updatePortMap(deviceId, ports);
264
tom8cc5aa72014-09-19 15:14:43 -0700265 events.addAll(pruneOldPorts(device, ports, processed));
266 }
267 return events;
268 }
269
270 // Creates a new port based on the port description adds it to the map and
271 // Returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700272 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700273 private DeviceEvent createPort(Device device, PortDescription portDescription,
274 Map<PortNumber, Port> ports) {
275 DefaultPort port = new DefaultPort(device, portDescription.portNumber(),
276 portDescription.isEnabled());
277 ports.put(port.number(), port);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700278 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700279 return new DeviceEvent(PORT_ADDED, device, port);
280 }
281
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700282 // Checks if the specified port requires update and if so, it replaces the
tom8cc5aa72014-09-19 15:14:43 -0700283 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700284 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700285 private DeviceEvent updatePort(Device device, Port port,
286 PortDescription portDescription,
287 Map<PortNumber, Port> ports) {
288 if (port.isEnabled() != portDescription.isEnabled()) {
289 DefaultPort updatedPort =
290 new DefaultPort(device, portDescription.portNumber(),
291 portDescription.isEnabled());
292 ports.put(port.number(), updatedPort);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700293 updatePortMap(device.id(), ports);
tom8cc5aa72014-09-19 15:14:43 -0700294 return new DeviceEvent(PORT_UPDATED, device, port);
295 }
296 return null;
297 }
298
299 // Prunes the specified list of ports based on which ports are in the
300 // processed list and returns list of corresponding events.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700301 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700302 private List<DeviceEvent> pruneOldPorts(Device device,
303 Map<PortNumber, Port> ports,
304 Set<PortNumber> processed) {
305 List<DeviceEvent> events = new ArrayList<>();
306 Iterator<PortNumber> iterator = ports.keySet().iterator();
307 while (iterator.hasNext()) {
308 PortNumber portNumber = iterator.next();
309 if (!processed.contains(portNumber)) {
310 events.add(new DeviceEvent(PORT_REMOVED, device,
311 ports.get(portNumber)));
312 iterator.remove();
313 }
314 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700315 if (!events.isEmpty()) {
316 updatePortMap(device.id(), ports);
317 }
tom8cc5aa72014-09-19 15:14:43 -0700318 return events;
319 }
320
321 // Gets the map of ports for the specified device; if one does not already
322 // exist, it creates and registers a new one.
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700323 // WARN: returned value is a copy, changes made to the Map
324 // needs to be written back using updatePortMap
325 //@GuardedBy("this")
tom8cc5aa72014-09-19 15:14:43 -0700326 private Map<PortNumber, Port> getPortMap(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700327 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700328 if (ports == null) {
329 ports = new HashMap<>();
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700330 // this probably is waste of time in most cases.
331 updatePortMap(deviceId, ports);
tom8cc5aa72014-09-19 15:14:43 -0700332 }
333 return ports;
334 }
335
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700336 //@GuardedBy("this")
337 private void updatePortMap(DeviceId deviceId, Map<PortNumber, Port> ports) {
338 rawDevicePorts.put(serialize(deviceId), serialize(ports));
339 devicePorts.put(deviceId, Optional.of(ports));
340 }
341
tom8cc5aa72014-09-19 15:14:43 -0700342 @Override
343 public DeviceEvent updatePortStatus(DeviceId deviceId,
tom0872a172014-09-23 11:24:26 -0700344 PortDescription portDescription) {
tom8cc5aa72014-09-19 15:14:43 -0700345 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700346 Device device = devices.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700347 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
348 Map<PortNumber, Port> ports = getPortMap(deviceId);
349 Port port = ports.get(portDescription.portNumber());
350 return updatePort(device, port, portDescription, ports);
351 }
352 }
353
354 @Override
355 public List<Port> getPorts(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700356 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
357 return ports == null ? Collections.<Port>emptyList() : ImmutableList.copyOf(ports.values());
tom8cc5aa72014-09-19 15:14:43 -0700358 }
359
360 @Override
361 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700362 Map<PortNumber, Port> ports = devicePorts.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700363 return ports == null ? null : ports.get(portNumber);
364 }
365
366 @Override
367 public boolean isAvailable(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700368 return availableDevices.contains(serialize(deviceId));
tom8cc5aa72014-09-19 15:14:43 -0700369 }
370
371 @Override
372 public MastershipRole getRole(DeviceId deviceId) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700373 MastershipRole role = roles.getUnchecked(deviceId).orNull();
tom8cc5aa72014-09-19 15:14:43 -0700374 return role != null ? role : MastershipRole.NONE;
375 }
376
377 @Override
378 public DeviceEvent setRole(DeviceId deviceId, MastershipRole role) {
379 synchronized (this) {
380 Device device = getDevice(deviceId);
381 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700382 MastershipRole oldRole = deserialize(
383 rawRoles.put(serialize(deviceId), serialize(role)));
384 roles.put(deviceId, Optional.of(role));
tom8cc5aa72014-09-19 15:14:43 -0700385 return oldRole == role ? null :
386 new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device, null);
387 }
388 }
389
390 @Override
391 public DeviceEvent removeDevice(DeviceId deviceId) {
392 synchronized (this) {
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700393 byte[] deviceIdBytes = serialize(deviceId);
394 rawRoles.remove(deviceIdBytes);
395 roles.invalidate(deviceId);
396
397 // TODO conditional remove?
398 Device device = deserialize(rawDevices.remove(deviceIdBytes));
399 devices.invalidate(deviceId);
tom8cc5aa72014-09-19 15:14:43 -0700400 return device == null ? null :
401 new DeviceEvent(DEVICE_REMOVED, device, null);
402 }
403 }
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700404
405 // TODO cache serialized DeviceID if we suffer from serialization cost
tom0872a172014-09-23 11:24:26 -0700406 private byte[] serialize(final Object obj) {
407 return storeService.serialize(obj);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700408 }
409
tom0872a172014-09-23 11:24:26 -0700410 private <T> T deserialize(final byte[] bytes) {
411 return storeService.deserialize(bytes);
Yuta HIGUCHIb3b2ac42014-09-21 23:37:11 -0700412 }
413
Yuta HIGUCHIc7052012014-09-22 19:11:00 -0700414 /**
415 * An IMap EntryListener, which reflects each remote event to cache.
416 *
417 * @param <K> IMap key type after deserialization
418 * @param <V> IMap value type after deserialization
419 */
tom0872a172014-09-23 11:24:26 -0700420 public final class RemoteEventHandler<K, V> extends
Yuta HIGUCHIc7052012014-09-22 19:11:00 -0700421 EntryAdapter<byte[], byte[]> {
422
423 private LoadingCache<K, Optional<V>> cache;
424
425 /**
426 * Constructor.
427 *
428 * @param cache cache to update
429 */
430 public RemoteEventHandler(
431 LoadingCache<K, Optional<V>> cache) {
432 this.cache = checkNotNull(cache);
433 }
434
435 @Override
436 public void mapCleared(MapEvent event) {
437 cache.invalidateAll();
438 }
439
440 @Override
441 public void entryUpdated(EntryEvent<byte[], byte[]> event) {
tom0872a172014-09-23 11:24:26 -0700442 cache.put(storeService.<K>deserialize(event.getKey()),
443 Optional.of(storeService.<V>deserialize(event.getValue())));
Yuta HIGUCHIc7052012014-09-22 19:11:00 -0700444 }
445
446 @Override
447 public void entryRemoved(EntryEvent<byte[], byte[]> event) {
Yuta HIGUCHI951e7902014-09-23 14:45:11 -0700448 cache.invalidate(storeService.<K>deserialize(event.getKey()));
Yuta HIGUCHIc7052012014-09-22 19:11:00 -0700449 }
450
451 @Override
452 public void entryAdded(EntryEvent<byte[], byte[]> event) {
453 entryUpdated(event);
454 }
455 }
tom8cc5aa72014-09-19 15:14:43 -0700456}