GUI -- Added ability to define an alternate port for WS - used to connect to mock web-socket server.  Topo view can use query string '?wsport=8123' for example.

Change-Id: Ie34d557b9580d58239380010e8d58233998a78ca
diff --git a/web/gui/src/main/webapp/app/fw/remote/urlfn.js b/web/gui/src/main/webapp/app/fw/remote/urlfn.js
index fcb373c..fe43267 100644
--- a/web/gui/src/main/webapp/app/fw/remote/urlfn.js
+++ b/web/gui/src/main/webapp/app/fw/remote/urlfn.js
@@ -33,25 +33,25 @@
                 return secure ? protocol + 's' : protocol;
             }
 
-            function urlBase(protocol) {
+            function urlBase(protocol, port) {
                 return matchSecure(protocol) + '://' +
-                    $loc.host() + ':' + $loc.port();
+                    $loc.host() + ':' + (port || $loc.port());
             }
 
             function httpPrefix(suffix) {
                 return urlBase('http') + suffix;
             }
 
-            function wsPrefix(suffix) {
-                return urlBase('ws') + suffix;
+            function wsPrefix(suffix, wsport) {
+                return urlBase('ws', wsport) + suffix;
             }
 
             function rsUrl(path) {
                 return httpPrefix(rsSuffix) + path;
             }
 
-            function wsUrl(path) {
-                return wsPrefix(wsSuffix) + path;
+            function wsUrl(path, wsport) {
+                return wsPrefix(wsSuffix, wsport) + path;
             }
 
             return {
diff --git a/web/gui/src/main/webapp/app/fw/remote/websocket.js b/web/gui/src/main/webapp/app/fw/remote/websocket.js
index de7b076..bdd1e3c 100644
--- a/web/gui/src/main/webapp/app/fw/remote/websocket.js
+++ b/web/gui/src/main/webapp/app/fw/remote/websocket.js
@@ -23,15 +23,17 @@
     var fs;
 
     angular.module('onosRemote')
-    .factory('WebSocketService', ['$location', 'UrlFnService', 'FnService',
-        function ($loc, ufs, _fs_) {
+    .factory('WebSocketService',
+            ['$log', '$location', 'UrlFnService', 'FnService',
+
+        function ($log, $loc, ufs, _fs_) {
             fs = _fs_;
 
             // creates a web socket for the given path, returning a "handle".
-            // cb is the callbacks block.
-            function createWebSocket(path, cb) {
-                //var fullUrl = ufs.wsUrl(path),
-                var fullUrl = 'ws://localhost:8123/foo',
+            // opts contains the event handler callbacks.
+            function createWebSocket(path, opts) {
+                var wsport = opts && opts.wsport,
+                    fullUrl = ufs.wsUrl(path, wsport),
                     ws = new WebSocket(fullUrl),
                     api = {
                         meta: { path: fullUrl, ws: ws },
@@ -39,9 +41,11 @@
                         close: close
                     };
 
-                ws.onopen = (cb && cb.onOpen) || null;
-                ws.onmessage = (cb && cb.onMessage) || null;
-                ws.onclose = (cb && cb.onClose) || null;
+                $log.debug('Attempting to open websocket to: ' + fullUrl);
+
+                ws.onopen = (opts && opts.onOpen) || null;
+                ws.onmessage = (opts && opts.onMessage) || null;
+                ws.onclose = (opts && opts.onClose) || null;
 
                 function send(msg) {
                     if (msg) {
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 8e7cec2..7337a68 100644
--- a/web/gui/src/main/webapp/app/view/topo/topo.js
+++ b/web/gui/src/main/webapp/app/view/topo/topo.js
@@ -147,11 +147,14 @@
 
     }
 
-    function setUpWebSocket() {
+    // wsport indicates web-socket-server port other than the default.
+    // Used for testing with the mock-web-socket-server.
+    function setUpWebSocket(wsport) {
         var wsHandle = wss.createWebSocket('topology', {
             onOpen: onWsOpen,
             onMessage: onWsMessage,
-            onClose: onWsClose
+            onClose: onWsClose,
+            wsport: wsport
         });
 
         // TODO: handle "guiSuccessor" functionality (replace host)
@@ -168,11 +171,11 @@
     angular.module('ovTopo', moduleDependencies)
 
         .controller('OvTopoCtrl', [
-            '$scope', '$log',
+            '$scope', '$log', '$location',
             'KeyService', 'ZoomService', 'GlyphService', 'MapService',
             'WebSocketService',
 
-        function ($scope, _$log_, _ks_, _zs_, _gs_, _ms_, _wss_) {
+        function ($scope, _$log_, $loc, _ks_, _zs_, _gs_, _ms_, _wss_) {
             var self = this;
             $log = _$log_;
             ks = _ks_;
@@ -200,7 +203,7 @@
             setUpDefs();
             setUpZoom();
             setUpMap();
-            setUpWebSocket();
+            setUpWebSocket($loc.search().wsport);
 
             $log.log('OvTopoCtrl has been created');
         }]);
diff --git a/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js b/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js
index 9886fc3..7b6c3c4 100644
--- a/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js
@@ -76,4 +76,9 @@
         setLoc('https', 'foo', '123');
         expect(ufs.wsUrl('path')).toEqual('wss://foo:123/onos/ui/ws/path');
     });
+
+    it('should allow us to define an alternate WS port', function () {
+        setLoc('http', 'foo', '123');
+        expect(ufs.wsUrl('xyyzy', 456)).toEqual('ws://foo:456/onos/ui/ws/xyyzy');
+    });
 });