blob: 6bde992b03836e5c4cd26ed60bf82b29ad77185f [file] [log] [blame]
Simon Huntf542d842015-02-11 16:20:33 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 Burrows46c5f102017-07-14 16:52:46 +010026 var $log, flash, wss, api;
Simon Huntf542d842015-02-11 16:20:33 -080027
Simon Huntf542d842015-02-11 16:20:33 -080028 /*
Simon Hunt8d22c4b2015-08-06 16:24:43 -070029 API to topoForce
30 hovered()
31 somethingSelected()
32 selectOrder()
Simon Huntf542d842015-02-11 16:20:33 -080033 */
34
Simon Hunt21281fd2017-03-30 22:28:28 -070035 var allTrafficTypes = [
36 'flowStatsBytes',
37 'portStatsBitSec',
Steven Burrows46c5f102017-07-14 16:52:46 +010038 'portStatsPktSec',
Simon Hunt21281fd2017-03-30 22:28:28 -070039 ],
40 allTrafficMsgs = [
41 'Flow Stats (bytes)',
42 'Port Stats (bits / second)',
Steven Burrows46c5f102017-07-14 16:52:46 +010043 'Port Stats (packets / second)',
Simon Hunt21281fd2017-03-30 22:28:28 -070044 ];
45
Simon Huntf542d842015-02-11 16:20:33 -080046 // internal state
Simon Hunt8d22c4b2015-08-06 16:24:43 -070047 var trafficMode = null,
Simon Hunt21281fd2017-03-30 22:28:28 -070048 hoverMode = null,
49 allTrafficIndex = 0;
50
Simon Huntf542d842015-02-11 16:20:33 -080051
Simon Huntf542d842015-02-11 16:20:33 -080052 // === -----------------------------------------------------
Simon Huntf542d842015-02-11 16:20:33 -080053 // Helper functions
54
Simon Hunt8d22c4b2015-08-06 16:24:43 -070055 // invoked in response to change in selection and/or mouseover/out:
Simon Huntd2862c32015-08-24 17:41:51 -070056 function requestTrafficForMode(mouse) {
Simon Hunta17fa672015-08-19 18:42:22 -070057 if (trafficMode === 'flows') {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070058 requestDeviceLinkFlows();
Simon Hunta17fa672015-08-19 18:42:22 -070059 } else if (trafficMode === 'intents') {
Simon Huntd2862c32015-08-24 17:41:51 -070060 if (!mouse || hoverMode === 'intents') {
61 requestRelatedIntents();
62 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -070063 } else {
Simon Huntd2862c32015-08-24 17:41:51 -070064 // do nothing
Simon Hunt8d22c4b2015-08-06 16:24:43 -070065 }
66 }
67
Simon Huntf542d842015-02-11 16:20:33 -080068 function requestDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070069 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080070 var hov = api.hovered();
71
72 function hoverValid() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070073 return hoverMode === 'flows' &&
Simon Huntf542d842015-02-11 16:20:33 -080074 hov && (hov.class === 'device');
75 }
76
Simon Hunt8d22c4b2015-08-06 16:24:43 -070077 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070078 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -080079 ids: api.selectOrder(),
Steven Burrows46c5f102017-07-14 16:52:46 +010080 hover: hoverValid() ? hov.id : '',
Simon Huntf542d842015-02-11 16:20:33 -080081 });
82 }
83 }
84
85 function requestRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070086 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080087 var hov = api.hovered();
88
89 function hoverValid() {
Prince Pereira46c82d42016-09-19 13:30:50 +053090 return hoverMode === 'intents' && hov && (
91 hov.class === 'host' ||
92 hov.class === 'device' ||
93 hov.class === 'link');
Simon Huntf542d842015-02-11 16:20:33 -080094 }
95
Simon Hunt8d22c4b2015-08-06 16:24:43 -070096 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070097 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -080098 ids: api.selectOrder(),
Steven Burrows46c5f102017-07-14 16:52:46 +010099 hover: hoverValid() ? hov.id : '',
Simon Huntf542d842015-02-11 16:20:33 -0800100 });
101 }
102 }
103
104
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700105 // === -------------------------------------------------------------
106 // Traffic requests invoked from keystrokes or toolbar buttons...
Simon Huntf542d842015-02-11 16:20:33 -0800107
Simon Huntd2862c32015-08-24 17:41:51 -0700108 function cancelTraffic(forced) {
109 if (!trafficMode || (!forced && trafficMode === 'allFlowPort')) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700110 return false;
Simon Huntf542d842015-02-11 16:20:33 -0800111 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700112
113 trafficMode = hoverMode = null;
114 wss.sendEvent('cancelTraffic');
115 flash.flash('Traffic monitoring canceled');
116 return true;
Simon Huntf542d842015-02-11 16:20:33 -0800117 }
118
Simon Hunt21281fd2017-03-30 22:28:28 -0700119 function showAllTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700120 trafficMode = 'allFlowPort';
121 hoverMode = null;
Simon Hunt21281fd2017-03-30 22:28:28 -0700122 wss.sendEvent('requestAllTraffic', {
Steven Burrows46c5f102017-07-14 16:52:46 +0100123 trafficType: allTrafficTypes[allTrafficIndex],
Simon Hunt21281fd2017-03-30 22:28:28 -0700124 });
125 flash.flash(allTrafficMsgs[allTrafficIndex]);
126 allTrafficIndex = (allTrafficIndex + 1) % 3;
Simon Huntf542d842015-02-11 16:20:33 -0800127 }
128
Steven Burrows46c5f102017-07-14 16:52:46 +0100129 function showDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700130 trafficMode = hoverMode = 'flows';
131 requestDeviceLinkFlows();
132 flash.flash('Device Flows');
133 }
Simon Huntf542d842015-02-11 16:20:33 -0800134
Steven Burrows46c5f102017-07-14 16:52:46 +0100135 function showRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700136 trafficMode = hoverMode = 'intents';
Simon Huntf542d842015-02-11 16:20:33 -0800137 requestRelatedIntents();
138 flash.flash('Related Paths');
139 }
140
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700141 function showPrevIntent() {
142 if (trafficMode === 'intents') {
143 hoverMode = null;
144 wss.sendEvent('requestPrevRelatedIntent');
145 flash.flash('Previous related intent');
146 }
147 }
148
149 function showNextIntent() {
150 if (trafficMode === 'intents') {
151 hoverMode = null;
152 wss.sendEvent('requestNextRelatedIntent');
153 flash.flash('Next related intent');
154 }
155 }
156
157 function showSelectedIntentTraffic() {
158 if (trafficMode === 'intents') {
159 hoverMode = null;
160 wss.sendEvent('requestSelectedIntentTraffic');
161 flash.flash('Traffic on Selected Path');
162 }
163 }
164
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700165 // force the system to create a single intent selection
166 function selectIntent(data) {
167 trafficMode = 'intents';
168 hoverMode = null;
169 wss.sendEvent('selectIntent', data);
170 flash.flash('Selecting Intent ' + data.key);
171 }
172
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700173
174 // === ------------------------------------------------------
175 // action buttons on detail panel (multiple selection)
176
Steven Burrows46c5f102017-07-14 16:52:46 +0100177 function addHostIntent() {
Simon Huntf542d842015-02-11 16:20:33 -0800178 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700179 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800180 one: so[0],
181 two: so[1],
Steven Burrows46c5f102017-07-14 16:52:46 +0100182 ids: so,
Simon Huntf542d842015-02-11 16:20:33 -0800183 });
Simon Huntd2862c32015-08-24 17:41:51 -0700184 trafficMode = 'intents';
185 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800186 flash.flash('Host-to-Host flow added');
187 }
188
Steven Burrows46c5f102017-07-14 16:52:46 +0100189 function removeIntent(d) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530190 $log.debug('Entering removeIntent');
191 wss.sendEvent('removeIntent', {
192 appId: d.appId,
193 appName: d.appName,
Viswanath KSP813a20d2016-09-13 04:25:41 +0530194 key: d.key,
Steven Burrows46c5f102017-07-14 16:52:46 +0100195 purge: d.intentPurge,
Viswanath KSP0f297702016-08-13 18:02:43 +0530196 });
197 trafficMode = 'intents';
198 hoverMode = null;
Viswanath KSP813a20d2016-09-13 04:25:41 +0530199 var txt = d.intentPurge ? 'purged' : 'withdrawn';
200 flash.flash('Intent ' + txt);
Viswanath KSP0f297702016-08-13 18:02:43 +0530201 }
202
Steven Burrows46c5f102017-07-14 16:52:46 +0100203 function resubmitIntent(d) {
Viswanath KSP14aee092016-10-02 01:47:40 +0530204 $log.debug('Entering resubmitIntent');
205 wss.sendEvent('resubmitIntent', {
206 appId: d.appId,
207 appName: d.appName,
208 key: d.key,
Steven Burrows46c5f102017-07-14 16:52:46 +0100209 purge: d.intentPurge,
Viswanath KSP14aee092016-10-02 01:47:40 +0530210 });
211 trafficMode = 'intents';
212 hoverMode = null;
213 flash.flash('Intent resubmitted');
214 }
215
Steven Burrows46c5f102017-07-14 16:52:46 +0100216 function addMultiSourceIntent() {
Simon Huntf542d842015-02-11 16:20:33 -0800217 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700218 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800219 src: so.slice(0, so.length - 1),
220 dst: so[so.length - 1],
Steven Burrows46c5f102017-07-14 16:52:46 +0100221 ids: so,
Simon Huntf542d842015-02-11 16:20:33 -0800222 });
Simon Huntd2862c32015-08-24 17:41:51 -0700223 trafficMode = 'intents';
224 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800225 flash.flash('Multi-Source flow added');
226 }
227
Steven Burrows46c5f102017-07-14 16:52:46 +0100228 function removeIntents() {
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530229 $log.debug('Entering removeIntents');
230 wss.sendEvent('removeIntents', {});
231 trafficMode = 'intents';
232 hoverMode = null;
233 flash.flash('Intent are purged');
234 }
235
Simon Huntf542d842015-02-11 16:20:33 -0800236
Simon Huntf542d842015-02-11 16:20:33 -0800237 // === -----------------------------------------------------
238 // === MODULE DEFINITION ===
239
240 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800241 .factory('TopoTrafficService',
Steven Burrows46c5f102017-07-14 16:52:46 +0100242 ['$log', 'FnService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800243
Steven Burrows46c5f102017-07-14 16:52:46 +0100244 function (_$log_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800245 $log = _$log_;
Simon Huntf542d842015-02-11 16:20:33 -0800246 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700247 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800248
Simon Huntf542d842015-02-11 16:20:33 -0800249 return {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700250 initTraffic: function (_api_) { api = _api_; },
251 destroyTraffic: function () { },
Simon Huntf542d842015-02-11 16:20:33 -0800252
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700253 // invoked from toolbar overlay buttons or keystrokes
Simon Huntf542d842015-02-11 16:20:33 -0800254 cancelTraffic: cancelTraffic,
Simon Hunt21281fd2017-03-30 22:28:28 -0700255 showAllTraffic: showAllTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700256 showDeviceLinkFlows: showDeviceLinkFlows,
257 showRelatedIntents: showRelatedIntents,
258 showPrevIntent: showPrevIntent,
259 showNextIntent: showNextIntent,
260 showSelectedIntentTraffic: showSelectedIntentTraffic,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700261 selectIntent: selectIntent,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700262
263 // invoked from mouseover/mouseout and selection change
Simon Huntf542d842015-02-11 16:20:33 -0800264 requestTrafficForMode: requestTrafficForMode,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700265
Simon Huntd3ceffa2015-08-25 12:44:35 -0700266 // TODO: these should move to new UI demo app
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700267 // invoked from buttons on detail (multi-select) panel
268 addHostIntent: addHostIntent,
Viswanath KSP0f297702016-08-13 18:02:43 +0530269 addMultiSourceIntent: addMultiSourceIntent,
Viswanath KSP14aee092016-10-02 01:47:40 +0530270 removeIntent: removeIntent,
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530271 resubmitIntent: resubmitIntent,
Steven Burrows46c5f102017-07-14 16:52:46 +0100272 removeIntents: removeIntents,
Simon Huntf542d842015-02-11 16:20:33 -0800273 };
274 }]);
275}());