blob: 2957629ac32d1d1409e3f25a5f0d191ae4224c86 [file] [log] [blame]
Simon Huntbb596362015-01-26 21:27:42 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Topology Event Module.
Simon Hunt24077f12015-02-04 17:58:21 -080019
20 Defines the conduit between the client and the server:
21 - provides a clean API for sending events to the server
Simon Hunt75ec9692015-02-11 16:40:36 -080022 - dispatches incoming events from the server to the appropriate sub-module
Simon Hunt24077f12015-02-04 17:58:21 -080023
Simon Huntbb596362015-01-26 21:27:42 -080024 */
25
26(function () {
27 'use strict';
28
Simon Huntfd1231a2015-01-26 22:14:51 -080029 // injected refs
Simon Hunt8d22c4b2015-08-06 16:24:43 -070030 var $log, $interval, wss, tps, tis, tfs, tss, tov, tspr;
Simon Huntfd1231a2015-01-26 22:14:51 -080031
32 // internal state
Simon Hunt3b9ad04d2015-03-11 15:26:02 -070033 var handlerMap,
Simon Hunt732bb2e2015-05-13 18:32:16 -070034 openListener,
35 heartbeatTimer;
36
Simon Hunt8d22c4b2015-08-06 16:24:43 -070037 var heartbeatPeriod = 9000; // 9 seconds
Simon Hunt1894d792015-02-04 17:09:20 -080038
Simon Huntfd1231a2015-01-26 22:14:51 -080039 // ==========================
40
Simon Hunt20207df2015-03-10 18:30:14 -070041 function createHandlerMap() {
42 handlerMap = {
Simon Hunt24077f12015-02-04 17:58:21 -080043 showSummary: tps,
Simon Hunt08f841d02015-02-10 14:39:20 -080044
45 showDetails: tss,
46
Simon Hunt8d22c4b2015-08-06 16:24:43 -070047 showHighlights: tov,
Simon Huntf542d842015-02-11 16:20:33 -080048
Simon Hunt24077f12015-02-04 17:58:21 -080049 addInstance: tis,
50 updateInstance: tis,
51 removeInstance: tis,
Simon Hunt08f841d02015-02-10 14:39:20 -080052
Simon Hunt24077f12015-02-04 17:58:21 -080053 addDevice: tfs,
54 updateDevice: tfs,
55 removeDevice: tfs,
56 addHost: tfs,
57 updateHost: tfs,
58 removeHost: tfs,
59 addLink: tfs,
60 updateLink: tfs,
Simon Huntfd8c7d72015-04-14 17:53:37 -070061 removeLink: tfs,
62
Simon Hunt4a6b54b2015-10-27 22:08:25 -070063 topoStartDone: tfs,
64
Simon Huntfd8c7d72015-04-14 17:53:37 -070065 spriteListResponse: tspr,
66 spriteDataResponse: tspr
Simon Hunt24077f12015-02-04 17:58:21 -080067 };
68 }
69
Simon Hunt3b9ad04d2015-03-11 15:26:02 -070070 function wsOpen(host, url) {
71 $log.debug('TOPO: web socket open - cluster node:', host, 'URL:', url);
Thomas Vachuska4c2fa062015-03-23 15:07:55 -070072 // Request batch of initial data from the new server
73 wss.sendEvent('topoStart');
Simon Hunt3b9ad04d2015-03-11 15:26:02 -070074 }
75
Simon Hunt732bb2e2015-05-13 18:32:16 -070076 function cancelHeartbeat() {
77 if (heartbeatTimer) {
78 $interval.cancel(heartbeatTimer);
79 }
80 heartbeatTimer = null;
81 }
82
83 function scheduleHeartbeat() {
84 cancelHeartbeat();
85 heartbeatTimer = $interval(function () {
86 wss.sendEvent('topoHeartbeat');
87 }, heartbeatPeriod);
88 }
89
90
Simon Huntbb596362015-01-26 21:27:42 -080091 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -080092 .factory('TopoEventService',
Simon Hunt732bb2e2015-05-13 18:32:16 -070093 ['$log', '$interval', 'WebSocketService',
Simon Huntac4c6f72015-02-03 19:50:53 -080094 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Hunt8d22c4b2015-08-06 16:24:43 -070095 'TopoSelectService', 'TopoOverlayService', 'TopoSpriteService',
Simon Huntfd1231a2015-01-26 22:14:51 -080096
Simon Hunt732bb2e2015-05-13 18:32:16 -070097 function (_$log_, _$interval_, _wss_,
Simon Hunt8d22c4b2015-08-06 16:24:43 -070098 _tps_, _tis_, _tfs_, _tss_, _tov_, _tspr_) {
Simon Huntbb596362015-01-26 21:27:42 -080099 $log = _$log_;
Simon Hunt732bb2e2015-05-13 18:32:16 -0700100 $interval = _$interval_;
Thomas Vachuska329af532015-03-10 02:08:33 -0700101 wss = _wss_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800102 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800103 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800104 tfs = _tfs_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800105 tss = _tss_;
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700106 tov = _tov_;
Simon Huntfd8c7d72015-04-14 17:53:37 -0700107 tspr = _tspr_;
Simon Huntbb596362015-01-26 21:27:42 -0800108
Simon Hunt20207df2015-03-10 18:30:14 -0700109 createHandlerMap();
Simon Hunt24077f12015-02-04 17:58:21 -0800110
Thomas Vachuska329af532015-03-10 02:08:33 -0700111 function start() {
Simon Hunt3b9ad04d2015-03-11 15:26:02 -0700112 openListener = wss.addOpenListener(wsOpen);
Simon Hunt20207df2015-03-10 18:30:14 -0700113 wss.bindHandlers(handlerMap);
Thomas Vachuska329af532015-03-10 02:08:33 -0700114 wss.sendEvent('topoStart');
Simon Hunt732bb2e2015-05-13 18:32:16 -0700115 scheduleHeartbeat();
Thomas Vachuska329af532015-03-10 02:08:33 -0700116 $log.debug('topo comms started');
Simon Huntfd1231a2015-01-26 22:14:51 -0800117 }
118
Thomas Vachuska329af532015-03-10 02:08:33 -0700119 function stop() {
Simon Hunt732bb2e2015-05-13 18:32:16 -0700120 cancelHeartbeat();
Thomas Vachuska329af532015-03-10 02:08:33 -0700121 wss.sendEvent('topoStop');
Simon Hunt20207df2015-03-10 18:30:14 -0700122 wss.unbindHandlers(handlerMap);
Simon Hunt3b9ad04d2015-03-11 15:26:02 -0700123 wss.removeOpenListener(openListener);
124 openListener = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700125 $log.debug('topo comms stopped');
Simon Huntfd1231a2015-01-26 22:14:51 -0800126 }
Simon Huntbb596362015-01-26 21:27:42 -0800127
128 return {
Thomas Vachuska329af532015-03-10 02:08:33 -0700129 start: start,
Simon Hunt237676b52015-03-10 19:04:26 -0700130 stop: stop
Simon Hunt737c89f2015-01-28 12:23:19 -0800131 };
Simon Huntbb596362015-01-26 21:27:42 -0800132 }]);
133}());