blob: edebb5264d0f891fc6ce615a80b7f24ac4d6af36 [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 Huntfd1231a2015-01-26 22:14:51 -080061 var dispatcher = {
62 handleEvent: function (ev) {
Simon Hunt24077f12015-02-04 17:58:21 -080063 var eid = ev.event,
64 api = evApis[eid] || {},
65 eh = api[eid];
66
67 if (eh) {
Simon Huntdc6adea2015-02-09 22:29:36 -080068 $log.debug(' *EVENT* ', eid, ev.payload);
Simon Hunt24077f12015-02-04 17:58:21 -080069 eh(ev.payload);
70 } else {
71 $log.warn('Unknown event (ignored):', ev);
72 }
Simon Huntfd1231a2015-01-26 22:14:51 -080073 },
Simon Hunt24077f12015-02-04 17:58:21 -080074
Simon Huntfd1231a2015-01-26 22:14:51 -080075 sendEvent: function (evType, payload) {
76 if (wsock) {
77 wes.sendEvent(wsock, evType, payload);
78 } else {
79 $log.warn('sendEvent: no websocket open:', evType, payload);
80 }
81 }
82 };
83
84 // === Web Socket functions ===
85
86 function onWsOpen() {
87 $log.debug('web socket opened...');
88 // kick off request for periodic summary data...
89 dispatcher.sendEvent('requestSummary');
90 }
91
92 function onWsMessage(ev) {
93 dispatcher.handleEvent(ev);
94 }
95
96 function onWsClose(reason) {
97 $log.log('web socket closed; reason=', reason);
98 wsock = null;
99 }
100
101 // ==========================
102
Simon Huntbb596362015-01-26 21:27:42 -0800103 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -0800104 .factory('TopoEventService',
105 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Huntac4c6f72015-02-03 19:50:53 -0800106 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800107 'TopoSelectService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800108
Simon Hunt08f841d02015-02-10 14:39:20 -0800109 function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_, _tfs_, _tss_) {
Simon Huntbb596362015-01-26 21:27:42 -0800110 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800111 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800112 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800113 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800114 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800115 tfs = _tfs_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800116 tss = _tss_;
Simon Huntbb596362015-01-26 21:27:42 -0800117
Simon Hunt24077f12015-02-04 17:58:21 -0800118 bindApis();
119
Simon Huntfd1231a2015-01-26 22:14:51 -0800120 // TODO: handle "guiSuccessor" functionality (replace host)
121 // TODO: implement retry on close functionality
Simon Hunt1894d792015-02-04 17:09:20 -0800122
Simon Huntfd1231a2015-01-26 22:14:51 -0800123 function openSock() {
124 wsock = wss.createWebSocket('topology', {
125 onOpen: onWsOpen,
126 onMessage: onWsMessage,
127 onClose: onWsClose,
128 wsport: $loc.search().wsport
129 });
130 $log.debug('web socket opened:', wsock);
131 }
132
133 function closeSock() {
134 var path;
135 if (wsock) {
136 path = wsock.meta.path;
137 wsock.close();
138 wsock = null;
139 $log.debug('web socket closed. path:', path);
140 }
141 }
Simon Huntbb596362015-01-26 21:27:42 -0800142
143 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800144 openSock: openSock,
Simon Hunt1894d792015-02-04 17:09:20 -0800145 closeSock: closeSock,
146 sendEvent: dispatcher.sendEvent
Simon Hunt737c89f2015-01-28 12:23:19 -0800147 };
Simon Huntbb596362015-01-26 21:27:42 -0800148 }]);
149}());