blob: 10ed6df08d94163bcdd94d5cd5451dc651c69fd6 [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
22 - dispatches incoming events from the server to the appropriate submodule
23
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 Hunt08f841d02015-02-10 14:39:20 -080030 var $log, wss, wes, tps, tis, tfs, tss;
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 Hunt24077f12015-02-04 17:58:21 -080043 addInstance: tis,
44 updateInstance: tis,
45 removeInstance: tis,
Simon Hunt08f841d02015-02-10 14:39:20 -080046
Simon Hunt24077f12015-02-04 17:58:21 -080047 addDevice: tfs,
48 updateDevice: tfs,
49 removeDevice: tfs,
50 addHost: tfs,
51 updateHost: tfs,
52 removeHost: tfs,
53 addLink: tfs,
54 updateLink: tfs,
55 removeLink: tfs
56
57 // TODO: add remaining event api vectors
58 };
59 }
60
Simon Huntc252aa62015-02-10 16:45:39 -080061 var nilApi = {},
62 dispatcher = {
Simon Huntfd1231a2015-01-26 22:14:51 -080063 handleEvent: function (ev) {
Simon Hunt24077f12015-02-04 17:58:21 -080064 var eid = ev.event,
Simon Huntc252aa62015-02-10 16:45:39 -080065 api = evApis[eid] || nilApi,
Simon Hunt24077f12015-02-04 17:58:21 -080066 eh = api[eid];
67
68 if (eh) {
Simon Huntc252aa62015-02-10 16:45:39 -080069 $log.debug(' << *Rx* ', eid, ev.payload);
Simon Hunt24077f12015-02-04 17:58:21 -080070 eh(ev.payload);
71 } else {
72 $log.warn('Unknown event (ignored):', ev);
73 }
Simon Huntfd1231a2015-01-26 22:14:51 -080074 },
Simon Hunt24077f12015-02-04 17:58:21 -080075
Simon Huntfd1231a2015-01-26 22:14:51 -080076 sendEvent: function (evType, payload) {
77 if (wsock) {
Simon Huntc252aa62015-02-10 16:45:39 -080078 $log.debug(' *Tx* >> ', evType, payload);
Simon Huntfd1231a2015-01-26 22:14:51 -080079 wes.sendEvent(wsock, evType, payload);
80 } else {
81 $log.warn('sendEvent: no websocket open:', evType, payload);
82 }
83 }
84 };
85
86 // === Web Socket functions ===
87
88 function onWsOpen() {
89 $log.debug('web socket opened...');
90 // kick off request for periodic summary data...
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 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 Hunt08f841d02015-02-10 14:39:20 -0800109 'TopoSelectService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800110
Simon Hunt08f841d02015-02-10 14:39:20 -0800111 function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_, _tfs_, _tss_) {
Simon Huntbb596362015-01-26 21:27:42 -0800112 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800113 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800114 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800115 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800116 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800117 tfs = _tfs_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800118 tss = _tss_;
Simon Huntbb596362015-01-26 21:27:42 -0800119
Simon Hunt24077f12015-02-04 17:58:21 -0800120 bindApis();
121
Simon Huntfd1231a2015-01-26 22:14:51 -0800122 // TODO: handle "guiSuccessor" functionality (replace host)
123 // TODO: implement retry on close functionality
Simon Hunt1894d792015-02-04 17:09:20 -0800124
Simon Huntfd1231a2015-01-26 22:14:51 -0800125 function openSock() {
126 wsock = wss.createWebSocket('topology', {
127 onOpen: onWsOpen,
128 onMessage: onWsMessage,
129 onClose: onWsClose,
130 wsport: $loc.search().wsport
131 });
132 $log.debug('web socket opened:', wsock);
133 }
134
135 function closeSock() {
136 var path;
137 if (wsock) {
138 path = wsock.meta.path;
139 wsock.close();
140 wsock = null;
141 $log.debug('web socket closed. path:', path);
142 }
143 }
Simon Huntbb596362015-01-26 21:27:42 -0800144
145 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800146 openSock: openSock,
Simon Hunt1894d792015-02-04 17:09:20 -0800147 closeSock: closeSock,
148 sendEvent: dispatcher.sendEvent
Simon Hunt737c89f2015-01-28 12:23:19 -0800149 };
Simon Huntbb596362015-01-26 21:27:42 -0800150 }]);
151}());