blob: c643494132bc5b490eb2fd03d356931181b8d4db [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,
Thomas Vachuska2b4de872021-03-30 16:31:34 -070050 allTrafficIndex = 0,
51 customTrafficIndex = 0;
Simon Hunt21281fd2017-03-30 22:28:28 -070052
Simon Huntf542d842015-02-11 16:20:33 -080053
Simon Huntf542d842015-02-11 16:20:33 -080054 // === -----------------------------------------------------
Simon Huntf542d842015-02-11 16:20:33 -080055 // Helper functions
56
Simon Huntcaed0412017-08-12 13:49:17 -070057 function setLionBundle(bundle) {
58 $log.debug('topoTraffic: setting Lion bundle');
59 topoLion = bundle;
60 allTrafficMsgs = [
61 topoLion('tr_fl_fstats_bytes'),
62 topoLion('tr_fl_pstats_bits'),
63 topoLion('tr_fl_pstats_pkts'),
64 ];
65 }
66
Simon Hunt8d22c4b2015-08-06 16:24:43 -070067 // invoked in response to change in selection and/or mouseover/out:
Simon Huntd2862c32015-08-24 17:41:51 -070068 function requestTrafficForMode(mouse) {
Simon Hunta17fa672015-08-19 18:42:22 -070069 if (trafficMode === 'flows') {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070070 requestDeviceLinkFlows();
Simon Hunta17fa672015-08-19 18:42:22 -070071 } else if (trafficMode === 'intents') {
Simon Huntd2862c32015-08-24 17:41:51 -070072 if (!mouse || hoverMode === 'intents') {
73 requestRelatedIntents();
74 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -070075 } else {
Simon Huntd2862c32015-08-24 17:41:51 -070076 // do nothing
Simon Hunt8d22c4b2015-08-06 16:24:43 -070077 }
78 }
79
Simon Huntf542d842015-02-11 16:20:33 -080080 function requestDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070081 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080082 var hov = api.hovered();
83
84 function hoverValid() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070085 return hoverMode === 'flows' &&
Simon Huntf542d842015-02-11 16:20:33 -080086 hov && (hov.class === 'device');
87 }
88
Simon Hunt8d22c4b2015-08-06 16:24:43 -070089 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070090 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -080091 ids: api.selectOrder(),
Steven Burrows1c2a9682017-07-14 16:52:46 +010092 hover: hoverValid() ? hov.id : '',
Simon Huntf542d842015-02-11 16:20:33 -080093 });
94 }
95 }
96
97 function requestRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070098 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080099 var hov = api.hovered();
100
101 function hoverValid() {
Prince Pereira46c82d42016-09-19 13:30:50 +0530102 return hoverMode === 'intents' && hov && (
103 hov.class === 'host' ||
104 hov.class === 'device' ||
105 hov.class === 'link');
Simon Huntf542d842015-02-11 16:20:33 -0800106 }
107
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700108 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700109 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -0800110 ids: api.selectOrder(),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100111 hover: hoverValid() ? hov.id : '',
Simon Huntf542d842015-02-11 16:20:33 -0800112 });
113 }
114 }
115
116
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700117 // === -------------------------------------------------------------
118 // Traffic requests invoked from keystrokes or toolbar buttons...
Simon Huntf542d842015-02-11 16:20:33 -0800119
Simon Huntd2862c32015-08-24 17:41:51 -0700120 function cancelTraffic(forced) {
121 if (!trafficMode || (!forced && trafficMode === 'allFlowPort')) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700122 return false;
Simon Huntf542d842015-02-11 16:20:33 -0800123 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700124
125 trafficMode = hoverMode = null;
126 wss.sendEvent('cancelTraffic');
Simon Huntcaed0412017-08-12 13:49:17 -0700127 flash.flash(topoLion('fl_monitoring_canceled'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700128 return true;
Simon Huntf542d842015-02-11 16:20:33 -0800129 }
130
Simon Hunt21281fd2017-03-30 22:28:28 -0700131 function showAllTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700132 trafficMode = 'allFlowPort';
133 hoverMode = null;
Simon Hunt21281fd2017-03-30 22:28:28 -0700134 wss.sendEvent('requestAllTraffic', {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100135 trafficType: allTrafficTypes[allTrafficIndex],
Simon Hunt21281fd2017-03-30 22:28:28 -0700136 });
137 flash.flash(allTrafficMsgs[allTrafficIndex]);
138 allTrafficIndex = (allTrafficIndex + 1) % 3;
Simon Huntf542d842015-02-11 16:20:33 -0800139 }
140
Thomas Vachuska2b4de872021-03-30 16:31:34 -0700141 function showCustomTraffic() {
142 trafficMode = 'allCustom';
143 hoverMode = null;
144 wss.sendEvent('requestCustomTraffic', {
145 index: customTrafficIndex,
146 });
147 flash.flash('Custom Traffic');
148 customTrafficIndex = customTrafficIndex + 1;
149 }
150
Steven Burrows1c2a9682017-07-14 16:52:46 +0100151 function showDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700152 trafficMode = hoverMode = 'flows';
153 requestDeviceLinkFlows();
Simon Huntcaed0412017-08-12 13:49:17 -0700154 flash.flash(topoLion('tr_fl_dev_flows'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700155 }
Simon Huntf542d842015-02-11 16:20:33 -0800156
Steven Burrows1c2a9682017-07-14 16:52:46 +0100157 function showRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700158 trafficMode = hoverMode = 'intents';
Simon Huntf542d842015-02-11 16:20:33 -0800159 requestRelatedIntents();
Simon Huntcaed0412017-08-12 13:49:17 -0700160 flash.flash(topoLion('tr_fl_rel_paths'));
Simon Huntf542d842015-02-11 16:20:33 -0800161 }
162
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700163 function showPrevIntent() {
164 if (trafficMode === 'intents') {
165 hoverMode = null;
166 wss.sendEvent('requestPrevRelatedIntent');
Simon Huntcaed0412017-08-12 13:49:17 -0700167 flash.flash(topoLion('tr_fl_prev_rel_int'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700168 }
169 }
170
171 function showNextIntent() {
172 if (trafficMode === 'intents') {
173 hoverMode = null;
174 wss.sendEvent('requestNextRelatedIntent');
Simon Huntcaed0412017-08-12 13:49:17 -0700175 flash.flash(topoLion('tr_fl_next_rel_int'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700176 }
177 }
178
179 function showSelectedIntentTraffic() {
180 if (trafficMode === 'intents') {
181 hoverMode = null;
182 wss.sendEvent('requestSelectedIntentTraffic');
Simon Huntcaed0412017-08-12 13:49:17 -0700183 flash.flash(topoLion('tr_fl_traf_on_path'));
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700184 }
185 }
186
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700187 // force the system to create a single intent selection
188 function selectIntent(data) {
189 trafficMode = 'intents';
190 hoverMode = null;
191 wss.sendEvent('selectIntent', data);
Simon Huntcaed0412017-08-12 13:49:17 -0700192 flash.flash(topoLion('fl_selecting_intent') + ' ' + data.key);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700193 }
194
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700195
196 // === ------------------------------------------------------
197 // action buttons on detail panel (multiple selection)
198
Steven Burrows1c2a9682017-07-14 16:52:46 +0100199 function addHostIntent() {
Simon Huntf542d842015-02-11 16:20:33 -0800200 var so = api.selectOrder();
Simon Huntcaed0412017-08-12 13:49:17 -0700201
Simon Hunt237676b52015-03-10 19:04:26 -0700202 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800203 one: so[0],
204 two: so[1],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100205 ids: so,
Simon Huntf542d842015-02-11 16:20:33 -0800206 });
Simon Huntd2862c32015-08-24 17:41:51 -0700207 trafficMode = 'intents';
208 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700209 flash.flash(topoLion('tr_fl_h2h_flow_added'));
Simon Huntf542d842015-02-11 16:20:33 -0800210 }
211
Steven Burrows1c2a9682017-07-14 16:52:46 +0100212 function removeIntent(d) {
Simon Huntcaed0412017-08-12 13:49:17 -0700213 var action = topoLion(d.intentPurge ? 'purged' : 'withdrawn');
214
Viswanath KSP0f297702016-08-13 18:02:43 +0530215 wss.sendEvent('removeIntent', {
216 appId: d.appId,
217 appName: d.appName,
Viswanath KSP813a20d2016-09-13 04:25:41 +0530218 key: d.key,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100219 purge: d.intentPurge,
Viswanath KSP0f297702016-08-13 18:02:43 +0530220 });
221 trafficMode = 'intents';
222 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700223 flash.flash(topoLion('intent') + ' ' + action);
Viswanath KSP0f297702016-08-13 18:02:43 +0530224 }
225
Steven Burrows1c2a9682017-07-14 16:52:46 +0100226 function resubmitIntent(d) {
Viswanath KSP14aee092016-10-02 01:47:40 +0530227 wss.sendEvent('resubmitIntent', {
228 appId: d.appId,
229 appName: d.appName,
230 key: d.key,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100231 purge: d.intentPurge,
Viswanath KSP14aee092016-10-02 01:47:40 +0530232 });
233 trafficMode = 'intents';
234 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700235 flash.flash(topoLion('intent') + ' ' + topoLion('resubmitted'));
Viswanath KSP14aee092016-10-02 01:47:40 +0530236 }
237
Steven Burrows1c2a9682017-07-14 16:52:46 +0100238 function addMultiSourceIntent() {
Simon Huntf542d842015-02-11 16:20:33 -0800239 var so = api.selectOrder();
Simon Huntcaed0412017-08-12 13:49:17 -0700240
Simon Hunt237676b52015-03-10 19:04:26 -0700241 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800242 src: so.slice(0, so.length - 1),
243 dst: so[so.length - 1],
Steven Burrows1c2a9682017-07-14 16:52:46 +0100244 ids: so,
Simon Huntf542d842015-02-11 16:20:33 -0800245 });
Simon Huntd2862c32015-08-24 17:41:51 -0700246 trafficMode = 'intents';
247 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700248 flash.flash(topoLion('tr_fl_multisrc_flow') + ' ' + topoLion('added'));
Simon Huntf542d842015-02-11 16:20:33 -0800249 }
250
Steven Burrows1c2a9682017-07-14 16:52:46 +0100251 function removeIntents() {
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530252 wss.sendEvent('removeIntents', {});
253 trafficMode = 'intents';
254 hoverMode = null;
Simon Huntcaed0412017-08-12 13:49:17 -0700255 flash.flash(topoLion('intents') + ' ' + topoLion('purged'));
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530256 }
257
Simon Huntf542d842015-02-11 16:20:33 -0800258
Simon Huntf542d842015-02-11 16:20:33 -0800259 // === -----------------------------------------------------
260 // === MODULE DEFINITION ===
261
262 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800263 .factory('TopoTrafficService',
Simon Huntcaed0412017-08-12 13:49:17 -0700264 ['$log', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800265
Steven Burrows1c2a9682017-07-14 16:52:46 +0100266 function (_$log_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800267 $log = _$log_;
Simon Huntf542d842015-02-11 16:20:33 -0800268 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700269 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800270
Simon Huntf542d842015-02-11 16:20:33 -0800271 return {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700272 initTraffic: function (_api_) { api = _api_; },
273 destroyTraffic: function () { },
Simon Huntf542d842015-02-11 16:20:33 -0800274
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700275 // invoked from toolbar overlay buttons or keystrokes
Simon Huntf542d842015-02-11 16:20:33 -0800276 cancelTraffic: cancelTraffic,
Simon Hunt21281fd2017-03-30 22:28:28 -0700277 showAllTraffic: showAllTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700278 showDeviceLinkFlows: showDeviceLinkFlows,
279 showRelatedIntents: showRelatedIntents,
280 showPrevIntent: showPrevIntent,
281 showNextIntent: showNextIntent,
282 showSelectedIntentTraffic: showSelectedIntentTraffic,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700283 selectIntent: selectIntent,
Thomas Vachuska2b4de872021-03-30 16:31:34 -0700284 showCustomTraffic: showCustomTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700285
286 // invoked from mouseover/mouseout and selection change
Simon Huntf542d842015-02-11 16:20:33 -0800287 requestTrafficForMode: requestTrafficForMode,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700288
289 // invoked from buttons on detail (multi-select) panel
290 addHostIntent: addHostIntent,
Viswanath KSP0f297702016-08-13 18:02:43 +0530291 addMultiSourceIntent: addMultiSourceIntent,
Viswanath KSP14aee092016-10-02 01:47:40 +0530292 removeIntent: removeIntent,
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530293 resubmitIntent: resubmitIntent,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100294 removeIntents: removeIntents,
Simon Huntcaed0412017-08-12 13:49:17 -0700295
296 setLionBundle: setLionBundle,
Simon Huntf542d842015-02-11 16:20:33 -0800297 };
298 }]);
299}());