blob: a4124672724e53bfcee54818adab01d3e3250d5b [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 Hunt36fc15c2015-02-12 17:02:58 -080030 var $log, wss, wes, vs, 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');
Simon Hunt36fc15c2015-02-12 17:02:58 -080092 vs.hide();
Simon Huntfd1231a2015-01-26 22:14:51 -080093 }
94
95 function onWsMessage(ev) {
96 dispatcher.handleEvent(ev);
97 }
98
99 function onWsClose(reason) {
100 $log.log('web socket closed; reason=', reason);
101 wsock = null;
Simon Hunt36fc15c2015-02-12 17:02:58 -0800102 vs.show([
103 'Oops!',
104 'Web-socket connection to server closed...',
105 'Try refreshing the page.'
106 ]);
Simon Huntfd1231a2015-01-26 22:14:51 -0800107 }
108
109 // ==========================
110
Simon Huntbb596362015-01-26 21:27:42 -0800111 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -0800112 .factory('TopoEventService',
Simon Hunt36fc15c2015-02-12 17:02:58 -0800113 ['$log', '$location', 'WebSocketService', 'WsEventService', 'VeilService',
Simon Huntac4c6f72015-02-03 19:50:53 -0800114 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Huntf542d842015-02-11 16:20:33 -0800115 'TopoSelectService', 'TopoTrafficService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800116
Simon Hunt36fc15c2015-02-12 17:02:58 -0800117 function (_$log_, $loc, _wss_, _wes_, _vs_,
Simon Huntf542d842015-02-11 16:20:33 -0800118 _tps_, _tis_, _tfs_, _tss_, _tts_) {
Simon Huntbb596362015-01-26 21:27:42 -0800119 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800120 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800121 wes = _wes_;
Simon Hunt36fc15c2015-02-12 17:02:58 -0800122 vs = _vs_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800123 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800124 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800125 tfs = _tfs_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800126 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800127 tts = _tts_;
Simon Huntbb596362015-01-26 21:27:42 -0800128
Simon Hunt24077f12015-02-04 17:58:21 -0800129 bindApis();
130
Simon Huntfd1231a2015-01-26 22:14:51 -0800131 // TODO: handle "guiSuccessor" functionality (replace host)
132 // TODO: implement retry on close functionality
Simon Hunt1894d792015-02-04 17:09:20 -0800133
Simon Huntfd1231a2015-01-26 22:14:51 -0800134 function openSock() {
135 wsock = wss.createWebSocket('topology', {
136 onOpen: onWsOpen,
137 onMessage: onWsMessage,
138 onClose: onWsClose,
139 wsport: $loc.search().wsport
140 });
141 $log.debug('web socket opened:', wsock);
142 }
143
144 function closeSock() {
145 var path;
146 if (wsock) {
147 path = wsock.meta.path;
148 wsock.close();
149 wsock = null;
150 $log.debug('web socket closed. path:', path);
151 }
152 }
Simon Huntbb596362015-01-26 21:27:42 -0800153
154 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800155 openSock: openSock,
Simon Hunt1894d792015-02-04 17:09:20 -0800156 closeSock: closeSock,
157 sendEvent: dispatcher.sendEvent
Simon Hunt737c89f2015-01-28 12:23:19 -0800158 };
Simon Huntbb596362015-01-26 21:27:42 -0800159 }]);
160}());