Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 19 | |
| 20 | Defines the conduit between the client and the server: |
| 21 | - provides a clean API for sending events to the server |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 22 | - dispatches incoming events from the server to the appropriate sub-module |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 23 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | (function () { |
| 27 | 'use strict'; |
| 28 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 29 | // injected refs |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 30 | var $log, $interval, wss, tps, tis, tfs, tss, tov, tspr; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 31 | |
| 32 | // internal state |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 33 | var handlerMap, |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 34 | openListener, |
| 35 | heartbeatTimer; |
| 36 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 37 | var heartbeatPeriod = 9000; // 9 seconds |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 38 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 39 | // ========================== |
| 40 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 41 | function createHandlerMap() { |
| 42 | handlerMap = { |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 43 | showSummary: tps, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 44 | |
| 45 | showDetails: tss, |
| 46 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 47 | showHighlights: tov, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 49 | addInstance: tis, |
| 50 | updateInstance: tis, |
| 51 | removeInstance: tis, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 52 | |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 53 | addDevice: tfs, |
| 54 | updateDevice: tfs, |
| 55 | removeDevice: tfs, |
| 56 | addHost: tfs, |
| 57 | updateHost: tfs, |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame^] | 58 | moveHost: tfs, |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 59 | removeHost: tfs, |
| 60 | addLink: tfs, |
| 61 | updateLink: tfs, |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 62 | removeLink: tfs, |
| 63 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 64 | topoStartDone: tfs, |
| 65 | |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 66 | spriteListResponse: tspr, |
| 67 | spriteDataResponse: tspr |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 68 | }; |
| 69 | } |
| 70 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 71 | function wsOpen(host, url) { |
| 72 | $log.debug('TOPO: web socket open - cluster node:', host, 'URL:', url); |
Thomas Vachuska | 4c2fa06 | 2015-03-23 15:07:55 -0700 | [diff] [blame] | 73 | // Request batch of initial data from the new server |
| 74 | wss.sendEvent('topoStart'); |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 77 | function cancelHeartbeat() { |
| 78 | if (heartbeatTimer) { |
| 79 | $interval.cancel(heartbeatTimer); |
| 80 | } |
| 81 | heartbeatTimer = null; |
| 82 | } |
| 83 | |
| 84 | function scheduleHeartbeat() { |
| 85 | cancelHeartbeat(); |
| 86 | heartbeatTimer = $interval(function () { |
| 87 | wss.sendEvent('topoHeartbeat'); |
| 88 | }, heartbeatPeriod); |
| 89 | } |
| 90 | |
| 91 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 92 | angular.module('ovTopo') |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 93 | .factory('TopoEventService', |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 94 | ['$log', '$interval', 'WebSocketService', |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 95 | 'TopoPanelService', 'TopoInstService', 'TopoForceService', |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 96 | 'TopoSelectService', 'TopoOverlayService', 'TopoSpriteService', |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 97 | |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 98 | function (_$log_, _$interval_, _wss_, |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 99 | _tps_, _tis_, _tfs_, _tss_, _tov_, _tspr_) { |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 100 | $log = _$log_; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 101 | $interval = _$interval_; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 102 | wss = _wss_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 103 | tps = _tps_; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 104 | tis = _tis_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 105 | tfs = _tfs_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 106 | tss = _tss_; |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 107 | tov = _tov_; |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 108 | tspr = _tspr_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 109 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 110 | createHandlerMap(); |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 111 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 112 | function start() { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 113 | openListener = wss.addOpenListener(wsOpen); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 114 | wss.bindHandlers(handlerMap); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 115 | wss.sendEvent('topoStart'); |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 116 | scheduleHeartbeat(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 117 | $log.debug('topo comms started'); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 120 | function stop() { |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 121 | cancelHeartbeat(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 122 | wss.sendEvent('topoStop'); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 123 | wss.unbindHandlers(handlerMap); |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 124 | wss.removeOpenListener(openListener); |
| 125 | openListener = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 126 | $log.debug('topo comms stopped'); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 127 | } |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 128 | |
| 129 | return { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 130 | start: start, |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 131 | stop: stop |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 132 | }; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 133 | }]); |
| 134 | }()); |