blob: 7332ad0704c1ccc6b304486880c387d798fea377 [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
Srikanth Vavilapalli8c1ccca2015-06-08 19:23:24 -070072 if (fs.endsWith(lab, 'bps')) {
Simon Hunt96641372015-06-02 15:53:25 -070073 // inject additional styling for port-based traffic
74 units = lab.substring(lab.length-4);
75 portcls = 'port-traffic-' + units;
76
77 // for GBps
78 if (units.substring(0,1) === 'G') {
79 magnitude = fs.parseBitRate(lab);
80 if (magnitude >= 9) {
81 portcls += '-choked'
82 }
83 }
84 ldata.el.classed(portcls, true);
85 }
Simon Huntf542d842015-02-11 16:20:33 -080086 }
87 }
88 });
89
90 api.updateLinks();
91 }
92
93 // === -----------------------------------------------------
94 // Helper functions
95
96 function requestDeviceLinkFlows() {
97 var hov = api.hovered();
98
99 function hoverValid() {
100 return hoverMode === hoverModeFlows &&
101 hov && (hov.class === 'device');
102 }
103
104 if (api.validateSelectionContext()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700105 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -0800106 ids: api.selectOrder(),
107 hover: hoverValid() ? hov.id : ''
108 });
109 }
110 }
111
112 function requestRelatedIntents() {
113 var hov = api.hovered();
114
115 function hoverValid() {
116 return hoverMode === hoverModeIntents &&
117 hov && (hov.class === 'host' || hov.class === 'device');
118 }
119
120 if (api.validateSelectionContext()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700121 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -0800122 ids: api.selectOrder(),
123 hover: hoverValid() ? hov.id : ''
124 });
125 }
126 }
127
128
129 // === -----------------------------------------------------
130 // Traffic requests
131
132 function cancelTraffic() {
Simon Hunt237676b52015-03-10 19:04:26 -0700133 wss.sendEvent('cancelTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800134 }
135
136 // invoked in response to change in selection and/or mouseover/out:
137 function requestTrafficForMode() {
138 if (hoverMode === hoverModeFlows) {
139 requestDeviceLinkFlows();
140 } else if (hoverMode === hoverModeIntents) {
141 requestRelatedIntents();
142 }
143 }
144
145 // === -----------------------------
146 // keystroke commands
147
148 // keystroke-right-arrow (see topo.js)
149 function showNextIntentAction() {
150 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700151 wss.sendEvent('requestNextRelatedIntent');
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700152 flash.flash('Next related intent');
Simon Huntf542d842015-02-11 16:20:33 -0800153 }
154
155 // keystroke-left-arrow (see topo.js)
156 function showPrevIntentAction() {
157 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700158 wss.sendEvent('requestPrevRelatedIntent');
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700159 flash.flash('Previous related intent');
Simon Huntf542d842015-02-11 16:20:33 -0800160 }
161
162 // keystroke-W (see topo.js)
163 function showSelectedIntentTrafficAction() {
164 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700165 wss.sendEvent('requestSelectedIntentTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800166 flash.flash('Traffic on Selected Path');
167 }
168
169 // keystroke-A (see topo.js)
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700170 function showAllFlowTrafficAction() {
Simon Huntf542d842015-02-11 16:20:33 -0800171 hoverMode = hoverModeAll;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700172 wss.sendEvent('requestAllFlowTraffic');
173 flash.flash('All Flow Traffic');
174 }
175
176 // keystroke-A (see topo.js)
177 function showAllPortTrafficAction() {
178 hoverMode = hoverModeAll;
179 wss.sendEvent('requestAllPortTraffic');
180 flash.flash('All Port Traffic');
Simon Huntf542d842015-02-11 16:20:33 -0800181 }
182
183 // === -----------------------------
184 // action buttons on detail panel
185
186 // also, keystroke-V (see topo.js)
187 function showRelatedIntentsAction () {
188 hoverMode = hoverModeIntents;
189 requestRelatedIntents();
190 flash.flash('Related Paths');
191 }
192
193 function addHostIntentAction () {
194 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700195 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800196 one: so[0],
197 two: so[1],
198 ids: so
199 });
200 flash.flash('Host-to-Host flow added');
201 }
202
203 function addMultiSourceIntentAction () {
204 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700205 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800206 src: so.slice(0, so.length - 1),
207 dst: so[so.length - 1],
208 ids: so
209 });
210 flash.flash('Multi-Source flow added');
211 }
212
213 // also, keystroke-F (see topo.js)
214 function showDeviceLinkFlowsAction () {
215 hoverMode = hoverModeFlows;
216 requestDeviceLinkFlows();
217 flash.flash('Device Flows');
218 }
219
220
221 // === -----------------------------------------------------
222 // === MODULE DEFINITION ===
223
224 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800225 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700226 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800227
Simon Hunt237676b52015-03-10 19:04:26 -0700228 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800229 $log = _$log_;
230 fs = _fs_;
231 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700232 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800233
234 function initTraffic(_api_) {
235 api = _api_;
236 }
237
238 function destroyTraffic() { }
239
240 return {
241 initTraffic: initTraffic,
242 destroyTraffic: destroyTraffic,
243
244 showTraffic: showTraffic,
245
246 cancelTraffic: cancelTraffic,
247 requestTrafficForMode: requestTrafficForMode,
248 showRelatedIntentsAction: showRelatedIntentsAction,
249 addHostIntentAction: addHostIntentAction,
250 addMultiSourceIntentAction: addMultiSourceIntentAction,
251 showDeviceLinkFlowsAction: showDeviceLinkFlowsAction,
252 showNextIntentAction: showNextIntentAction,
253 showPrevIntentAction: showPrevIntentAction,
254 showSelectedIntentTrafficAction: showSelectedIntentTrafficAction,
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700255 showAllFlowTrafficAction: showAllFlowTrafficAction,
256 showAllPortTrafficAction: showAllPortTrafficAction
Simon Huntf542d842015-02-11 16:20:33 -0800257 };
258 }]);
259}());