DeviceStore bugfixes

Change-Id: Iebbfd99ea578c36438ec11e28e5230c73886dd55
diff --git a/core/store/src/main/java/org/onlab/onos/store/device/impl/DistributedDeviceStore.java b/core/store/src/main/java/org/onlab/onos/store/device/impl/DistributedDeviceStore.java
index 6d132ad..62a4c5e 100644
--- a/core/store/src/main/java/org/onlab/onos/store/device/impl/DistributedDeviceStore.java
+++ b/core/store/src/main/java/org/onlab/onos/store/device/impl/DistributedDeviceStore.java
@@ -1,12 +1,15 @@
 package org.onlab.onos.store.device.impl;
 
+import static com.google.common.base.Predicates.notNull;
 import com.google.common.base.Optional;
 import com.google.common.cache.LoadingCache;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSet.Builder;
 import com.hazelcast.core.IMap;
 import com.hazelcast.core.ISet;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -184,10 +187,12 @@
                                                       desc.swVersion(),
                                                       desc.serialNumber());
             synchronized (this) {
+                final byte[] deviceIdBytes = serialize(device.id());
+                rawDevices.put(deviceIdBytes, serialize(updated));
                 devices.put(device.id(), Optional.of(updated));
                 availableDevices.add(serialize(device.id()));
             }
-            return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
+            return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, updated, null);
         }
 
         // Otherwise merely attempt to change availability
@@ -231,7 +236,7 @@
 
             events.addAll(pruneOldPorts(device, ports, processed));
         }
-        return events;
+        return FluentIterable.from(events).filter(notNull()).toList();
     }
 
     // Creates a new port based on the port description adds it to the map and
@@ -258,7 +263,7 @@
                                     portDescription.isEnabled());
             ports.put(port.number(), updatedPort);
             updatePortMap(device.id(), ports);
-            return new DeviceEvent(PORT_UPDATED, device, port);
+            return new DeviceEvent(PORT_UPDATED, device, updatedPort);
         }
         return null;
     }
@@ -355,17 +360,17 @@
 
         @Override
         protected void onAdd(DeviceId deviceId, DefaultDevice device) {
-            delegate.notify(new DeviceEvent(DEVICE_ADDED, device));
+            notifyDelegate(new DeviceEvent(DEVICE_ADDED, device));
         }
 
         @Override
         protected void onRemove(DeviceId deviceId, DefaultDevice device) {
-            delegate.notify(new DeviceEvent(DEVICE_REMOVED, device));
+            notifyDelegate(new DeviceEvent(DEVICE_REMOVED, device));
         }
 
         @Override
         protected void onUpdate(DeviceId deviceId, DefaultDevice device) {
-            delegate.notify(new DeviceEvent(DEVICE_UPDATED, device));
+            notifyDelegate(new DeviceEvent(DEVICE_UPDATED, device));
         }
     }
 
@@ -376,17 +381,17 @@
 
         @Override
         protected void onAdd(DeviceId deviceId, Map<PortNumber, Port> ports) {
-//            delegate.notify(new DeviceEvent(PORT_ADDED, getDevice(deviceId)));
+//            notifyDelegate(new DeviceEvent(PORT_ADDED, getDevice(deviceId)));
         }
 
         @Override
         protected void onRemove(DeviceId deviceId, Map<PortNumber, Port> ports) {
-//            delegate.notify(new DeviceEvent(PORT_REMOVED, getDevice(deviceId)));
+//            notifyDelegate(new DeviceEvent(PORT_REMOVED, getDevice(deviceId)));
         }
 
         @Override
         protected void onUpdate(DeviceId deviceId, Map<PortNumber, Port> ports) {
-//            delegate.notify(new DeviceEvent(PORT_UPDATED, getDevice(deviceId)));
+//            notifyDelegate(new DeviceEvent(PORT_UPDATED, getDevice(deviceId)));
         }
     }
 
diff --git a/core/store/src/main/java/org/onlab/onos/store/impl/AbstractDistributedStore.java b/core/store/src/main/java/org/onlab/onos/store/impl/AbstractDistributedStore.java
index e7c2d58..41af9b3 100644
--- a/core/store/src/main/java/org/onlab/onos/store/impl/AbstractDistributedStore.java
+++ b/core/store/src/main/java/org/onlab/onos/store/impl/AbstractDistributedStore.java
@@ -107,7 +107,7 @@
         @Override
         public void entryRemoved(EntryEvent<byte[], byte[]> event) {
             K key = deserialize(event.getKey());
-            V val = deserialize(event.getValue());
+            V val = deserialize(event.getOldValue());
             cache.invalidate(key);
             onRemove(key, val);
         }
diff --git a/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java b/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
index 9b78798..ae3bc5a 100644
--- a/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
+++ b/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceStore.java
@@ -1,6 +1,8 @@
 package org.onlab.onos.net.trivial.impl;
 
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -33,6 +35,7 @@
 import java.util.concurrent.ConcurrentHashMap;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Predicates.notNull;
 import static org.onlab.onos.net.device.DeviceEvent.Type.*;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -123,7 +126,7 @@
                 devices.put(device.id(), updated);
                 availableDevices.add(device.id());
             }
-            return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, device, null);
+            return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, updated, null);
         }
 
         // Otherwise merely attempt to change availability
@@ -165,7 +168,7 @@
 
             events.addAll(pruneOldPorts(device, ports, processed));
         }
-        return events;
+        return FluentIterable.from(events).filter(notNull()).toList();
     }
 
     // Creates a new port based on the port description adds it to the map and