blob: 11a4dba6ab425a2227c00b59a7253a5d3fe47bf7 [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
Simon Hunt8d22c4b2015-08-06 16:24:43 -070026 var $log, fs, 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',
38 'portStatsPktSec'
39 ],
40 allTrafficMsgs = [
41 'Flow Stats (bytes)',
42 'Port Stats (bits / second)',
43 'Port Stats (packets / second)'
44 ];
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
52
53 // === -----------------------------------------------------
Simon Huntf542d842015-02-11 16:20:33 -080054 // Helper functions
55
Simon Hunt8d22c4b2015-08-06 16:24:43 -070056 // invoked in response to change in selection and/or mouseover/out:
Simon Huntd2862c32015-08-24 17:41:51 -070057 function requestTrafficForMode(mouse) {
Simon Hunta17fa672015-08-19 18:42:22 -070058 if (trafficMode === 'flows') {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070059 requestDeviceLinkFlows();
Simon Hunta17fa672015-08-19 18:42:22 -070060 } else if (trafficMode === 'intents') {
Simon Huntd2862c32015-08-24 17:41:51 -070061 if (!mouse || hoverMode === 'intents') {
62 requestRelatedIntents();
63 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -070064 } else {
Simon Huntd2862c32015-08-24 17:41:51 -070065 // do nothing
Simon Hunt8d22c4b2015-08-06 16:24:43 -070066 }
67 }
68
Simon Huntf542d842015-02-11 16:20:33 -080069 function requestDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070070 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080071 var hov = api.hovered();
72
73 function hoverValid() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070074 return hoverMode === 'flows' &&
Simon Huntf542d842015-02-11 16:20:33 -080075 hov && (hov.class === 'device');
76 }
77
Simon Hunt8d22c4b2015-08-06 16:24:43 -070078 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070079 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -080080 ids: api.selectOrder(),
81 hover: hoverValid() ? hov.id : ''
82 });
83 }
84 }
85
86 function requestRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070087 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080088 var hov = api.hovered();
89
90 function hoverValid() {
Prince Pereira46c82d42016-09-19 13:30:50 +053091 return hoverMode === 'intents' && hov && (
92 hov.class === 'host' ||
93 hov.class === 'device' ||
94 hov.class === 'link');
Simon Huntf542d842015-02-11 16:20:33 -080095 }
96
Simon Hunt8d22c4b2015-08-06 16:24:43 -070097 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070098 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -080099 ids: api.selectOrder(),
100 hover: hoverValid() ? hov.id : ''
101 });
102 }
103 }
104
105
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700106 // === -------------------------------------------------------------
107 // Traffic requests invoked from keystrokes or toolbar buttons...
Simon Huntf542d842015-02-11 16:20:33 -0800108
Simon Huntd2862c32015-08-24 17:41:51 -0700109 function cancelTraffic(forced) {
110 if (!trafficMode || (!forced && trafficMode === 'allFlowPort')) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700111 return false;
Simon Huntf542d842015-02-11 16:20:33 -0800112 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700113
114 trafficMode = hoverMode = null;
115 wss.sendEvent('cancelTraffic');
116 flash.flash('Traffic monitoring canceled');
117 return true;
Simon Huntf542d842015-02-11 16:20:33 -0800118 }
119
Simon Hunt21281fd2017-03-30 22:28:28 -0700120 function showAllTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700121 trafficMode = 'allFlowPort';
122 hoverMode = null;
Simon Hunt21281fd2017-03-30 22:28:28 -0700123 wss.sendEvent('requestAllTraffic', {
124 trafficType: allTrafficTypes[allTrafficIndex]
125 });
126 flash.flash(allTrafficMsgs[allTrafficIndex]);
127 allTrafficIndex = (allTrafficIndex + 1) % 3;
Simon Huntf542d842015-02-11 16:20:33 -0800128 }
129
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700130 function showDeviceLinkFlows () {
131 trafficMode = hoverMode = 'flows';
132 requestDeviceLinkFlows();
133 flash.flash('Device Flows');
134 }
Simon Huntf542d842015-02-11 16:20:33 -0800135
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700136 function showRelatedIntents () {
137 trafficMode = hoverMode = 'intents';
Simon Huntf542d842015-02-11 16:20:33 -0800138 requestRelatedIntents();
139 flash.flash('Related Paths');
140 }
141
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700142 function showPrevIntent() {
143 if (trafficMode === 'intents') {
144 hoverMode = null;
145 wss.sendEvent('requestPrevRelatedIntent');
146 flash.flash('Previous related intent');
147 }
148 }
149
150 function showNextIntent() {
151 if (trafficMode === 'intents') {
152 hoverMode = null;
153 wss.sendEvent('requestNextRelatedIntent');
154 flash.flash('Next related intent');
155 }
156 }
157
158 function showSelectedIntentTraffic() {
159 if (trafficMode === 'intents') {
160 hoverMode = null;
161 wss.sendEvent('requestSelectedIntentTraffic');
162 flash.flash('Traffic on Selected Path');
163 }
164 }
165
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700166 // force the system to create a single intent selection
167 function selectIntent(data) {
168 trafficMode = 'intents';
169 hoverMode = null;
170 wss.sendEvent('selectIntent', data);
171 flash.flash('Selecting Intent ' + data.key);
172 }
173
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700174
175 // === ------------------------------------------------------
176 // action buttons on detail panel (multiple selection)
177
178 function addHostIntent () {
Simon Huntf542d842015-02-11 16:20:33 -0800179 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700180 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800181 one: so[0],
182 two: so[1],
183 ids: so
184 });
Simon Huntd2862c32015-08-24 17:41:51 -0700185 trafficMode = 'intents';
186 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800187 flash.flash('Host-to-Host flow added');
188 }
189
Viswanath KSP0f297702016-08-13 18:02:43 +0530190 function removeIntent (d) {
191 $log.debug('Entering removeIntent');
192 wss.sendEvent('removeIntent', {
193 appId: d.appId,
194 appName: d.appName,
Viswanath KSP813a20d2016-09-13 04:25:41 +0530195 key: d.key,
196 purge: d.intentPurge
Viswanath KSP0f297702016-08-13 18:02:43 +0530197 });
198 trafficMode = 'intents';
199 hoverMode = null;
Viswanath KSP813a20d2016-09-13 04:25:41 +0530200 var txt = d.intentPurge ? 'purged' : 'withdrawn';
201 flash.flash('Intent ' + txt);
Viswanath KSP0f297702016-08-13 18:02:43 +0530202 }
203
Viswanath KSP14aee092016-10-02 01:47:40 +0530204 function resubmitIntent (d) {
205 $log.debug('Entering resubmitIntent');
206 wss.sendEvent('resubmitIntent', {
207 appId: d.appId,
208 appName: d.appName,
209 key: d.key,
210 purge: d.intentPurge
211 });
212 trafficMode = 'intents';
213 hoverMode = null;
214 flash.flash('Intent resubmitted');
215 }
216
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700217 function addMultiSourceIntent () {
Simon Huntf542d842015-02-11 16:20:33 -0800218 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700219 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800220 src: so.slice(0, so.length - 1),
221 dst: so[so.length - 1],
222 ids: so
223 });
Simon Huntd2862c32015-08-24 17:41:51 -0700224 trafficMode = 'intents';
225 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800226 flash.flash('Multi-Source flow added');
227 }
228
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530229 function removeIntents () {
230 $log.debug('Entering removeIntents');
231 wss.sendEvent('removeIntents', {});
232 trafficMode = 'intents';
233 hoverMode = null;
234 flash.flash('Intent are purged');
235 }
236
Simon Huntf542d842015-02-11 16:20:33 -0800237
Simon Huntf542d842015-02-11 16:20:33 -0800238 // === -----------------------------------------------------
239 // === MODULE DEFINITION ===
240
241 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800242 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700243 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800244
Simon Hunt237676b52015-03-10 19:04:26 -0700245 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800246 $log = _$log_;
247 fs = _fs_;
248 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700249 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800250
Simon Huntf542d842015-02-11 16:20:33 -0800251 return {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700252 initTraffic: function (_api_) { api = _api_; },
253 destroyTraffic: function () { },
Simon Huntf542d842015-02-11 16:20:33 -0800254
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700255 // invoked from toolbar overlay buttons or keystrokes
Simon Huntf542d842015-02-11 16:20:33 -0800256 cancelTraffic: cancelTraffic,
Simon Hunt21281fd2017-03-30 22:28:28 -0700257 showAllTraffic: showAllTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700258 showDeviceLinkFlows: showDeviceLinkFlows,
259 showRelatedIntents: showRelatedIntents,
260 showPrevIntent: showPrevIntent,
261 showNextIntent: showNextIntent,
262 showSelectedIntentTraffic: showSelectedIntentTraffic,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700263 selectIntent: selectIntent,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700264
265 // invoked from mouseover/mouseout and selection change
Simon Huntf542d842015-02-11 16:20:33 -0800266 requestTrafficForMode: requestTrafficForMode,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700267
Simon Huntd3ceffa2015-08-25 12:44:35 -0700268 // TODO: these should move to new UI demo app
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700269 // invoked from buttons on detail (multi-select) panel
270 addHostIntent: addHostIntent,
Viswanath KSP0f297702016-08-13 18:02:43 +0530271 addMultiSourceIntent: addMultiSourceIntent,
Viswanath KSP14aee092016-10-02 01:47:40 +0530272 removeIntent: removeIntent,
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530273 resubmitIntent: resubmitIntent,
274 removeIntents: removeIntents
Simon Huntf542d842015-02-11 16:20:33 -0800275 };
276 }]);
277}());