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 | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 30 | var $log, wss, wes, tps, tis, tfs, tss, tts; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 31 | |
| 32 | // internal state |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 33 | var wsock, evApis; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 34 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 35 | // ========================== |
| 36 | |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 37 | function bindApis() { |
| 38 | evApis = { |
| 39 | showSummary: tps, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 40 | |
| 41 | showDetails: tss, |
| 42 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 43 | showTraffic: tts, |
| 44 | |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 45 | addInstance: tis, |
| 46 | updateInstance: tis, |
| 47 | removeInstance: tis, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 49 | addDevice: tfs, |
| 50 | updateDevice: tfs, |
| 51 | removeDevice: tfs, |
| 52 | addHost: tfs, |
| 53 | updateHost: tfs, |
| 54 | removeHost: tfs, |
| 55 | addLink: tfs, |
| 56 | updateLink: tfs, |
| 57 | removeLink: tfs |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 58 | }; |
| 59 | } |
| 60 | |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 61 | var nilApi = {}, |
| 62 | dispatcher = { |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame^] | 63 | handleEvent: function (ev) { |
| 64 | var eid = ev.event, |
| 65 | api = evApis[eid] || nilApi, |
| 66 | eh = api[eid]; |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 67 | |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame^] | 68 | if (eh) { |
| 69 | $log.debug(' << *Rx* ', eid, ev.payload); |
| 70 | eh(ev.payload); |
| 71 | } else { |
| 72 | $log.warn('Unknown event (ignored):', ev); |
| 73 | } |
| 74 | }, |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 75 | |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame^] | 76 | sendEvent: function (evType, payload) { |
| 77 | if (wsock) { |
| 78 | $log.debug(' *Tx* >> ', evType, payload); |
| 79 | wes.sendEvent(wsock, evType, payload); |
| 80 | } else { |
| 81 | $log.warn('sendEvent: no websocket open:', evType, payload); |
| 82 | } |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 83 | } |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame^] | 84 | }; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 85 | |
| 86 | // === Web Socket functions === |
| 87 | |
| 88 | function onWsOpen() { |
| 89 | $log.debug('web socket opened...'); |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame^] | 90 | // start by requesting periodic summary data... |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 91 | dispatcher.sendEvent('requestSummary'); |
| 92 | } |
| 93 | |
| 94 | function onWsMessage(ev) { |
| 95 | dispatcher.handleEvent(ev); |
| 96 | } |
| 97 | |
| 98 | function onWsClose(reason) { |
| 99 | $log.log('web socket closed; reason=', reason); |
| 100 | wsock = null; |
| 101 | } |
| 102 | |
| 103 | // ========================== |
| 104 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 105 | angular.module('ovTopo') |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 106 | .factory('TopoEventService', |
| 107 | ['$log', '$location', 'WebSocketService', 'WsEventService', |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 108 | 'TopoPanelService', 'TopoInstService', 'TopoForceService', |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 109 | 'TopoSelectService', 'TopoTrafficService', |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 110 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 111 | function (_$log_, $loc, _wss_, _wes_, |
| 112 | _tps_, _tis_, _tfs_, _tss_, _tts_) { |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 113 | $log = _$log_; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 114 | wss = _wss_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 115 | wes = _wes_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 116 | tps = _tps_; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 117 | tis = _tis_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 118 | tfs = _tfs_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 119 | tss = _tss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 120 | tts = _tts_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 121 | |
Simon Hunt | 24077f1 | 2015-02-04 17:58:21 -0800 | [diff] [blame] | 122 | bindApis(); |
| 123 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 124 | // TODO: handle "guiSuccessor" functionality (replace host) |
| 125 | // TODO: implement retry on close functionality |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 126 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 127 | function openSock() { |
| 128 | wsock = wss.createWebSocket('topology', { |
| 129 | onOpen: onWsOpen, |
| 130 | onMessage: onWsMessage, |
| 131 | onClose: onWsClose, |
| 132 | wsport: $loc.search().wsport |
| 133 | }); |
| 134 | $log.debug('web socket opened:', wsock); |
| 135 | } |
| 136 | |
| 137 | function closeSock() { |
| 138 | var path; |
| 139 | if (wsock) { |
| 140 | path = wsock.meta.path; |
| 141 | wsock.close(); |
| 142 | wsock = null; |
| 143 | $log.debug('web socket closed. path:', path); |
| 144 | } |
| 145 | } |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 146 | |
| 147 | return { |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 148 | openSock: openSock, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 149 | closeSock: closeSock, |
| 150 | sendEvent: dispatcher.sendEvent |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 151 | }; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 152 | }]); |
| 153 | }()); |