GUI -- First pass at TopoEventService - to encapsulate sending/receiving events from server.

Change-Id: I604d63a715f1ee25ca7ed05bacccb9eb8d65a0f4
diff --git a/web/gui/src/main/webapp/app/index.html b/web/gui/src/main/webapp/app/index.html
index beeb2d3..bad7a6c 100644
--- a/web/gui/src/main/webapp/app/index.html
+++ b/web/gui/src/main/webapp/app/index.html
@@ -73,6 +73,7 @@
     <!-- {INJECTED-JAVASCRIPT} -->
     <script src="view/sample/sample.js"></script>
     <script src="view/topo/topo.js"></script>
+    <script src="view/topo/topoEvent.js"></script>
     <script src="view/device/device.js"></script>
     <!-- TODO: inject javascript refs server-side -->
 
diff --git a/web/gui/src/main/webapp/app/view/topo/topo.js b/web/gui/src/main/webapp/app/view/topo/topo.js
index 5addda8..480e499 100644
--- a/web/gui/src/main/webapp/app/view/topo/topo.js
+++ b/web/gui/src/main/webapp/app/view/topo/topo.js
@@ -28,13 +28,13 @@
     ];
 
     // references to injected services etc.
-    var $log, ks, zs, gs, ms, wss, ps;
+    var $log, ks, zs, gs, ms, ps, wss, tes;
 
     // DOM elements
     var ovtopo, svg, defs, zoomLayer, map;
 
     // Internal state
-    var zoomer, wsock;
+    var zoomer, wsock, evDispatcher;
 
     // Note: "exported" state should be properties on 'self' variable
 
@@ -130,20 +130,21 @@
     }
 
     // --- Web Socket Connection -----------------------------------------
+    // TODO: migrate this code to be encapsulated by TopoEventService
 
     function onWsOpen() {
         $log.log('web socket opened...');
-
+        evDispatcher.sendEvent('requestSummary');
     }
 
     function onWsMessage(ev) {
-        $log.log('got JSON event: ', ev);
-
+        evDispatcher.handleEvent(ev);
     }
 
     function onWsClose(reason) {
         $log.log('web socket closed; reason=', reason);
-
+        wsock = null;
+        tes.bindSock(null);
     }
 
     // wsport indicates web-socket-server port other than the default.
@@ -156,6 +157,8 @@
             wsport: wsport
         });
 
+        tes.bindSock(wsock);
+
         // TODO: handle "guiSuccessor" functionality (replace host)
         // TODO: implement retry on close functionality
 
@@ -172,18 +175,19 @@
         .controller('OvTopoCtrl', [
             '$scope', '$log', '$location', '$timeout',
             'KeyService', 'ZoomService', 'GlyphService', 'MapService',
-            'WebSocketService', 'PanelService',
+            'PanelService', 'WebSocketService', 'TopoEventService',
 
         function ($scope, _$log_, $loc, $timeout,
-                  _ks_, _zs_, _gs_, _ms_, _wss_, _ps_) {
+                  _ks_, _zs_, _gs_, _ms_, _ps_, _wss_, _tes_) {
             var self = this;
             $log = _$log_;
             ks = _ks_;
             zs = _zs_;
             gs = _gs_;
             ms = _ms_;
-            wss = _wss_;
             ps = _ps_;
+            wss = _wss_;
+            tes = _tes_;
 
             self.notifyResize = function () {
                 svgResized(svg.style('width'), svg.style('height'));
@@ -194,6 +198,7 @@
                 $log.log('OvTopoCtrl is saying Buh-Bye!');
                 wsock && wsock.close();
                 wsock = null;
+                tes.bindSock(null);
                 ps.destroyPanel('topo-p-summary');
             });
 
@@ -201,6 +206,9 @@
             ovtopo = d3.select('#ov-topo');
             svg = ovtopo.select('svg');
 
+            // bind to topo event service..
+            evDispatcher = tes.dispatcher;
+
             setUpKeys();
             setUpDefs();
             setUpZoom();
diff --git a/web/gui/src/main/webapp/app/view/topo/topoEvent.js b/web/gui/src/main/webapp/app/view/topo/topoEvent.js
new file mode 100644
index 0000000..943c883
--- /dev/null
+++ b/web/gui/src/main/webapp/app/view/topo/topoEvent.js
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ ONOS GUI -- Topology Event Module.
+ Defines event handling for events received from the server.
+ */
+
+(function () {
+    'use strict';
+
+    var $log, wes;
+
+    var evHandler = {
+        showSummary: showSummary,
+        addInstance: addInstance
+    };
+
+    function unknownEvent(ev) {
+        $log.warn('Unknown event (ignored):', ev);
+    }
+
+    // === Event Handlers ===
+
+    function showSummary(ev) {
+        $log.log('  **** Show Summary ****  ', ev.payload);
+    }
+
+    function addInstance(ev) {
+        $log.log(' *** We got an ADD INSTANCE event: ', ev);
+    }
+
+    angular.module('ovTopo')
+        .factory('TopoEventService', ['$log', 'WsEventService',
+        function (_$log_, _wes_) {
+            $log = _$log_;
+            wes = _wes_;
+
+            var wsock;
+
+            return {
+                dispatcher: {
+                    handleEvent: function (ev) {
+                        (evHandler[ev.event] || unknownEvent)(ev);
+                    },
+                    sendEvent: function (evType, payload) {
+                        if (wsock) {
+                            wes.sendEvent(wsock, evType, payload);
+                        } else {
+                            $log.warn('sendEvent: no websocket open:',
+                                evType, payload);
+                        }
+                    }
+                },
+                bindSock: function (ws) {
+                    wsock = ws;
+                }
+            }
+        }]);
+}());