blob: 30adc2376cdc2ce9866b46d0fb6fff5c192fd1f6 [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
26 var $log, fs, flash;
27
28 // api to topoForce
29 var api;
30 /*
31 clearLinkTrafficStyle()
32 removeLinkLabels()
33 updateLinks()
34 findLinkById( id )
35 hovered()
36 validateSelectionContext()
37 sendEvent( type, {payload} )
38 */
39
40 // constants
41 var hoverModeNone = 0,
42 hoverModeAll = 1,
43 hoverModeFlows = 2,
44 hoverModeIntents = 3;
45
46 // internal state
47 var hoverMode = hoverModeNone;
48
49
50 // === -----------------------------------------------------
51 // Event Handlers
52
53 function showTraffic(data) {
54 var paths = data.paths;
55
56 api.clearLinkTrafficStyle();
57 api.removeLinkLabels();
58
59 // Now highlight all links in the paths payload, and attach
60 // labels to them, if they are defined.
61 paths.forEach(function (p) {
62 var n = p.links.length,
63 i, ldata;
64
65 for (i=0; i<n; i++) {
66 ldata = api.findLinkById(p.links[i]);
67 if (ldata && ldata.el) {
68 ldata.el.classed(p.class, true);
69 ldata.label = p.labels[i];
70 }
71 }
72 });
73
74 api.updateLinks();
75 }
76
77 // === -----------------------------------------------------
78 // Helper functions
79
80 function requestDeviceLinkFlows() {
81 var hov = api.hovered();
82
83 function hoverValid() {
84 return hoverMode === hoverModeFlows &&
85 hov && (hov.class === 'device');
86 }
87
88 if (api.validateSelectionContext()) {
89 api.sendEvent('requestDeviceLinkFlows', {
90 ids: api.selectOrder(),
91 hover: hoverValid() ? hov.id : ''
92 });
93 }
94 }
95
96 function requestRelatedIntents() {
97 var hov = api.hovered();
98
99 function hoverValid() {
100 return hoverMode === hoverModeIntents &&
101 hov && (hov.class === 'host' || hov.class === 'device');
102 }
103
104 if (api.validateSelectionContext()) {
105 api.sendEvent('requestRelatedIntents', {
106 ids: api.selectOrder(),
107 hover: hoverValid() ? hov.id : ''
108 });
109 }
110 }
111
112
113 // === -----------------------------------------------------
114 // Traffic requests
115
116 function cancelTraffic() {
117 api.sendEvent('cancelTraffic');
118 }
119
120 // invoked in response to change in selection and/or mouseover/out:
121 function requestTrafficForMode() {
122 if (hoverMode === hoverModeFlows) {
123 requestDeviceLinkFlows();
124 } else if (hoverMode === hoverModeIntents) {
125 requestRelatedIntents();
126 }
127 }
128
129 // === -----------------------------
130 // keystroke commands
131
132 // keystroke-right-arrow (see topo.js)
133 function showNextIntentAction() {
134 hoverMode = hoverModeNone;
135 api.sendEvent('requestNextRelatedIntent');
136 flash.flash('>');
137 }
138
139 // keystroke-left-arrow (see topo.js)
140 function showPrevIntentAction() {
141 hoverMode = hoverModeNone;
142 api.sendEvent('requestPrevRelatedIntent');
143 flash.flash('<');
144 }
145
146 // keystroke-W (see topo.js)
147 function showSelectedIntentTrafficAction() {
148 hoverMode = hoverModeNone;
149 api.sendEvent('requestSelectedIntentTraffic');
150 flash.flash('Traffic on Selected Path');
151 }
152
153 // keystroke-A (see topo.js)
154 function showAllTrafficAction() {
155 hoverMode = hoverModeAll;
156 api.sendEvent('requestAllTraffic');
157 flash.flash('All Traffic');
158 }
159
160 // === -----------------------------
161 // action buttons on detail panel
162
163 // also, keystroke-V (see topo.js)
164 function showRelatedIntentsAction () {
165 hoverMode = hoverModeIntents;
166 requestRelatedIntents();
167 flash.flash('Related Paths');
168 }
169
170 function addHostIntentAction () {
171 var so = api.selectOrder();
172 api.sendEvent('addHostIntent', {
173 one: so[0],
174 two: so[1],
175 ids: so
176 });
177 flash.flash('Host-to-Host flow added');
178 }
179
180 function addMultiSourceIntentAction () {
181 var so = api.selectOrder();
182 api.sendEvent('addMultiSourceIntent', {
183 src: so.slice(0, so.length - 1),
184 dst: so[so.length - 1],
185 ids: so
186 });
187 flash.flash('Multi-Source flow added');
188 }
189
190 // also, keystroke-F (see topo.js)
191 function showDeviceLinkFlowsAction () {
192 hoverMode = hoverModeFlows;
193 requestDeviceLinkFlows();
194 flash.flash('Device Flows');
195 }
196
197
198 // === -----------------------------------------------------
199 // === MODULE DEFINITION ===
200
201 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800202 .factory('TopoTrafficService',
Simon Huntf542d842015-02-11 16:20:33 -0800203 ['$log', 'FnService', 'FlashService',
204
205 function (_$log_, _fs_, _flash_) {
206 $log = _$log_;
207 fs = _fs_;
208 flash = _flash_;
209
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}());