blob: 1ce88109fe124dc1733796baab687d52890287e9 [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');
Simon Huntf542d842015-02-11 16:20:33 -0800135 flash.flash('>');
136 }
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');
Simon Huntf542d842015-02-11 16:20:33 -0800142 flash.flash('<');
143 }
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)
153 function showAllTrafficAction() {
154 hoverMode = hoverModeAll;
Simon Hunt237676b52015-03-10 19:04:26 -0700155 wss.sendEvent('requestAllTraffic');
Simon Huntf542d842015-02-11 16:20:33 -0800156 flash.flash('All Traffic');
157 }
158
159 // === -----------------------------
160 // action buttons on detail panel
161
162 // also, keystroke-V (see topo.js)
163 function showRelatedIntentsAction () {
164 hoverMode = hoverModeIntents;
165 requestRelatedIntents();
166 flash.flash('Related Paths');
167 }
168
169 function addHostIntentAction () {
170 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 });
176 flash.flash('Host-to-Host flow added');
177 }
178
179 function addMultiSourceIntentAction () {
180 var so = api.selectOrder();
Simon Hunt237676b52015-03-10 19:04:26 -0700181 wss.sendEvent('addMultiSourceIntent', {
Simon Huntf542d842015-02-11 16:20:33 -0800182 src: so.slice(0, so.length - 1),
183 dst: so[so.length - 1],
184 ids: so
185 });
186 flash.flash('Multi-Source flow added');
187 }
188
189 // also, keystroke-F (see topo.js)
190 function showDeviceLinkFlowsAction () {
191 hoverMode = hoverModeFlows;
192 requestDeviceLinkFlows();
193 flash.flash('Device Flows');
194 }
195
196
197 // === -----------------------------------------------------
198 // === MODULE DEFINITION ===
199
200 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800201 .factory('TopoTrafficService',
Simon Hunt237676b52015-03-10 19:04:26 -0700202 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Huntf542d842015-02-11 16:20:33 -0800203
Simon Hunt237676b52015-03-10 19:04:26 -0700204 function (_$log_, _fs_, _flash_, _wss_) {
Simon Huntf542d842015-02-11 16:20:33 -0800205 $log = _$log_;
206 fs = _fs_;
207 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700208 wss = _wss_;
Simon Huntf542d842015-02-11 16:20:33 -0800209
210 function initTraffic(_api_) {
211 api = _api_;
212 }
213
214 function destroyTraffic() { }
215
216 return {
217 initTraffic: initTraffic,
218 destroyTraffic: destroyTraffic,
219
220 showTraffic: showTraffic,
221
222 cancelTraffic: cancelTraffic,
223 requestTrafficForMode: requestTrafficForMode,
224 showRelatedIntentsAction: showRelatedIntentsAction,
225 addHostIntentAction: addHostIntentAction,
226 addMultiSourceIntentAction: addMultiSourceIntentAction,
227 showDeviceLinkFlowsAction: showDeviceLinkFlowsAction,
228 showNextIntentAction: showNextIntentAction,
229 showPrevIntentAction: showPrevIntentAction,
230 showSelectedIntentTrafficAction: showSelectedIntentTrafficAction,
231 showAllTrafficAction: showAllTrafficAction
232 };
233 }]);
234}());