blob: 887fe4a9ac268e4400e965c0bd9a12382b6f8170 [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 Huntac4c6f72015-02-03 19:50:53 -080030 var $log, wss, wes, tps, tis, tfs;
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,
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 Huntfd1231a2015-01-26 22:14:51 -080057 var dispatcher = {
58 handleEvent: function (ev) {
Simon Hunt24077f12015-02-04 17:58:21 -080059 var eid = ev.event,
60 api = evApis[eid] || {},
61 eh = api[eid];
62
63 if (eh) {
Simon Huntdc6adea2015-02-09 22:29:36 -080064 $log.debug(' *EVENT* ', eid, ev.payload);
Simon Hunt24077f12015-02-04 17:58:21 -080065 eh(ev.payload);
66 } else {
67 $log.warn('Unknown event (ignored):', ev);
68 }
Simon Huntfd1231a2015-01-26 22:14:51 -080069 },
Simon Hunt24077f12015-02-04 17:58:21 -080070
Simon Huntfd1231a2015-01-26 22:14:51 -080071 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 Huntbb596362015-01-26 21:27:42 -080099 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -0800100 .factory('TopoEventService',
101 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Huntac4c6f72015-02-03 19:50:53 -0800102 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800103
Simon Huntac4c6f72015-02-03 19:50:53 -0800104 function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_, _tfs_) {
Simon Huntbb596362015-01-26 21:27:42 -0800105 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800106 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800107 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800108 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800109 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800110 tfs = _tfs_;
Simon Huntbb596362015-01-26 21:27:42 -0800111
Simon Hunt24077f12015-02-04 17:58:21 -0800112 bindApis();
113
Simon Huntfd1231a2015-01-26 22:14:51 -0800114 // TODO: handle "guiSuccessor" functionality (replace host)
115 // TODO: implement retry on close functionality
Simon Hunt1894d792015-02-04 17:09:20 -0800116
Simon Huntfd1231a2015-01-26 22:14:51 -0800117 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 Huntbb596362015-01-26 21:27:42 -0800136
137 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800138 openSock: openSock,
Simon Hunt1894d792015-02-04 17:09:20 -0800139 closeSock: closeSock,
140 sendEvent: dispatcher.sendEvent
Simon Hunt737c89f2015-01-28 12:23:19 -0800141 };
Simon Huntbb596362015-01-26 21:27:42 -0800142 }]);
143}());