blob: 0ed005ea3e3181fe6cf10d5d3523d8bf372b7957 [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,
62 i, ldata;
63
64 for (i=0; i<n; i++) {
65 ldata = api.findLinkById(p.links[i]);
66 if (ldata && ldata.el) {
67 ldata.el.classed(p.class, true);
68 ldata.label = p.labels[i];
69 }
70 }
71 });
72
73 api.updateLinks();
74 }
75
76 // === -----------------------------------------------------
77 // Helper functions
78
79 function requestDeviceLinkFlows() {
80 var hov = api.hovered();
81
82 function hoverValid() {
83 return hoverMode === hoverModeFlows &&
84 hov && (hov.class === 'device');
85 }
86
87 if (api.validateSelectionContext()) {
Simon Hunt237676b52015-03-10 19:04:26 -070088 wss.sendEvent('requestDeviceLinkFlows', {
Simon Huntf542d842015-02-11 16:20:33 -080089 ids: api.selectOrder(),
90 hover: hoverValid() ? hov.id : ''
91 });
92 }
93 }
94
95 function requestRelatedIntents() {
96 var hov = api.hovered();
97
98 function hoverValid() {
99 return hoverMode === hoverModeIntents &&
100 hov && (hov.class === 'host' || hov.class === 'device');
101 }
102
103 if (api.validateSelectionContext()) {
Simon Hunt237676b52015-03-10 19:04:26 -0700104 wss.sendEvent('requestRelatedIntents', {
Simon Huntf542d842015-02-11 16:20:33 -0800105 ids: api.selectOrder(),
106 hover: hoverValid() ? hov.id : ''
107 });
108 }
109 }
110
111
112 // === -----------------------------------------------------
113 // Traffic requests
114
115 function cancelTraffic() {
Simon Hunt237676b52015-03-10 19:04:26 -0700116 wss.sendEvent('cancelTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800117 }
118
119 // invoked in response to change in selection and/or mouseover/out:
120 function requestTrafficForMode() {
121 if (hoverMode === hoverModeFlows) {
122 requestDeviceLinkFlows();
123 } else if (hoverMode === hoverModeIntents) {
124 requestRelatedIntents();
125 }
126 }
127
128 // === -----------------------------
129 // keystroke commands
130
131 // keystroke-right-arrow (see topo.js)
132 function showNextIntentAction() {
133 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700134 wss.sendEvent('requestNextRelatedIntent');
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700135 flash.flash('Next related intent');
Simon Huntf542d842015-02-11 16:20:33 -0800136 }
137
138 // keystroke-left-arrow (see topo.js)
139 function showPrevIntentAction() {
140 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700141 wss.sendEvent('requestPrevRelatedIntent');
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700142 flash.flash('Previous related intent');
Simon Huntf542d842015-02-11 16:20:33 -0800143 }
144
145 // keystroke-W (see topo.js)
146 function showSelectedIntentTrafficAction() {
147 hoverMode = hoverModeNone;
Simon Hunt237676b52015-03-10 19:04:26 -0700148 wss.sendEvent('requestSelectedIntentTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800149 flash.flash('Traffic on Selected Path');
150 }
151
152 // keystroke-A (see topo.js)
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700153 function showAllFlowTrafficAction() {
Simon Huntf542d842015-02-11 16:20:33 -0800154 hoverMode = hoverModeAll;
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700155 wss.sendEvent('requestAllFlowTraffic');
156 flash.flash('All Flow Traffic');
157 }
158
159 // keystroke-A (see topo.js)
160 function showAllPortTrafficAction() {
161 hoverMode = hoverModeAll;
162 wss.sendEvent('requestAllPortTraffic');
163 flash.flash('All Port Traffic');
Simon Huntf542d842015-02-11 16:20:33 -0800164 }
165
166 // === -----------------------------
167 // action buttons on detail panel
168
169 // also, keystroke-V (see topo.js)
170 function showRelatedIntentsAction () {
171 hoverMode = hoverModeIntents;
172 requestRelatedIntents();
173 flash.flash('Related Paths');
174 }
175
176 function addHostIntentAction () {
177 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700178 wss.sendEvent('addHostIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800179 one: so[0],
180 two: so[1],
181 ids: so
182 });
183 flash.flash('Host-to-Host flow added');
184 }
185
186 function addMultiSourceIntentAction () {
187 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700188 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800189 src: so.slice(0, so.length - 1),
190 dst: so[so.length - 1],
191 ids: so
192 });
193 flash.flash('Multi-Source flow added');
194 }
195
196 // also, keystroke-F (see topo.js)
197 function showDeviceLinkFlowsAction () {
198 hoverMode = hoverModeFlows;
199 requestDeviceLinkFlows();
200 flash.flash('Device Flows');
201 }
202
203
204 // === -----------------------------------------------------
205 // === MODULE DEFINITION ===
206
207 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800208 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700209 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800210
Simon Hunt237676b52015-03-10 19:04:26 -0700211 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800212 $log = _$log_;
213 fs = _fs_;
214 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700215 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800216
217 function initTraffic(_api_) {
218 api = _api_;
219 }
220
221 function destroyTraffic() { }
222
223 return {
224 initTraffic: initTraffic,
225 destroyTraffic: destroyTraffic,
226
227 showTraffic: showTraffic,
228
229 cancelTraffic: cancelTraffic,
230 requestTrafficForMode: requestTrafficForMode,
231 showRelatedIntentsAction: showRelatedIntentsAction,
232 addHostIntentAction: addHostIntentAction,
233 addMultiSourceIntentAction: addMultiSourceIntentAction,
234 showDeviceLinkFlowsAction: showDeviceLinkFlowsAction,
235 showNextIntentAction: showNextIntentAction,
236 showPrevIntentAction: showPrevIntentAction,
237 showSelectedIntentTrafficAction: showSelectedIntentTrafficAction,
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700238 showAllFlowTrafficAction: showAllFlowTrafficAction,
239 showAllPortTrafficAction: showAllPortTrafficAction
Simon Huntf542d842015-02-11 16:20:33 -0800240 };
241 }]);
242}());