Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 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. |
| 19 | |
| 20 | Defines the conduit between the client and the server: |
| 21 | - provides a clean API for sending events to the server |
| 22 | - dispatches incoming events from the server to the appropriate sub-module |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | (function () { |
| 27 | 'use strict'; |
| 28 | |
| 29 | // injected refs |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 30 | var $log, wss, t2fs, t2ovs; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 31 | |
| 32 | // internal state |
| 33 | var handlerMap, |
| 34 | openListener; |
| 35 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 36 | // ========================== Helper Functions |
| 37 | |
| 38 | function createHandlerMap() { |
| 39 | handlerMap = { |
| 40 | topo2AllInstances: t2fs, |
| 41 | topo2CurrentLayout: t2fs, |
| 42 | topo2CurrentRegion: t2fs, |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 43 | topo2PeerRegions: t2fs, |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 44 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 45 | topo2UiModelEvent: t2fs, |
| 46 | topo2Highlights: t2ovs.showHighlights, |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 47 | |
| 48 | // Add further event names / module references as needed |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | function wsOpen(host, url) { |
| 53 | $log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url); |
| 54 | // tell the server we are ready to receive topo events |
| 55 | wss.sendEvent('topo2Start'); |
| 56 | } |
| 57 | |
| 58 | // bind our event handlers to the web socket service, so that our |
| 59 | // callbacks get invoked for incoming events |
| 60 | function bindHandlers() { |
| 61 | wss.bindHandlers(handlerMap); |
| 62 | $log.debug('topo2 event handlers bound'); |
| 63 | } |
| 64 | |
| 65 | // tell the server we are ready to receive topology events |
| 66 | function start() { |
| 67 | // in case we fail over to a new server, |
| 68 | // listen for wsock-open events |
| 69 | openListener = wss.addOpenListener(wsOpen); |
| 70 | wss.sendEvent('topo2Start'); |
| 71 | $log.debug('topo2 comms started'); |
| 72 | } |
| 73 | |
| 74 | // tell the server we no longer wish to receive topology events |
| 75 | function stop() { |
| 76 | wss.sendEvent('topo2Stop'); |
| 77 | wss.unbindHandlers(handlerMap); |
| 78 | wss.removeOpenListener(openListener); |
| 79 | openListener = null; |
| 80 | $log.debug('topo2 comms stopped'); |
| 81 | } |
| 82 | |
| 83 | // ========================== Main Service Definition |
| 84 | |
| 85 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 86 | .factory('Topo2EventService', [ |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 87 | '$log', 'WebSocketService', 'Topo2ForceService', 'Topo2OverlayService', |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 88 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 89 | function (_$log_, _wss_, _t2fs_, _t2ovs_) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 90 | $log = _$log_; |
| 91 | wss = _wss_; |
| 92 | t2fs = _t2fs_; |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 93 | t2ovs = _t2ovs_; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 94 | |
| 95 | // deferred creation of handler map, so module references are good |
| 96 | createHandlerMap(); |
| 97 | |
| 98 | return { |
| 99 | bindHandlers: bindHandlers, |
| 100 | start: start, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 101 | stop: stop, |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 102 | }; |
| 103 | }]); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 104 | })(); |