blob: 1836e1e26ee12fdf819a489be27496328fcff109 [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 Huntf542d842015-02-11 16:20:33 -080035 // internal state
Simon Hunt8d22c4b2015-08-06 16:24:43 -070036 var trafficMode = null,
37 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -080038
39
40 // === -----------------------------------------------------
Simon Huntf542d842015-02-11 16:20:33 -080041 // Helper functions
42
Simon Hunt8d22c4b2015-08-06 16:24:43 -070043 // invoked in response to change in selection and/or mouseover/out:
Simon Huntd2862c32015-08-24 17:41:51 -070044 function requestTrafficForMode(mouse) {
Simon Hunta17fa672015-08-19 18:42:22 -070045 if (trafficMode === 'flows') {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070046 requestDeviceLinkFlows();
Simon Hunta17fa672015-08-19 18:42:22 -070047 } else if (trafficMode === 'intents') {
Simon Huntd2862c32015-08-24 17:41:51 -070048 if (!mouse || hoverMode === 'intents') {
49 requestRelatedIntents();
50 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -070051 } else {
Simon Huntd2862c32015-08-24 17:41:51 -070052 // do nothing
Simon Hunt8d22c4b2015-08-06 16:24:43 -070053 }
54 }
55
Simon Huntf542d842015-02-11 16:20:33 -080056 function requestDeviceLinkFlows() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070057 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080058 var hov = api.hovered();
59
60 function hoverValid() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070061 return hoverMode === 'flows' &&
Simon Huntf542d842015-02-11 16:20:33 -080062 hov && (hov.class === 'device');
63 }
64
Simon Hunt8d22c4b2015-08-06 16:24:43 -070065 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070066 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -080067 ids: api.selectOrder(),
68 hover: hoverValid() ? hov.id : ''
69 });
70 }
71 }
72
73 function requestRelatedIntents() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070074 // generates payload based on current hover-state
Simon Huntf542d842015-02-11 16:20:33 -080075 var hov = api.hovered();
76
77 function hoverValid() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070078 return hoverMode === 'intents' &&
Simon Huntf542d842015-02-11 16:20:33 -080079 hov && (hov.class === 'host' || hov.class === 'device');
80 }
81
Simon Hunt8d22c4b2015-08-06 16:24:43 -070082 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070083 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -080084 ids: api.selectOrder(),
85 hover: hoverValid() ? hov.id : ''
86 });
87 }
88 }
89
90
Simon Hunt8d22c4b2015-08-06 16:24:43 -070091 // === -------------------------------------------------------------
92 // Traffic requests invoked from keystrokes or toolbar buttons...
Simon Huntf542d842015-02-11 16:20:33 -080093
Simon Huntd2862c32015-08-24 17:41:51 -070094 function cancelTraffic(forced) {
95 if (!trafficMode || (!forced && trafficMode === 'allFlowPort')) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070096 return false;
Simon Huntf542d842015-02-11 16:20:33 -080097 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -070098
99 trafficMode = hoverMode = null;
100 wss.sendEvent('cancelTraffic');
101 flash.flash('Traffic monitoring canceled');
102 return true;
Simon Huntf542d842015-02-11 16:20:33 -0800103 }
104
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700105 function showAllFlowTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700106 trafficMode = 'allFlowPort';
107 hoverMode = null;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700108 wss.sendEvent('requestAllFlowTraffic');
109 flash.flash('All Flow Traffic');
110 }
111
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700112 function showAllPortTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700113 trafficMode = 'allFlowPort';
114 hoverMode = null;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700115 wss.sendEvent('requestAllPortTraffic');
116 flash.flash('All Port Traffic');
Simon Huntf542d842015-02-11 16:20:33 -0800117 }
118
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700119 function showDeviceLinkFlows () {
120 trafficMode = hoverMode = 'flows';
121 requestDeviceLinkFlows();
122 flash.flash('Device Flows');
123 }
Simon Huntf542d842015-02-11 16:20:33 -0800124
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700125 function showRelatedIntents () {
126 trafficMode = hoverMode = 'intents';
Simon Huntf542d842015-02-11 16:20:33 -0800127 requestRelatedIntents();
128 flash.flash('Related Paths');
129 }
130
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700131 function showPrevIntent() {
132 if (trafficMode === 'intents') {
133 hoverMode = null;
134 wss.sendEvent('requestPrevRelatedIntent');
135 flash.flash('Previous related intent');
136 }
137 }
138
139 function showNextIntent() {
140 if (trafficMode === 'intents') {
141 hoverMode = null;
142 wss.sendEvent('requestNextRelatedIntent');
143 flash.flash('Next related intent');
144 }
145 }
146
147 function showSelectedIntentTraffic() {
148 if (trafficMode === 'intents') {
149 hoverMode = null;
150 wss.sendEvent('requestSelectedIntentTraffic');
151 flash.flash('Traffic on Selected Path');
152 }
153 }
154
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700155 // force the system to create a single intent selection
156 function selectIntent(data) {
157 trafficMode = 'intents';
158 hoverMode = null;
159 wss.sendEvent('selectIntent', data);
160 flash.flash('Selecting Intent ' + data.key);
161 }
162
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700163
164 // === ------------------------------------------------------
165 // action buttons on detail panel (multiple selection)
166
167 function addHostIntent () {
Simon Huntf542d842015-02-11 16:20:33 -0800168 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700169 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800170 one: so[0],
171 two: so[1],
172 ids: so
173 });
Simon Huntd2862c32015-08-24 17:41:51 -0700174 trafficMode = 'intents';
175 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800176 flash.flash('Host-to-Host flow added');
177 }
178
Viswanath KSP0f297702016-08-13 18:02:43 +0530179 function removeIntent (d) {
180 $log.debug('Entering removeIntent');
181 wss.sendEvent('removeIntent', {
182 appId: d.appId,
183 appName: d.appName,
Viswanath KSP813a20d2016-09-13 04:25:41 +0530184 key: d.key,
185 purge: d.intentPurge
Viswanath KSP0f297702016-08-13 18:02:43 +0530186 });
187 trafficMode = 'intents';
188 hoverMode = null;
Viswanath KSP813a20d2016-09-13 04:25:41 +0530189 var txt = d.intentPurge ? 'purged' : 'withdrawn';
190 flash.flash('Intent ' + txt);
Viswanath KSP0f297702016-08-13 18:02:43 +0530191 }
192
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700193 function addMultiSourceIntent () {
Simon Huntf542d842015-02-11 16:20:33 -0800194 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700195 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800196 src: so.slice(0, so.length - 1),
197 dst: so[so.length - 1],
198 ids: so
199 });
Simon Huntd2862c32015-08-24 17:41:51 -0700200 trafficMode = 'intents';
201 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800202 flash.flash('Multi-Source flow added');
203 }
204
Simon Huntf542d842015-02-11 16:20:33 -0800205
Simon Huntf542d842015-02-11 16:20:33 -0800206 // === -----------------------------------------------------
207 // === MODULE DEFINITION ===
208
209 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800210 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700211 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800212
Simon Hunt237676b52015-03-10 19:04:26 -0700213 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800214 $log = _$log_;
215 fs = _fs_;
216 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700217 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800218
Simon Huntf542d842015-02-11 16:20:33 -0800219 return {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700220 initTraffic: function (_api_) { api = _api_; },
221 destroyTraffic: function () { },
Simon Huntf542d842015-02-11 16:20:33 -0800222
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700223 // invoked from toolbar overlay buttons or keystrokes
Simon Huntf542d842015-02-11 16:20:33 -0800224 cancelTraffic: cancelTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700225 showAllFlowTraffic: showAllFlowTraffic,
226 showAllPortTraffic: showAllPortTraffic,
227 showDeviceLinkFlows: showDeviceLinkFlows,
228 showRelatedIntents: showRelatedIntents,
229 showPrevIntent: showPrevIntent,
230 showNextIntent: showNextIntent,
231 showSelectedIntentTraffic: showSelectedIntentTraffic,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700232 selectIntent: selectIntent,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700233
234 // invoked from mouseover/mouseout and selection change
Simon Huntf542d842015-02-11 16:20:33 -0800235 requestTrafficForMode: requestTrafficForMode,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700236
Simon Huntd3ceffa2015-08-25 12:44:35 -0700237 // TODO: these should move to new UI demo app
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700238 // invoked from buttons on detail (multi-select) panel
239 addHostIntent: addHostIntent,
Viswanath KSP0f297702016-08-13 18:02:43 +0530240 addMultiSourceIntent: addMultiSourceIntent,
241 removeIntent: removeIntent
Simon Huntf542d842015-02-11 16:20:33 -0800242 };
243 }]);
244}());