blob: 80d0b4b73f84eb5598c50e2b1a67862a563c50c4 [file] [log] [blame]
Simon Huntf542d842015-02-11 16:20:33 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Hunt237676b52015-03-10 19:04:26 -070026 var $log, fs, flash, wss;
Simon Huntf542d842015-02-11 16:20:33 -080027
28 // api to topoForce
29 var api;
30 /*
31 clearLinkTrafficStyle()
32 removeLinkLabels()
33 updateLinks()
34 findLinkById( id )
35 hovered()
36 validateSelectionContext()
Simon Huntf542d842015-02-11 16:20:33 -080037 */
38
39 // constants
40 var hoverModeNone = 0,
41 hoverModeAll = 1,
42 hoverModeFlows = 2,
43 hoverModeIntents = 3;
44
45 // internal state
46 var hoverMode = hoverModeNone;
47
48
49 // === -----------------------------------------------------
50 // Event Handlers
51
52 function showTraffic(data) {
53 var paths = data.paths;
54
55 api.clearLinkTrafficStyle();
56 api.removeLinkLabels();
57
58 // Now highlight all links in the paths payload, and attach
59 // labels to them, if they are defined.
60 paths.forEach(function (p) {
61 var n = p.links.length,
Simon Hunt96641372015-06-02 15:53:25 -070062 i, ldata, lab, units, magnitude, portcls;
Simon Huntf542d842015-02-11 16:20:33 -080063
64 for (i=0; i<n; i++) {
65 ldata = api.findLinkById(p.links[i]);
Simon Hunt96641372015-06-02 15:53:25 -070066 lab = p.labels[i];
67
68 if (ldata && !ldata.el.empty()) {
Simon Huntf542d842015-02-11 16:20:33 -080069 ldata.el.classed(p.class, true);
Simon Hunt96641372015-06-02 15:53:25 -070070 ldata.label = lab;
71
72 // TODO: change this to 'bps' when we measure bits/sec
73 if (fs.endsWith(lab, 'Bps')) {
74 // inject additional styling for port-based traffic
75 units = lab.substring(lab.length-4);
76 portcls = 'port-traffic-' + units;
77
78 // for GBps
79 if (units.substring(0,1) === 'G') {
80 magnitude = fs.parseBitRate(lab);
81 if (magnitude >= 9) {
82 portcls += '-choked'
83 }
84 }
85 ldata.el.classed(portcls, true);
86 }
Simon Huntf542d842015-02-11 16:20:33 -080087 }
88 }
89 });
90
91 api.updateLinks();
92 }
93
94 // === -----------------------------------------------------
95 // Helper functions
96
97 function requestDeviceLinkFlows() {
98 var hov = api.hovered();
99
100 function hoverValid() {
101 return hoverMode === hoverModeFlows &&
102 hov && (hov.class === 'device');
103 }
104
105 if (api.validateSelectionContext()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700106 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -0800107 ids: api.selectOrder(),
108 hover: hoverValid() ? hov.id : ''
109 });
110 }
111 }
112
113 function requestRelatedIntents() {
114 var hov = api.hovered();
115
116 function hoverValid() {
117 return hoverMode === hoverModeIntents &&
118 hov && (hov.class === 'host' || hov.class === 'device');
119 }
120
121 if (api.validateSelectionContext()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700122 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -0800123 ids: api.selectOrder(),
124 hover: hoverValid() ? hov.id : ''
125 });
126 }
127 }
128
129
130 // === -----------------------------------------------------
131 // Traffic requests
132
133 function cancelTraffic() {
Simon Hunt237676b52015-03-10 19:04:26 -0700134 wss.sendEvent('cancelTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800135 }
136
137 // invoked in response to change in selection and/or mouseover/out:
138 function requestTrafficForMode() {
139 if (hoverMode === hoverModeFlows) {
140 requestDeviceLinkFlows();
141 } else if (hoverMode === hoverModeIntents) {
142 requestRelatedIntents();
143 }
144 }
145
146 // === -----------------------------
147 // keystroke commands
148
149 // keystroke-right-arrow (see topo.js)
150 function showNextIntentAction() {
151 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700152 wss.sendEvent('requestNextRelatedIntent');
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700153 flash.flash('Next related intent');
Simon Huntf542d842015-02-11 16:20:33 -0800154 }
155
156 // keystroke-left-arrow (see topo.js)
157 function showPrevIntentAction() {
158 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700159 wss.sendEvent('requestPrevRelatedIntent');
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700160 flash.flash('Previous related intent');
Simon Huntf542d842015-02-11 16:20:33 -0800161 }
162
163 // keystroke-W (see topo.js)
164 function showSelectedIntentTrafficAction() {
165 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700166 wss.sendEvent('requestSelectedIntentTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800167 flash.flash('Traffic on Selected Path');
168 }
169
170 // keystroke-A (see topo.js)
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700171 function showAllFlowTrafficAction() {
Simon Huntf542d842015-02-11 16:20:33 -0800172 hoverMode = hoverModeAll;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700173 wss.sendEvent('requestAllFlowTraffic');
174 flash.flash('All Flow Traffic');
175 }
176
177 // keystroke-A (see topo.js)
178 function showAllPortTrafficAction() {
179 hoverMode = hoverModeAll;
180 wss.sendEvent('requestAllPortTraffic');
181 flash.flash('All Port Traffic');
Simon Huntf542d842015-02-11 16:20:33 -0800182 }
183
184 // === -----------------------------
185 // action buttons on detail panel
186
187 // also, keystroke-V (see topo.js)
188 function showRelatedIntentsAction () {
189 hoverMode = hoverModeIntents;
190 requestRelatedIntents();
191 flash.flash('Related Paths');
192 }
193
194 function addHostIntentAction () {
195 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700196 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800197 one: so[0],
198 two: so[1],
199 ids: so
200 });
201 flash.flash('Host-to-Host flow added');
202 }
203
204 function addMultiSourceIntentAction () {
205 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700206 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800207 src: so.slice(0, so.length - 1),
208 dst: so[so.length - 1],
209 ids: so
210 });
211 flash.flash('Multi-Source flow added');
212 }
213
214 // also, keystroke-F (see topo.js)
215 function showDeviceLinkFlowsAction () {
216 hoverMode = hoverModeFlows;
217 requestDeviceLinkFlows();
218 flash.flash('Device Flows');
219 }
220
221
222 // === -----------------------------------------------------
223 // === MODULE DEFINITION ===
224
225 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800226 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700227 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800228
Simon Hunt237676b52015-03-10 19:04:26 -0700229 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800230 $log = _$log_;
231 fs = _fs_;
232 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700233 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800234
235 function initTraffic(_api_) {
236 api = _api_;
237 }
238
239 function destroyTraffic() { }
240
241 return {
242 initTraffic: initTraffic,
243 destroyTraffic: destroyTraffic,
244
245 showTraffic: showTraffic,
246
247 cancelTraffic: cancelTraffic,
248 requestTrafficForMode: requestTrafficForMode,
249 showRelatedIntentsAction: showRelatedIntentsAction,
250 addHostIntentAction: addHostIntentAction,
251 addMultiSourceIntentAction: addMultiSourceIntentAction,
252 showDeviceLinkFlowsAction: showDeviceLinkFlowsAction,
253 showNextIntentAction: showNextIntentAction,
254 showPrevIntentAction: showPrevIntentAction,
255 showSelectedIntentTrafficAction: showSelectedIntentTrafficAction,
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700256 showAllFlowTrafficAction: showAllFlowTrafficAction,
257 showAllPortTrafficAction: showAllPortTrafficAction
Simon Huntf542d842015-02-11 16:20:33 -0800258 };
259 }]);
260}());