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