blob: 215f3e35f78730d04ca352733f601438036774f4 [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() {
Prince Pereira46c82d42016-09-19 13:30:50 +053078 return hoverMode === 'intents' && hov && (
79 hov.class === 'host' ||
80 hov.class === 'device' ||
81 hov.class === 'link');
Simon Huntf542d842015-02-11 16:20:33 -080082 }
83
Simon Hunt8d22c4b2015-08-06 16:24:43 -070084 if (api.somethingSelected()) {
Simon Hunt237676b52015-03-10 19:04:26 -070085 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -080086 ids: api.selectOrder(),
87 hover: hoverValid() ? hov.id : ''
88 });
89 }
90 }
91
92
Simon Hunt8d22c4b2015-08-06 16:24:43 -070093 // === -------------------------------------------------------------
94 // Traffic requests invoked from keystrokes or toolbar buttons...
Simon Huntf542d842015-02-11 16:20:33 -080095
Simon Huntd2862c32015-08-24 17:41:51 -070096 function cancelTraffic(forced) {
97 if (!trafficMode || (!forced && trafficMode === 'allFlowPort')) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -070098 return false;
Simon Huntf542d842015-02-11 16:20:33 -080099 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700100
101 trafficMode = hoverMode = null;
102 wss.sendEvent('cancelTraffic');
103 flash.flash('Traffic monitoring canceled');
104 return true;
Simon Huntf542d842015-02-11 16:20:33 -0800105 }
106
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700107 function showAllFlowTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700108 trafficMode = 'allFlowPort';
109 hoverMode = null;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700110 wss.sendEvent('requestAllFlowTraffic');
111 flash.flash('All Flow Traffic');
112 }
113
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700114 function showAllPortTraffic() {
Simon Huntd2862c32015-08-24 17:41:51 -0700115 trafficMode = 'allFlowPort';
116 hoverMode = null;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700117 wss.sendEvent('requestAllPortTraffic');
118 flash.flash('All Port Traffic');
Simon Huntf542d842015-02-11 16:20:33 -0800119 }
120
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700121 function showDeviceLinkFlows () {
122 trafficMode = hoverMode = 'flows';
123 requestDeviceLinkFlows();
124 flash.flash('Device Flows');
125 }
Simon Huntf542d842015-02-11 16:20:33 -0800126
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700127 function showRelatedIntents () {
128 trafficMode = hoverMode = 'intents';
Simon Huntf542d842015-02-11 16:20:33 -0800129 requestRelatedIntents();
130 flash.flash('Related Paths');
131 }
132
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700133 function showPrevIntent() {
134 if (trafficMode === 'intents') {
135 hoverMode = null;
136 wss.sendEvent('requestPrevRelatedIntent');
137 flash.flash('Previous related intent');
138 }
139 }
140
141 function showNextIntent() {
142 if (trafficMode === 'intents') {
143 hoverMode = null;
144 wss.sendEvent('requestNextRelatedIntent');
145 flash.flash('Next related intent');
146 }
147 }
148
149 function showSelectedIntentTraffic() {
150 if (trafficMode === 'intents') {
151 hoverMode = null;
152 wss.sendEvent('requestSelectedIntentTraffic');
153 flash.flash('Traffic on Selected Path');
154 }
155 }
156
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700157 // force the system to create a single intent selection
158 function selectIntent(data) {
159 trafficMode = 'intents';
160 hoverMode = null;
161 wss.sendEvent('selectIntent', data);
162 flash.flash('Selecting Intent ' + data.key);
163 }
164
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700165
166 // === ------------------------------------------------------
167 // action buttons on detail panel (multiple selection)
168
169 function addHostIntent () {
Simon Huntf542d842015-02-11 16:20:33 -0800170 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700171 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800172 one: so[0],
173 two: so[1],
174 ids: so
175 });
Simon Huntd2862c32015-08-24 17:41:51 -0700176 trafficMode = 'intents';
177 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800178 flash.flash('Host-to-Host flow added');
179 }
180
Viswanath KSP0f297702016-08-13 18:02:43 +0530181 function removeIntent (d) {
182 $log.debug('Entering removeIntent');
183 wss.sendEvent('removeIntent', {
184 appId: d.appId,
185 appName: d.appName,
Viswanath KSP813a20d2016-09-13 04:25:41 +0530186 key: d.key,
187 purge: d.intentPurge
Viswanath KSP0f297702016-08-13 18:02:43 +0530188 });
189 trafficMode = 'intents';
190 hoverMode = null;
Viswanath KSP813a20d2016-09-13 04:25:41 +0530191 var txt = d.intentPurge ? 'purged' : 'withdrawn';
192 flash.flash('Intent ' + txt);
Viswanath KSP0f297702016-08-13 18:02:43 +0530193 }
194
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700195 function addMultiSourceIntent () {
Simon Huntf542d842015-02-11 16:20:33 -0800196 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700197 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800198 src: so.slice(0, so.length - 1),
199 dst: so[so.length - 1],
200 ids: so
201 });
Simon Huntd2862c32015-08-24 17:41:51 -0700202 trafficMode = 'intents';
203 hoverMode = null;
Simon Huntf542d842015-02-11 16:20:33 -0800204 flash.flash('Multi-Source flow added');
205 }
206
Simon Huntf542d842015-02-11 16:20:33 -0800207
Simon Huntf542d842015-02-11 16:20:33 -0800208 // === -----------------------------------------------------
209 // === MODULE DEFINITION ===
210
211 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800212 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700213 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800214
Simon Hunt237676b52015-03-10 19:04:26 -0700215 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800216 $log = _$log_;
217 fs = _fs_;
218 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700219 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800220
Simon Huntf542d842015-02-11 16:20:33 -0800221 return {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700222 initTraffic: function (_api_) { api = _api_; },
223 destroyTraffic: function () { },
Simon Huntf542d842015-02-11 16:20:33 -0800224
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700225 // invoked from toolbar overlay buttons or keystrokes
Simon Huntf542d842015-02-11 16:20:33 -0800226 cancelTraffic: cancelTraffic,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700227 showAllFlowTraffic: showAllFlowTraffic,
228 showAllPortTraffic: showAllPortTraffic,
229 showDeviceLinkFlows: showDeviceLinkFlows,
230 showRelatedIntents: showRelatedIntents,
231 showPrevIntent: showPrevIntent,
232 showNextIntent: showNextIntent,
233 showSelectedIntentTraffic: showSelectedIntentTraffic,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700234 selectIntent: selectIntent,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700235
236 // invoked from mouseover/mouseout and selection change
Simon Huntf542d842015-02-11 16:20:33 -0800237 requestTrafficForMode: requestTrafficForMode,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700238
Simon Huntd3ceffa2015-08-25 12:44:35 -0700239 // TODO: these should move to new UI demo app
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700240 // invoked from buttons on detail (multi-select) panel
241 addHostIntent: addHostIntent,
Viswanath KSP0f297702016-08-13 18:02:43 +0530242 addMultiSourceIntent: addMultiSourceIntent,
243 removeIntent: removeIntent
Simon Huntf542d842015-02-11 16:20:33 -0800244 };
245 }]);
246}());