blob: 6cd634faaf30334d142dda6a4b09af5060c1858d [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.
19 Defines event handling for events received from the server.
20 */
21
22(function () {
23 'use strict';
24
Simon Huntfd1231a2015-01-26 22:14:51 -080025 // injected refs
Simon Huntac4c6f72015-02-03 19:50:53 -080026 var $log, wss, wes, tps, tis, tfs;
Simon Huntfd1231a2015-01-26 22:14:51 -080027
28 // internal state
29 var wsock;
Simon Huntbb596362015-01-26 21:27:42 -080030
31 var evHandler = {
32 showSummary: showSummary,
Simon Hunt48e61672015-01-30 14:48:25 -080033 addInstance: addInstance,
34 updateInstance: updateInstance,
Simon Huntac4c6f72015-02-03 19:50:53 -080035 removeInstance: removeInstance,
36 addDevice: addDevice,
37 updateDevice: updateDevice
Simon Huntfd1231a2015-01-26 22:14:51 -080038 // TODO: implement remaining handlers..
39
Simon Huntbb596362015-01-26 21:27:42 -080040 };
41
42 function unknownEvent(ev) {
43 $log.warn('Unknown event (ignored):', ev);
44 }
45
46 // === Event Handlers ===
47
48 function showSummary(ev) {
Simon Huntb0ec1e52015-01-28 18:13:49 -080049 $log.debug(' **** Show Summary **** ', ev.payload);
50 tps.showSummary(ev.payload);
Simon Huntbb596362015-01-26 21:27:42 -080051 }
52
53 function addInstance(ev) {
Simon Hunt4b668592015-01-29 17:33:53 -080054 $log.debug(' **** Add Instance **** ', ev.payload);
55 tis.addInstance(ev.payload);
Simon Huntbb596362015-01-26 21:27:42 -080056 }
57
Simon Hunt48e61672015-01-30 14:48:25 -080058 function updateInstance(ev) {
59 $log.debug(' **** Update Instance **** ', ev.payload);
60 tis.updateInstance(ev.payload);
61 }
62
63 function removeInstance(ev) {
64 $log.debug(' **** Remove Instance **** ', ev.payload);
65 tis.removeInstance(ev.payload);
66 }
67
Simon Huntac4c6f72015-02-03 19:50:53 -080068 function addDevice(ev) {
69 $log.debug(' **** Add Device **** ', ev.payload);
70 tfs.addDevice(ev.payload);
71 }
72
73 function updateDevice(ev) {
74 $log.debug(' **** Update Device **** ', ev.payload);
75 tfs.updateDevice(ev.payload);
76 }
77
Simon Huntfd1231a2015-01-26 22:14:51 -080078 // ==========================
79
80 var dispatcher = {
81 handleEvent: function (ev) {
82 (evHandler[ev.event] || unknownEvent)(ev);
83 },
84 sendEvent: function (evType, payload) {
85 if (wsock) {
86 wes.sendEvent(wsock, evType, payload);
87 } else {
88 $log.warn('sendEvent: no websocket open:', evType, payload);
89 }
90 }
91 };
92
93 // === Web Socket functions ===
94
95 function onWsOpen() {
96 $log.debug('web socket opened...');
97 // kick off request for periodic summary data...
98 dispatcher.sendEvent('requestSummary');
99 }
100
101 function onWsMessage(ev) {
102 dispatcher.handleEvent(ev);
103 }
104
105 function onWsClose(reason) {
106 $log.log('web socket closed; reason=', reason);
107 wsock = null;
108 }
109
110 // ==========================
111
Simon Huntbb596362015-01-26 21:27:42 -0800112 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -0800113 .factory('TopoEventService',
114 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Huntac4c6f72015-02-03 19:50:53 -0800115 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800116
Simon Huntac4c6f72015-02-03 19:50:53 -0800117 function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_, _tfs_) {
Simon Huntbb596362015-01-26 21:27:42 -0800118 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800119 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800120 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800121 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800122 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800123 tfs = _tfs_;
Simon Huntbb596362015-01-26 21:27:42 -0800124
Simon Huntfd1231a2015-01-26 22:14:51 -0800125 function bindDispatcher(TopoDomElementsPassedHere) {
126 // TODO: store refs to topo DOM elements...
127
128 return dispatcher;
129 }
130
131 // TODO: handle "guiSuccessor" functionality (replace host)
132 // TODO: implement retry on close functionality
133 function openSock() {
134 wsock = wss.createWebSocket('topology', {
135 onOpen: onWsOpen,
136 onMessage: onWsMessage,
137 onClose: onWsClose,
138 wsport: $loc.search().wsport
139 });
140 $log.debug('web socket opened:', wsock);
141 }
142
143 function closeSock() {
144 var path;
145 if (wsock) {
146 path = wsock.meta.path;
147 wsock.close();
148 wsock = null;
149 $log.debug('web socket closed. path:', path);
150 }
151 }
Simon Huntbb596362015-01-26 21:27:42 -0800152
153 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800154 bindDispatcher: bindDispatcher,
155 openSock: openSock,
156 closeSock: closeSock
Simon Hunt737c89f2015-01-28 12:23:19 -0800157 };
Simon Huntbb596362015-01-26 21:27:42 -0800158 }]);
159}());