blob: 3a68f921a87e4c7403ceb49779a09eaf42ca7ec4 [file] [log] [blame]
Simon Huntbb596362015-01-26 21:27:42 -08001/*
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 Hunt24077f12015-02-04 17:58:21 -080019
20 Defines the conduit between the client and the server:
21 - provides a clean API for sending events to the server
Simon Hunt75ec9692015-02-11 16:40:36 -080022 - dispatches incoming events from the server to the appropriate sub-module
Simon Hunt24077f12015-02-04 17:58:21 -080023
Simon Huntbb596362015-01-26 21:27:42 -080024 */
25
26(function () {
27 'use strict';
28
Simon Huntfd1231a2015-01-26 22:14:51 -080029 // injected refs
Simon Huntf542d842015-02-11 16:20:33 -080030 var $log, wss, wes, tps, tis, tfs, tss, tts;
Simon Huntfd1231a2015-01-26 22:14:51 -080031
32 // internal state
Simon Hunt24077f12015-02-04 17:58:21 -080033 var wsock, evApis;
Simon Hunt1894d792015-02-04 17:09:20 -080034
Simon Huntfd1231a2015-01-26 22:14:51 -080035 // ==========================
36
Simon Hunt24077f12015-02-04 17:58:21 -080037 function bindApis() {
38 evApis = {
39 showSummary: tps,
Simon Hunt08f841d02015-02-10 14:39:20 -080040
41 showDetails: tss,
42
Simon Huntf542d842015-02-11 16:20:33 -080043 showTraffic: tts,
44
Simon Hunt24077f12015-02-04 17:58:21 -080045 addInstance: tis,
46 updateInstance: tis,
47 removeInstance: tis,
Simon Hunt08f841d02015-02-10 14:39:20 -080048
Simon Hunt24077f12015-02-04 17:58:21 -080049 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 Hunt24077f12015-02-04 17:58:21 -080058 };
59 }
60
Simon Huntc252aa62015-02-10 16:45:39 -080061 var nilApi = {},
62 dispatcher = {
Simon Hunt75ec9692015-02-11 16:40:36 -080063 handleEvent: function (ev) {
64 var eid = ev.event,
65 api = evApis[eid] || nilApi,
66 eh = api[eid];
Simon Hunt24077f12015-02-04 17:58:21 -080067
Simon Hunt75ec9692015-02-11 16:40:36 -080068 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 Hunt24077f12015-02-04 17:58:21 -080075
Simon Hunt75ec9692015-02-11 16:40:36 -080076 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 Huntfd1231a2015-01-26 22:14:51 -080083 }
Simon Hunt75ec9692015-02-11 16:40:36 -080084 };
Simon Huntfd1231a2015-01-26 22:14:51 -080085
86 // === Web Socket functions ===
87
88 function onWsOpen() {
89 $log.debug('web socket opened...');
Simon Hunt75ec9692015-02-11 16:40:36 -080090 // start by requesting periodic summary data...
Simon Huntfd1231a2015-01-26 22:14:51 -080091 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 Huntbb596362015-01-26 21:27:42 -0800105 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -0800106 .factory('TopoEventService',
107 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Huntac4c6f72015-02-03 19:50:53 -0800108 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Huntf542d842015-02-11 16:20:33 -0800109 'TopoSelectService', 'TopoTrafficService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800110
Simon Huntf542d842015-02-11 16:20:33 -0800111 function (_$log_, $loc, _wss_, _wes_,
112 _tps_, _tis_, _tfs_, _tss_, _tts_) {
Simon Huntbb596362015-01-26 21:27:42 -0800113 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800114 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800115 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800116 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800117 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800118 tfs = _tfs_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800119 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800120 tts = _tts_;
Simon Huntbb596362015-01-26 21:27:42 -0800121
Simon Hunt24077f12015-02-04 17:58:21 -0800122 bindApis();
123
Simon Huntfd1231a2015-01-26 22:14:51 -0800124 // TODO: handle "guiSuccessor" functionality (replace host)
125 // TODO: implement retry on close functionality
Simon Hunt1894d792015-02-04 17:09:20 -0800126
Simon Huntfd1231a2015-01-26 22:14:51 -0800127 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 Huntbb596362015-01-26 21:27:42 -0800146
147 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800148 openSock: openSock,
Simon Hunt1894d792015-02-04 17:09:20 -0800149 closeSock: closeSock,
150 sendEvent: dispatcher.sendEvent
Simon Hunt737c89f2015-01-28 12:23:19 -0800151 };
Simon Huntbb596362015-01-26 21:27:42 -0800152 }]);
153}());