blob: 640a233e8078a795882b4fd176144addbd54e367 [file] [log] [blame]
Simon Huntf542d842015-02-11 16:20:33 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Huntf542d842015-02-11 16:20:33 -08003 *
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 Traffic Module.
19 Defines behavior for viewing different traffic modes.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Steven Burrows1c2a9682017-07-14 16:52:46 +010026 var $log, flash, wss, api;
Simon Huntf542d842015-02-11 16:20:33 -080027
Simon Huntcaed0412017-08-12 13:49:17 -070028 // function to be replaced by the localization bundle function
29 var topoLion = function (x) {
30 return '#ttraf#' + x + '#';
31 };
32
Simon Huntf542d842015-02-11 16:20:33 -080033 /*
Simon Hunt8d22c4b2015-08-06 16:24:43 -070034 API to topoForce
35 hovered()
36 somethingSelected()
37 selectOrder()
Simon Huntf542d842015-02-11 16:20:33 -080038 */
39
Simon Hunt21281fd2017-03-30 22:28:28 -070040 var allTrafficTypes = [
41 'flowStatsBytes',
42 'portStatsBitSec',
Steven Burrows1c2a9682017-07-14 16:52:46 +010043 'portStatsPktSec',
Simon Hunt21281fd2017-03-30 22:28:28 -070044 ],
Simon Huntcaed0412017-08-12 13:49:17 -070045 allTrafficMsgs = []; // filled in with localized messages
Simon Hunt21281fd2017-03-30 22:28:28 -070046
Simon Huntf542d842015-02-11 16:20:33 -080047 // internal state
Simon Hunt8d22c4b2015-08-06 16:24:43 -070048 var trafficMode = null,
Simon Hunt21281fd2017-03-30 22:28:28 -070049 hoverMode = null,
50 allTrafficIndex = 0;
51
Simon Huntf542d842015-02-11 16:20:33 -080052
Simon Huntf542d842015-02-11 16:20:33 -080053 // === -----------------------------------------------------
Simon Huntf542d842015-02-11 16:20:33 -080054 // Helper functions
55
Simon Huntcaed0412017-08-12 13:49:17 -070056 function setLionBundle(bundle) {
57 $log.debug('topoTraffic: setting Lion bundle');
58 topoLion = bundle;
59 allTrafficMsgs = [
60 topoLion('tr_fl_fstats_bytes'),
61 topoLion('tr_fl_pstats_bits'),
62 topoLion('tr_fl_pstats_pkts'),
63 ];
64 }
65
Simon Hunt8d22c4b2015-08-06 16:24:43 -070066 // invoked in response to change in selection and/or mouseover/out:
Simon Huntd2862c32015-08-24 17:41:51 -070067 function requestTrafficForMode(mouse) {
Simon Hunta17fa672015-08-19 18:42:22 -070068 if (trafficMode === 'flows') {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070069 requestDeviceLinkFlows();
Simon Hunta17fa672015-08-19 18:42:22 -070070 } else if (trafficMode === 'intents') {
Simon Huntd2862c32015-08-24 17:41:51 -070071 if (!mouse || hoverMode === 'intents') {
72 requestRelatedIntents();
73 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -070074 } else {
Simon Huntd2862c32015-08-24 17:41:51 -070075 // do nothing
Simon Hunt8d22c4b2015-08-06 16:24:43 -070076 }
77 }
78
Simon Huntf542d842015-02-11 16:20:33 -080079 function requestDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070080 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080081 var hov = api.hovered();
82
83 function hoverValid() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070084 return hoverMode === 'flows' &&
Simon Huntf542d842015-02-11 16:20:33 -080085 hov && (hov.class === 'device');
86 }
87
Simon Hunt8d22c4b2015-08-06 16:24:43 -070088 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070089 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -080090 ids: api.selectOrder(),
Steven Burrows1c2a9682017-07-14 16:52:46 +010091 hover: hoverValid() ? hov.id : '',
Simon Huntf542d842015-02-11 16:20:33 -080092 });
93 }
94 }
95
96 function requestRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070097 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080098 var hov = api.hovered();
99
100 function hoverValid() {
Prince Pereira46c82d42016-09-19 13:30:50 +0530101 return hoverMode === 'intents' && hov && (
102 hov.class === 'host' ||
103 hov.class === 'device' ||
104 hov.class === 'link');
Simon Huntf542d842015-02-11 16:20:33 -0800105 }
106
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700107 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700108 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -0800109 ids: api.selectOrder(),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100110 hover: hoverValid() ? hov.id : '',
Simon Huntf542d842015-02-11 16:20:33 -0800111 });
112 }
113 }
114
115
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700116 // === -------------------------------------------------------------
117 // Traffic requests invoked from keystrokes or toolbar buttons...
Simon Huntf542d842015-02-11 16:20:33 -0800118
Simon Huntd2862c32015-08-24 17:41:51 -0700119 function cancelTraffic(forced) {
120 if (!trafficMode || (!forced && trafficMode === 'allFlowPort')) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700121 return false;
Simon Huntf542d842015-02-11 16:20:33 -0800122 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700123
124 trafficMode = hoverMode = null;
125 wss.sendEvent('cancelTraffic');
Simon Huntcaed0412017-08-12 13:49:17 -0700126 flash.flash(topoLion('fl_monitoring_canceled'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700127 return true;
Simon Huntf542d842015-02-11 16:20:33 -0800128 }
129
Simon Hunt21281fd2017-03-30 22:28:28 -0700130 function showAllTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700131 trafficMode = 'allFlowPort';
132 hoverMode = null;
Simon Hunt21281fd2017-03-30 22:28:28 -0700133 wss.sendEvent('requestAllTraffic', {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100134 trafficType: allTrafficTypes[allTrafficIndex],
Simon Hunt21281fd2017-03-30 22:28:28 -0700135 });
136 flash.flash(allTrafficMsgs[allTrafficIndex]);
137 allTrafficIndex = (allTrafficIndex + 1) % 3;
Simon Huntf542d842015-02-11 16:20:33 -0800138 }
139
Steven Burrows1c2a9682017-07-14 16:52:46 +0100140 function showDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700141 trafficMode = hoverMode = 'flows';
142 requestDeviceLinkFlows();
Simon Huntcaed0412017-08-12 13:49:17 -0700143 flash.flash(topoLion('tr_fl_dev_flows'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700144 }
Simon Huntf542d842015-02-11 16:20:33 -0800145
Steven Burrows1c2a9682017-07-14 16:52:46 +0100146 function showRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700147 trafficMode = hoverMode = 'intents';
Simon Huntf542d842015-02-11 16:20:33 -0800148 requestRelatedIntents();
Simon Huntcaed0412017-08-12 13:49:17 -0700149 flash.flash(topoLion('tr_fl_rel_paths'));
Simon Huntf542d842015-02-11 16:20:33 -0800150 }
151
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700152 function showPrevIntent() {
153 if (trafficMode === 'intents') {
154 hoverMode = null;
155 wss.sendEvent('requestPrevRelatedIntent');
Simon Huntcaed0412017-08-12 13:49:17 -0700156 flash.flash(topoLion('tr_fl_prev_rel_int'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700157 }
158 }
159
160 function showNextIntent() {
161 if (trafficMode === 'intents') {
162 hoverMode = null;
163 wss.sendEvent('requestNextRelatedIntent');
Simon Huntcaed0412017-08-12 13:49:17 -0700164 flash.flash(topoLion('tr_fl_next_rel_int'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700165 }
166 }
167
168 function showSelectedIntentTraffic() {
169 if (trafficMode === 'intents') {
170 hoverMode = null;
171 wss.sendEvent('requestSelectedIntentTraffic');
Simon Huntcaed0412017-08-12 13:49:17 -0700172 flash.flash(topoLion('tr_fl_traf_on_path'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700173 }
174 }
175
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700176 // force the system to create a single intent selection
177 function selectIntent(data) {
178 trafficMode = 'intents';
179 hoverMode = null;
180 wss.sendEvent('selectIntent', data);
Simon Huntcaed0412017-08-12 13:49:17 -0700181 flash.flash(topoLion('fl_selecting_intent') + ' ' + data.key);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700182 }
183
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700184
185 // === ------------------------------------------------------
186 // action buttons on detail panel (multiple selection)
187
Steven Burrows1c2a9682017-07-14 16:52:46 +0100188 function addHostIntent() {
Simon Huntf542d842015-02-11 16:20:33 -0800189 var so = api.selectOrder();
Simon Huntcaed0412017-08-12 13:49:17 -0700190
Simon Hunt237676b52015-03-10 19:04:26 -0700191 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800192 one: so[0],
193 two: so[1],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100194 ids: so,
Simon Huntf542d842015-02-11 16:20:33 -0800195 });
Simon Huntd2862c32015-08-24 17:41:51 -0700196 trafficMode = 'intents';
197 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700198 flash.flash(topoLion('tr_fl_h2h_flow_added'));
Simon Huntf542d842015-02-11 16:20:33 -0800199 }
200
Steven Burrows1c2a9682017-07-14 16:52:46 +0100201 function removeIntent(d) {
Simon Huntcaed0412017-08-12 13:49:17 -0700202 var action = topoLion(d.intentPurge ? 'purged' : 'withdrawn');
203
Viswanath KSP0f297702016-08-13 18:02:43 +0530204 wss.sendEvent('removeIntent', {
205 appId: d.appId,
206 appName: d.appName,
Viswanath KSP813a20d2016-09-13 04:25:41 +0530207 key: d.key,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100208 purge: d.intentPurge,
Viswanath KSP0f297702016-08-13 18:02:43 +0530209 });
210 trafficMode = 'intents';
211 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700212 flash.flash(topoLion('intent') + ' ' + action);
Viswanath KSP0f297702016-08-13 18:02:43 +0530213 }
214
Steven Burrows1c2a9682017-07-14 16:52:46 +0100215 function resubmitIntent(d) {
Viswanath KSP14aee092016-10-02 01:47:40 +0530216 wss.sendEvent('resubmitIntent', {
217 appId: d.appId,
218 appName: d.appName,
219 key: d.key,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100220 purge: d.intentPurge,
Viswanath KSP14aee092016-10-02 01:47:40 +0530221 });
222 trafficMode = 'intents';
223 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700224 flash.flash(topoLion('intent') + ' ' + topoLion('resubmitted'));
Viswanath KSP14aee092016-10-02 01:47:40 +0530225 }
226
Steven Burrows1c2a9682017-07-14 16:52:46 +0100227 function addMultiSourceIntent() {
Simon Huntf542d842015-02-11 16:20:33 -0800228 var so = api.selectOrder();
Simon Huntcaed0412017-08-12 13:49:17 -0700229
Simon Hunt237676b52015-03-10 19:04:26 -0700230 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800231 src: so.slice(0, so.length - 1),
232 dst: so[so.length - 1],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100233 ids: so,
Simon Huntf542d842015-02-11 16:20:33 -0800234 });
Simon Huntd2862c32015-08-24 17:41:51 -0700235 trafficMode = 'intents';
236 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700237 flash.flash(topoLion('tr_fl_multisrc_flow') + ' ' + topoLion('added'));
Simon Huntf542d842015-02-11 16:20:33 -0800238 }
239
Steven Burrows1c2a9682017-07-14 16:52:46 +0100240 function removeIntents() {
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530241 wss.sendEvent('removeIntents', {});
242 trafficMode = 'intents';
243 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700244 flash.flash(topoLion('intents') + ' ' + topoLion('purged'));
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530245 }
246
Simon Huntf542d842015-02-11 16:20:33 -0800247
Simon Huntf542d842015-02-11 16:20:33 -0800248 // === -----------------------------------------------------
249 // === MODULE DEFINITION ===
250
251 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800252 .factory('TopoTrafficService',
Simon Huntcaed0412017-08-12 13:49:17 -0700253 ['$log', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800254
Steven Burrows1c2a9682017-07-14 16:52:46 +0100255 function (_$log_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800256 $log = _$log_;
Simon Huntf542d842015-02-11 16:20:33 -0800257 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700258 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800259
Simon Huntf542d842015-02-11 16:20:33 -0800260 return {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700261 initTraffic: function (_api_) { api = _api_; },
262 destroyTraffic: function () { },
Simon Huntf542d842015-02-11 16:20:33 -0800263
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700264 // invoked from toolbar overlay buttons or keystrokes
Simon Huntf542d842015-02-11 16:20:33 -0800265 cancelTraffic: cancelTraffic,
Simon Hunt21281fd2017-03-30 22:28:28 -0700266 showAllTraffic: showAllTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700267 showDeviceLinkFlows: showDeviceLinkFlows,
268 showRelatedIntents: showRelatedIntents,
269 showPrevIntent: showPrevIntent,
270 showNextIntent: showNextIntent,
271 showSelectedIntentTraffic: showSelectedIntentTraffic,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700272 selectIntent: selectIntent,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700273
274 // invoked from mouseover/mouseout and selection change
Simon Huntf542d842015-02-11 16:20:33 -0800275 requestTrafficForMode: requestTrafficForMode,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700276
277 // invoked from buttons on detail (multi-select) panel
278 addHostIntent: addHostIntent,
Viswanath KSP0f297702016-08-13 18:02:43 +0530279 addMultiSourceIntent: addMultiSourceIntent,
Viswanath KSP14aee092016-10-02 01:47:40 +0530280 removeIntent: removeIntent,
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530281 resubmitIntent: resubmitIntent,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100282 removeIntents: removeIntents,
Simon Huntcaed0412017-08-12 13:49:17 -0700283
284 setLionBundle: setLionBundle,
Simon Huntf542d842015-02-11 16:20:33 -0800285 };
286 }]);
287}());