blob: 051ce05914a75889e973862a789e52b769fb4da6 [file] [log] [blame]
Simon Hunt08f841d02015-02-10 14:39:20 -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 Selection Module.
19 Defines behavior when selecting nodes.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070026 var $log, fs, wss, tps, tts, ns;
Simon Hunt08f841d02015-02-10 14:39:20 -080027
28 // api to topoForce
29 var api;
30 /*
31 node() // get ref to D3 selection of nodes
32 zoomingOrPanning( ev )
33 updateDeviceColors( [dev] )
Simon Hunt0c6b2d32015-03-26 17:46:29 -070034 deselectLink()
Simon Hunt08f841d02015-02-10 14:39:20 -080035 */
36
37 // internal state
38 var hovered, // the node over which the mouse is hovering
39 selections = {}, // currently selected nodes (by id)
40 selectOrder = [], // the order in which we made selections
Simon Hunt0c6b2d32015-03-26 17:46:29 -070041 consumeClick = false; // used to coordinate with SVG click handler
Simon Hunt08f841d02015-02-10 14:39:20 -080042
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070043 // constants
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -070044 var devPath = 'device',
45 flowPath = 'flow',
Bri Prebilic Cole5c60d6e2015-05-12 09:11:03 -070046 portPath ='port',
47 groupPath = 'group';
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070048
Simon Hunt08f841d02015-02-10 14:39:20 -080049 // ==========================
50
51 function nSel() {
52 return selectOrder.length;
53 }
54 function getSel(idx) {
55 return selections[selectOrder[idx]];
56 }
57 function allSelectionsClass(cls) {
58 for (var i=0, n=nSel(); i<n; i++) {
59 if (getSel(i).obj.class !== cls) {
60 return false;
61 }
62 }
63 return true;
64 }
65
66 // ==========================
67
68 function nodeMouseOver(m) {
69 if (!m.dragStarted) {
Simon Hunt36a58c62015-04-08 11:00:07 -070070 //$log.debug("MouseOver()...", m);
Simon Hunt08f841d02015-02-10 14:39:20 -080071 if (hovered != m) {
72 hovered = m;
Simon Huntf542d842015-02-11 16:20:33 -080073 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -080074 }
75 }
76 }
77
78 function nodeMouseOut(m) {
79 if (!m.dragStarted) {
80 if (hovered) {
81 hovered = null;
Simon Huntf542d842015-02-11 16:20:33 -080082 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -080083 }
Simon Hunt36a58c62015-04-08 11:00:07 -070084 //$log.debug("MouseOut()...", m);
Simon Hunt08f841d02015-02-10 14:39:20 -080085 }
86 }
87
88 // ==========================
89
90 function selectObject(obj) {
91 var el = this,
Simon Hunt5aac2fc2015-06-09 12:34:07 -070092 nodeEv = el && el.tagName === 'g',
93 ev = d3.event.sourceEvent || {},
Simon Hunt08f841d02015-02-10 14:39:20 -080094 n;
95
96 if (api.zoomingOrPanning(ev)) {
97 return;
98 }
99
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700100 if (nodeEv) {
Simon Hunt08f841d02015-02-10 14:39:20 -0800101 n = d3.select(el);
102 } else {
103 api.node().each(function (d) {
104 if (d == obj) {
105 n = d3.select(el = this);
106 }
107 });
108 }
109 if (!n) return;
110
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700111 if (nodeEv) {
112 consumeClick = true;
113 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700114 api.deselectLink();
115
Simon Hunt08f841d02015-02-10 14:39:20 -0800116 if (ev.shiftKey && n.classed('selected')) {
117 deselectObject(obj.id);
118 updateDetail();
119 return;
120 }
121
122 if (!ev.shiftKey) {
123 deselectAll();
124 }
125
126 selections[obj.id] = { obj: obj, el: el };
127 selectOrder.push(obj.id);
128
129 n.classed('selected', true);
130 api.updateDeviceColors(obj);
131 updateDetail();
Simon Hunt08f841d02015-02-10 14:39:20 -0800132 }
133
134 function deselectObject(id) {
135 var obj = selections[id];
136 if (obj) {
137 d3.select(obj.el).classed('selected', false);
138 delete selections[id];
139 fs.removeFromArray(id, selectOrder);
140 api.updateDeviceColors(obj.obj);
141 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800142 }
143
144 function deselectAll() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700145 var something = (selectOrder.length > 0);
146
Simon Hunt08f841d02015-02-10 14:39:20 -0800147 // deselect all nodes in the network...
148 api.node().classed('selected', false);
149 selections = {};
150 selectOrder = [];
151 api.updateDeviceColors();
152 updateDetail();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700153
154 // return true if something was selected
155 return something;
Simon Hunt08f841d02015-02-10 14:39:20 -0800156 }
157
158 // === -----------------------------------------------------
159
160 function requestDetails() {
161 var data = getSel(0).obj;
Simon Hunt237676b52015-03-10 19:04:26 -0700162 wss.sendEvent('requestDetails', {
Simon Hunt08f841d02015-02-10 14:39:20 -0800163 id: data.id,
164 class: data.class
165 });
166 }
167
168 // === -----------------------------------------------------
169
170 function updateDetail() {
171 var nSel = selectOrder.length;
172 if (!nSel) {
173 emptySelect();
174 } else if (nSel === 1) {
175 singleSelect();
176 } else {
177 multiSelect();
178 }
179 }
180
181 function emptySelect() {
Simon Huntf542d842015-02-11 16:20:33 -0800182 tts.cancelTraffic();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700183 tps.displayNothing();
Simon Hunt08f841d02015-02-10 14:39:20 -0800184 }
185
186 function singleSelect() {
187 // NOTE: detail is shown from 'showDetails' event callback
188 requestDetails();
Simon Huntf542d842015-02-11 16:20:33 -0800189 tts.cancelTraffic();
190 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -0800191 }
192
193 function multiSelect() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800194 // display the selected nodes in the detail panel
195 tps.displayMulti(selectOrder);
196
197 // always add the 'show traffic' action
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700198 tps.addAction({
199 id: '-mult-rel-traf-btn',
200 gid: 'allTraffic',
201 cb: tts.showRelatedIntentsAction,
202 tt: 'Show Related Traffic'
203 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800204
205 // add other actions, based on what is selected...
206 if (nSel() === 2 && allSelectionsClass('host')) {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700207 tps.addAction({
208 id: 'host-flow-btn',
209 gid: 'endstation',
210 cb: tts.addHostIntentAction,
211 tt: 'Create Host-to-Host Flow'
212 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800213 } else if (nSel() >= 2 && allSelectionsClass('host')) {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700214 tps.addAction({
215 id: 'mult-src-flow-btn',
216 gid: 'flows',
217 cb: tts.addMultiSourceIntentAction,
218 tt: 'Create Multi-Source Flow'
219 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800220 }
221
Simon Huntf542d842015-02-11 16:20:33 -0800222 tts.cancelTraffic();
223 tts.requestTrafficForMode();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700224 tps.displaySomething();
Simon Hunt08f841d02015-02-10 14:39:20 -0800225 }
226
227
228 // === -----------------------------------------------------
229 // Event Handlers
230
231 function showDetails(data) {
Simon Hunt08f841d02015-02-10 14:39:20 -0800232 // display the data for the single selected node
233 tps.displaySingle(data);
234
235 // always add the 'show traffic' action
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700236 tps.addAction({
237 id: '-sin-rel-traf-btn',
238 gid: 'intentTraffic',
239 cb: tts.showRelatedIntentsAction,
240 tt: 'Show Related Traffic'
241 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800242
243 // add other actions, based on what is selected...
244 if (data.type === 'switch') {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700245 tps.addAction({
246 id: 'sin-dev-flows-btn',
247 gid: 'flows',
248 cb: tts.showDeviceLinkFlowsAction,
249 tt: 'Show Device Flows'
250 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800251 }
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700252 // TODO: have the server return explicit class and ID of each node
253 // for now, we assume the node is a device if it has a URI
254 if ((data.props).hasOwnProperty('URI')) {
255 tps.addAction({
Bri Prebilic Cole17c6d0a2015-07-16 14:56:40 -0700256 id: 'device-table-btn',
257 gid: data.type,
258 cb: function () {
259 ns.navTo(devPath, { devId: data.props['URI'] });
260 },
261 tt: 'Show device view'
262 });
263 tps.addAction({
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700264 id: 'flows-table-btn',
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700265 gid: 'flowTable',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700266 cb: function () {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700267 ns.navTo(flowPath, { devId: data.props['URI'] });
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700268 },
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700269 tt: 'Show flow view for this device'
270 });
271 tps.addAction({
272 id: 'ports-table-btn',
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700273 gid: 'portTable',
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700274 cb: function () {
275 ns.navTo(portPath, { devId: data.props['URI'] });
276 },
277 tt: 'Show port view for this device'
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700278 });
Bri Prebilic Cole5c60d6e2015-05-12 09:11:03 -0700279 tps.addAction({
280 id: 'groups-table-btn',
281 gid: 'groupTable',
282 cb: function () {
283 ns.navTo(groupPath, { devId: data.props['URI'] });
284 },
285 tt: 'Show group view for this device'
286 });
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700287 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800288
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700289 tps.displaySomething();
Simon Hunt6036b192015-02-11 11:20:26 -0800290 }
291
Simon Huntf542d842015-02-11 16:20:33 -0800292 function validateSelectionContext() {
293 if (!hovered && !nSel()) {
294 tts.cancelTraffic();
295 return false;
296 }
297 return true;
Simon Hunt08f841d02015-02-10 14:39:20 -0800298 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800299
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700300 function clickConsumed(x) {
301 var cc = consumeClick;
302 consumeClick = !!x;
303 return cc;
304 }
305
Simon Hunt08f841d02015-02-10 14:39:20 -0800306 // === -----------------------------------------------------
307 // === MODULE DEFINITION ===
308
309 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800310 .factory('TopoSelectService',
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700311 ['$log', 'FnService', 'WebSocketService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700312 'TopoPanelService', 'TopoTrafficService', 'NavService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800313
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700314 function (_$log_, _fs_, _wss_, _tps_, _tts_, _ns_) {
Simon Hunt6036b192015-02-11 11:20:26 -0800315 $log = _$log_;
316 fs = _fs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700317 wss = _wss_;
Simon Hunt6036b192015-02-11 11:20:26 -0800318 tps = _tps_;
Simon Huntf542d842015-02-11 16:20:33 -0800319 tts = _tts_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700320 ns = _ns_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800321
Simon Hunt6036b192015-02-11 11:20:26 -0800322 function initSelect(_api_) {
323 api = _api_;
324 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800325
Simon Hunt6036b192015-02-11 11:20:26 -0800326 function destroySelect() { }
Simon Hunt08f841d02015-02-10 14:39:20 -0800327
Simon Hunt6036b192015-02-11 11:20:26 -0800328 return {
329 initSelect: initSelect,
330 destroySelect: destroySelect,
Simon Hunt08f841d02015-02-10 14:39:20 -0800331
Simon Hunt6036b192015-02-11 11:20:26 -0800332 showDetails: showDetails,
Simon Hunt08f841d02015-02-10 14:39:20 -0800333
Simon Hunt6036b192015-02-11 11:20:26 -0800334 nodeMouseOver: nodeMouseOver,
335 nodeMouseOut: nodeMouseOut,
336 selectObject: selectObject,
337 deselectObject: deselectObject,
338 deselectAll: deselectAll,
Simon Huntf542d842015-02-11 16:20:33 -0800339
Simon Hunta0eb0a82015-02-11 12:30:06 -0800340 hovered: function () { return hovered; },
Simon Huntf542d842015-02-11 16:20:33 -0800341 selectOrder: function () { return selectOrder; },
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700342 validateSelectionContext: validateSelectionContext,
343
344 clickConsumed: clickConsumed
Simon Hunt6036b192015-02-11 11:20:26 -0800345 };
346 }]);
Simon Hunt08f841d02015-02-10 14:39:20 -0800347}());