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. |
| 19 | Defines event handling for events received from the server. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 25 | // injected refs |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame^] | 26 | var $log, wss, wes, tps, tis, tfs; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 27 | |
| 28 | // internal state |
| 29 | var wsock; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 30 | |
| 31 | var evHandler = { |
| 32 | showSummary: showSummary, |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 33 | addInstance: addInstance, |
| 34 | updateInstance: updateInstance, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame^] | 35 | removeInstance: removeInstance, |
| 36 | addDevice: addDevice, |
| 37 | updateDevice: updateDevice |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 38 | // TODO: implement remaining handlers.. |
| 39 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | function unknownEvent(ev) { |
| 43 | $log.warn('Unknown event (ignored):', ev); |
| 44 | } |
| 45 | |
| 46 | // === Event Handlers === |
| 47 | |
| 48 | function showSummary(ev) { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 49 | $log.debug(' **** Show Summary **** ', ev.payload); |
| 50 | tps.showSummary(ev.payload); |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | function addInstance(ev) { |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 54 | $log.debug(' **** Add Instance **** ', ev.payload); |
| 55 | tis.addInstance(ev.payload); |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 58 | function updateInstance(ev) { |
| 59 | $log.debug(' **** Update Instance **** ', ev.payload); |
| 60 | tis.updateInstance(ev.payload); |
| 61 | } |
| 62 | |
| 63 | function removeInstance(ev) { |
| 64 | $log.debug(' **** Remove Instance **** ', ev.payload); |
| 65 | tis.removeInstance(ev.payload); |
| 66 | } |
| 67 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame^] | 68 | function addDevice(ev) { |
| 69 | $log.debug(' **** Add Device **** ', ev.payload); |
| 70 | tfs.addDevice(ev.payload); |
| 71 | } |
| 72 | |
| 73 | function updateDevice(ev) { |
| 74 | $log.debug(' **** Update Device **** ', ev.payload); |
| 75 | tfs.updateDevice(ev.payload); |
| 76 | } |
| 77 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 78 | // ========================== |
| 79 | |
| 80 | var dispatcher = { |
| 81 | handleEvent: function (ev) { |
| 82 | (evHandler[ev.event] || unknownEvent)(ev); |
| 83 | }, |
| 84 | sendEvent: function (evType, payload) { |
| 85 | if (wsock) { |
| 86 | wes.sendEvent(wsock, evType, payload); |
| 87 | } else { |
| 88 | $log.warn('sendEvent: no websocket open:', evType, payload); |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | // === Web Socket functions === |
| 94 | |
| 95 | function onWsOpen() { |
| 96 | $log.debug('web socket opened...'); |
| 97 | // kick off request for periodic summary data... |
| 98 | dispatcher.sendEvent('requestSummary'); |
| 99 | } |
| 100 | |
| 101 | function onWsMessage(ev) { |
| 102 | dispatcher.handleEvent(ev); |
| 103 | } |
| 104 | |
| 105 | function onWsClose(reason) { |
| 106 | $log.log('web socket closed; reason=', reason); |
| 107 | wsock = null; |
| 108 | } |
| 109 | |
| 110 | // ========================== |
| 111 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 112 | angular.module('ovTopo') |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 113 | .factory('TopoEventService', |
| 114 | ['$log', '$location', 'WebSocketService', 'WsEventService', |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame^] | 115 | 'TopoPanelService', 'TopoInstService', 'TopoForceService', |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 116 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame^] | 117 | function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_, _tfs_) { |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 118 | $log = _$log_; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 119 | wss = _wss_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 120 | wes = _wes_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 121 | tps = _tps_; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 122 | tis = _tis_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame^] | 123 | tfs = _tfs_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 124 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 125 | function bindDispatcher(TopoDomElementsPassedHere) { |
| 126 | // TODO: store refs to topo DOM elements... |
| 127 | |
| 128 | return dispatcher; |
| 129 | } |
| 130 | |
| 131 | // TODO: handle "guiSuccessor" functionality (replace host) |
| 132 | // TODO: implement retry on close functionality |
| 133 | function openSock() { |
| 134 | wsock = wss.createWebSocket('topology', { |
| 135 | onOpen: onWsOpen, |
| 136 | onMessage: onWsMessage, |
| 137 | onClose: onWsClose, |
| 138 | wsport: $loc.search().wsport |
| 139 | }); |
| 140 | $log.debug('web socket opened:', wsock); |
| 141 | } |
| 142 | |
| 143 | function closeSock() { |
| 144 | var path; |
| 145 | if (wsock) { |
| 146 | path = wsock.meta.path; |
| 147 | wsock.close(); |
| 148 | wsock = null; |
| 149 | $log.debug('web socket closed. path:', path); |
| 150 | } |
| 151 | } |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 152 | |
| 153 | return { |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 154 | bindDispatcher: bindDispatcher, |
| 155 | openSock: openSock, |
| 156 | closeSock: closeSock |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 157 | }; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 158 | }]); |
| 159 | }()); |