Cleanup Hazelcast event notification framework.

Removed the custom leftover notification channels and replace them
with the newer (generic) notification framework channels.

NOTE: For now, all transient entry/events (PacketOutNotification and
ArpReplyNotification) are added by replacing the original (key, dummyByte)
tuple by (key, key) tuple. The reason is because with the new notification
framework, the entryAdded() upcall contains only the value.
The (key, key) tuple is a hack that needs to be removed when the
corresponding modules are refactored and fixed.

Change-Id: I3b1d31fba8d65400a5ed4b45dd42cef9da0bfc48
diff --git a/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java b/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java
index 9bd838b..e204e45 100755
--- a/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java
+++ b/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java
@@ -4,11 +4,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
 
 import net.floodlightcontroller.core.IFloodlightProviderService;
 import net.floodlightcontroller.core.module.FloodlightModuleContext;
@@ -17,30 +13,15 @@
 import net.floodlightcontroller.core.module.IFloodlightService;
 import net.floodlightcontroller.restserver.IRestApiService;
 import net.onrc.onos.datagrid.web.DatagridWebRoutable;
-import net.onrc.onos.ofcontroller.devicemanager.IDeviceEventHandler;
-import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
-import net.onrc.onos.ofcontroller.proxyarp.ArpReplyNotification;
-import net.onrc.onos.ofcontroller.proxyarp.IArpReplyEventHandler;
-import net.onrc.onos.ofcontroller.proxyarp.IPacketOutEventHandler;
-import net.onrc.onos.ofcontroller.proxyarp.PacketOutNotification;
-import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.esotericsoftware.kryo.Kryo;
-import com.esotericsoftware.kryo.io.Input;
-import com.esotericsoftware.kryo.io.Output;
 import com.hazelcast.config.Config;
 import com.hazelcast.config.FileSystemXmlConfig;
-import com.hazelcast.core.EntryEvent;
-import com.hazelcast.core.EntryListener;
 import com.hazelcast.core.Hazelcast;
 import com.hazelcast.core.HazelcastInstance;
-import com.hazelcast.core.IList;
-import com.hazelcast.core.IMap;
 import com.hazelcast.instance.GroupProperties;
-import net.onrc.onos.intent.Intent;
 
 /**
  * A datagrid service that uses Hazelcast as a datagrid.
@@ -48,8 +29,6 @@
  * appropriate in a multi-node cluster.
  */
 public class HazelcastDatagrid implements IFloodlightModule, IDatagridService {
-    private static final int MAX_BUFFER_SIZE = 64 * 1024;
-
     static final Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class);
     private IRestApiService restApi;
 
@@ -57,8 +36,6 @@
     private HazelcastInstance hazelcastInstance;
     private Config hazelcastConfig;
 
-    private final KryoFactory kryoFactory = new KryoFactory();
-
     //
     // NOTE: eventChannels is kept thread safe by using explicit "synchronized"
     // blocks below. Those are needed to protect the integrity of each entry
@@ -66,167 +43,13 @@
     //
     private final Map<String, IEventChannel<?, ?>> eventChannels = new HashMap<>();
 
-    // State related to the packet out map
-    private static final String PACKET_OUT_MAP_NAME = "packetOutMap";
-    private IMap<PacketOutNotification, byte[]> packetOutMap;
-    private final List<IPacketOutEventHandler> packetOutEventHandlers = new ArrayList<>();
-
-    private final byte[] dummyByte = {0};
-
-    // State related to the ARP reply map
-    private static final String ARP_REPLY_MAP_NAME = "arpReplyMap";
-    private IMap<ArpReplyNotification, byte[]> arpReplyMap;
-    private final List<IArpReplyEventHandler> arpReplyEventHandlers = new ArrayList<>();
-
-
-    private static final String INTENT_LIST_NAME = "intentList";
-    private IList<Intent> intentList;
-
-    @Override
-    public void registerIntent(Collection<Intent> intents) {
-        intentList.addAll(intents);
-    }
-
-
-    // State related to the Network Device map
-    private static final String MAP_DEVICE_NAME = "mapDevice";
-    private IMap<Long, OnosDevice> mapDevice;
-    private final List<IDeviceEventHandler> deviceEventHandlers = new ArrayList<>();
-
-    /**
-     * MapDeviceListener - reacts to Device related events.
-     */
-    class MapDeviceListener implements EntryListener<Long, OnosDevice> {
-
-        @Override
-        public void entryAdded(EntryEvent<Long, OnosDevice> event) {
-            for (IDeviceEventHandler deviceEventHandler : deviceEventHandlers) {
-                deviceEventHandler.addDeviceEvent(event.getKey(), event.getValue());
-            }
-        }
-
-        @Override
-        public void entryRemoved(EntryEvent<Long, OnosDevice> event) {
-            for (IDeviceEventHandler deviceEventHandler : deviceEventHandlers) {
-                deviceEventHandler.deleteDeviceEvent(event.getKey(), event.getValue());
-            }
-        }
-
-        @Override
-        public void entryUpdated(EntryEvent<Long, OnosDevice> event) {
-            for (IDeviceEventHandler deviceEventHandler : deviceEventHandlers) {
-                deviceEventHandler.updateDeviceEvent(event.getKey(), event.getValue());
-            }
-        }
-
-        @Override
-        public void entryEvicted(EntryEvent<Long, OnosDevice> arg0) {
-            //Not used.
-        }
-    }
-
-    /**
-     * Class for receiving notifications for sending packet-outs.
-     * <p/>
-     * The datagrid map is:
-     * - Key: Packet-out to send (PacketOutNotification)
-     * - Value: dummy value (we only need the key) (byte[])
-     */
-    class PacketOutMapListener implements EntryListener<PacketOutNotification, byte[]> {
-        /**
-         * Receive a notification that an entry is added.
-         *
-         * @param event the notification event for the entry.
-         */
-        @Override
-        public void entryAdded(EntryEvent<PacketOutNotification, byte[]> event) {
-            for (IPacketOutEventHandler packetOutEventHandler : packetOutEventHandlers) {
-                packetOutEventHandler.packetOutNotification(event.getKey());
-            }
-        }
-
-        /**
-         * Receive a notification that an entry is removed.
-         *
-         * @param event the notification event for the entry.
-         */
-        @Override
-        public void entryRemoved(EntryEvent<PacketOutNotification, byte[]> event) {
-            // Not used
-        }
-
-        /**
-         * Receive a notification that an entry is updated.
-         *
-         * @param event the notification event for the entry.
-         */
-        @Override
-        public void entryUpdated(EntryEvent<PacketOutNotification, byte[]> event) {
-            // Not used
-        }
-
-        /**
-         * Receive a notification that an entry is evicted.
-         *
-         * @param event the notification event for the entry.
-         */
-        @Override
-        public void entryEvicted(EntryEvent<PacketOutNotification, byte[]> event) {
-            // Not used
-        }
-    }
-
-    /**
-     * Class for receiving notifications for sending packet-outs.
-     * <p/>
-     * The datagrid map is:
-     * - Key: Packet-out to send (PacketOutNotification)
-     * - Value: dummy value (we only need the key) (byte[])
-     */
-    class ArpReplyMapListener implements EntryListener<ArpReplyNotification, byte[]> {
-        /**
-         * Receive a notification that an entry is added.
-         *
-         * @param event the notification event for the entry.
-         */
-        @Override
-        public void entryAdded(EntryEvent<ArpReplyNotification, byte[]> event) {
-            triggerEventHandler(event.getKey());
-        }
-
-        @Override
-        public void entryUpdated(EntryEvent<ArpReplyNotification, byte[]> event) {
-            triggerEventHandler(event.getKey());
-        }
-
-        @Override
-        public void entryRemoved(EntryEvent<ArpReplyNotification, byte[]> event) {
-            // Not used for ARP replies
-        }
-
-        @Override
-        public void entryEvicted(EntryEvent<ArpReplyNotification, byte[]> event) {
-            // Not used for ARP replies
-        }
-
-        /**
-         * Handle an event.
-         * @param notification notification
-         */
-        private void triggerEventHandler(ArpReplyNotification notification) {
-            for (IArpReplyEventHandler arpReplyEventHandler : arpReplyEventHandlers) {
-                arpReplyEventHandler.arpReplyEvent(notification);
-            }
-        }
-    }
-
     /**
      * Initialize the Hazelcast Datagrid operation.
      *
      * @param configFilename the configuration filename.
      */
     public void init(String configFilename) {
-    /*
+        /*
         System.setProperty("hazelcast.socket.receive.buffer.size", "32");
         System.setProperty("hazelcast.socket.send.buffer.size", "32");
         */
@@ -335,16 +158,6 @@
         hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig);
 
         restApi.addRestletRoutable(new DatagridWebRoutable());
-
-        packetOutMap = hazelcastInstance.getMap(PACKET_OUT_MAP_NAME);
-        packetOutMap.addEntryListener(new PacketOutMapListener(), true);
-
-        arpReplyMap = hazelcastInstance.getMap(ARP_REPLY_MAP_NAME);
-        arpReplyMap.addEntryListener(new ArpReplyMapListener(), true);
-        intentList = hazelcastInstance.getList(INTENT_LIST_NAME);
-
-        mapDevice = hazelcastInstance.getMap(MAP_DEVICE_NAME);
-        mapDevice.addEntryListener(new MapDeviceListener(), true);
     }
 
     /**
@@ -469,65 +282,4 @@
             }
         }
     }
-
-    @Override
-    public void registerPacketOutEventHandler(IPacketOutEventHandler packetOutEventHandler) {
-        if (packetOutEventHandler != null) {
-            packetOutEventHandlers.add(packetOutEventHandler);
-        }
-    }
-
-    @Override
-    public void deregisterPacketOutEventHandler(IPacketOutEventHandler packetOutEventHandler) {
-        packetOutEventHandlers.remove(packetOutEventHandler);
-    }
-
-    @Override
-    public void registerArpReplyEventHandler(IArpReplyEventHandler arpReplyEventHandler) {
-        if (arpReplyEventHandler != null) {
-            arpReplyEventHandlers.add(arpReplyEventHandler);
-        }
-    }
-
-    @Override
-    public void deregisterArpReplyEventHandler(IArpReplyEventHandler arpReplyEventHandler) {
-        arpReplyEventHandlers.remove(arpReplyEventHandler);
-    }
-
-    @Override
-    public void registerMapDeviceEventHandler(IDeviceEventHandler deviceEventHandler) {
-        if (deviceEventHandler != null) {
-            deviceEventHandlers.add(deviceEventHandler);
-        }
-    }
-
-    @Override
-    public void deregisterMapDeviceEventHandler(IDeviceEventHandler deviceEventHandler) {
-        deviceEventHandlers.remove(deviceEventHandler);
-    }
-
-    @Override
-    public void sendPacketOutNotification(PacketOutNotification packetOutNotification) {
-        packetOutMap.putAsync(packetOutNotification, dummyByte, 1L, TimeUnit.MILLISECONDS);
-    }
-
-    @Override
-    public void sendArpReplyNotification(ArpReplyNotification arpReply) {
-        arpReplyMap.putAsync(arpReply, dummyByte, 1L, TimeUnit.MILLISECONDS);
-    }
-
-    @Override
-    public void sendNotificationDeviceAdded(Long mac, OnosDevice dev) {
-        log.debug("DeviceAdded in datagrid. mac {}", dev.getMacAddress());
-        mapDevice.putAsync(mac, dev);
-    }
-
-    @Override
-    public void sendNotificationDeviceDeleted(OnosDevice dev) {
-        long mac = dev.getMacAddress().toLong();
-        if (mapDevice.containsKey(mac)) {
-            log.debug("DeviceDeleted in datagrid. mac {}", dev.getMacAddress());
-            mapDevice.removeAsync(mac);
-        }
-    }
 }