blob: e39e11c051adf8cdd4b9bc029f1ddcf87b3b3dae [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,
Simon Hunt1894d792015-02-04 17:09:20 -080037 updateDevice: updateDevice,
38 removeDevice: removeDevice,
39 addHost: addHost,
40 updateHost: updateHost,
41 removeHost: removeHost,
42 addLink: addLink,
43 updateLink: updateLink,
44 removeLink: removeLink
Simon Huntfd1231a2015-01-26 22:14:51 -080045
Simon Hunt1894d792015-02-04 17:09:20 -080046 // TODO: implement remaining handlers..
Simon Huntbb596362015-01-26 21:27:42 -080047 };
48
49 function unknownEvent(ev) {
50 $log.warn('Unknown event (ignored):', ev);
51 }
52
53 // === Event Handlers ===
54
Simon Hunt1894d792015-02-04 17:09:20 -080055 // NOTE: --- once these are done, we will collapse them into
56 // a more compact data structure... but for now, write in full..
57
Simon Huntbb596362015-01-26 21:27:42 -080058 function showSummary(ev) {
Simon Huntb0ec1e52015-01-28 18:13:49 -080059 $log.debug(' **** Show Summary **** ', ev.payload);
60 tps.showSummary(ev.payload);
Simon Huntbb596362015-01-26 21:27:42 -080061 }
62
63 function addInstance(ev) {
Simon Hunt4b668592015-01-29 17:33:53 -080064 $log.debug(' **** Add Instance **** ', ev.payload);
65 tis.addInstance(ev.payload);
Simon Huntbb596362015-01-26 21:27:42 -080066 }
67
Simon Hunt48e61672015-01-30 14:48:25 -080068 function updateInstance(ev) {
69 $log.debug(' **** Update Instance **** ', ev.payload);
70 tis.updateInstance(ev.payload);
71 }
72
73 function removeInstance(ev) {
74 $log.debug(' **** Remove Instance **** ', ev.payload);
75 tis.removeInstance(ev.payload);
76 }
77
Simon Huntac4c6f72015-02-03 19:50:53 -080078 function addDevice(ev) {
79 $log.debug(' **** Add Device **** ', ev.payload);
80 tfs.addDevice(ev.payload);
81 }
82
83 function updateDevice(ev) {
84 $log.debug(' **** Update Device **** ', ev.payload);
85 tfs.updateDevice(ev.payload);
86 }
87
Simon Hunt1894d792015-02-04 17:09:20 -080088 function removeDevice(ev) {
89 $log.debug(' **** Remove Device **** ', ev.payload);
90 tfs.removeDevice(ev.payload);
91 }
92
93 function addHost(ev) {
94 $log.debug(' **** Add Host **** ', ev.payload);
95 tfs.addHost(ev.payload);
96 }
97
98 function updateHost(ev) {
99 $log.debug(' **** Update Host **** ', ev.payload);
100 tfs.updateHost(ev.payload);
101 }
102
103 function removeHost(ev) {
104 $log.debug(' **** Remove Host **** ', ev.payload);
105 tfs.removeHost(ev.payload);
106 }
107
108 function addLink(ev) {
109 $log.debug(' **** Add Link **** ', ev.payload);
110 tfs.addLink(ev.payload);
111 }
112
113 function updateLink(ev) {
114 $log.debug(' **** Update Link **** ', ev.payload);
115 tfs.updateLink(ev.payload);
116 }
117
118 function removeLink(ev) {
119 $log.debug(' **** Remove Link **** ', ev.payload);
120 tfs.removeLink(ev.payload);
121 }
122
123
Simon Huntfd1231a2015-01-26 22:14:51 -0800124 // ==========================
125
126 var dispatcher = {
127 handleEvent: function (ev) {
128 (evHandler[ev.event] || unknownEvent)(ev);
129 },
130 sendEvent: function (evType, payload) {
131 if (wsock) {
132 wes.sendEvent(wsock, evType, payload);
133 } else {
134 $log.warn('sendEvent: no websocket open:', evType, payload);
135 }
136 }
137 };
138
139 // === Web Socket functions ===
140
141 function onWsOpen() {
142 $log.debug('web socket opened...');
143 // kick off request for periodic summary data...
144 dispatcher.sendEvent('requestSummary');
145 }
146
147 function onWsMessage(ev) {
148 dispatcher.handleEvent(ev);
149 }
150
151 function onWsClose(reason) {
152 $log.log('web socket closed; reason=', reason);
153 wsock = null;
154 }
155
156 // ==========================
157
Simon Huntbb596362015-01-26 21:27:42 -0800158 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -0800159 .factory('TopoEventService',
160 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Huntac4c6f72015-02-03 19:50:53 -0800161 'TopoPanelService', 'TopoInstService', 'TopoForceService',
Simon Huntfd1231a2015-01-26 22:14:51 -0800162
Simon Huntac4c6f72015-02-03 19:50:53 -0800163 function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_, _tfs_) {
Simon Huntbb596362015-01-26 21:27:42 -0800164 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -0800165 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -0800166 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800167 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -0800168 tis = _tis_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800169 tfs = _tfs_;
Simon Huntbb596362015-01-26 21:27:42 -0800170
Simon Huntfd1231a2015-01-26 22:14:51 -0800171 // TODO: handle "guiSuccessor" functionality (replace host)
172 // TODO: implement retry on close functionality
Simon Hunt1894d792015-02-04 17:09:20 -0800173
Simon Huntfd1231a2015-01-26 22:14:51 -0800174 function openSock() {
175 wsock = wss.createWebSocket('topology', {
176 onOpen: onWsOpen,
177 onMessage: onWsMessage,
178 onClose: onWsClose,
179 wsport: $loc.search().wsport
180 });
181 $log.debug('web socket opened:', wsock);
182 }
183
184 function closeSock() {
185 var path;
186 if (wsock) {
187 path = wsock.meta.path;
188 wsock.close();
189 wsock = null;
190 $log.debug('web socket closed. path:', path);
191 }
192 }
Simon Huntbb596362015-01-26 21:27:42 -0800193
194 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800195 openSock: openSock,
Simon Hunt1894d792015-02-04 17:09:20 -0800196 closeSock: closeSock,
197 sendEvent: dispatcher.sendEvent
Simon Hunt737c89f2015-01-28 12:23:19 -0800198 };
Simon Huntbb596362015-01-26 21:27:42 -0800199 }]);
200}());