blob: b8153c30a3a20957dd297a4a2918d9df4333574c [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 Coleac829e42015-05-05 13:42:06 -070044 var flowPath = 'flow',
Bri Prebilic Cole5c60d6e2015-05-12 09:11:03 -070045 portPath ='port',
46 groupPath = 'group';
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070047
Simon Hunt08f841d02015-02-10 14:39:20 -080048 // ==========================
49
50 function nSel() {
51 return selectOrder.length;
52 }
53 function getSel(idx) {
54 return selections[selectOrder[idx]];
55 }
56 function allSelectionsClass(cls) {
57 for (var i=0, n=nSel(); i<n; i++) {
58 if (getSel(i).obj.class !== cls) {
59 return false;
60 }
61 }
62 return true;
63 }
64
65 // ==========================
66
67 function nodeMouseOver(m) {
68 if (!m.dragStarted) {
Simon Hunt36a58c62015-04-08 11:00:07 -070069 //$log.debug("MouseOver()...", m);
Simon Hunt08f841d02015-02-10 14:39:20 -080070 if (hovered != m) {
71 hovered = m;
Simon Huntf542d842015-02-11 16:20:33 -080072 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -080073 }
74 }
75 }
76
77 function nodeMouseOut(m) {
78 if (!m.dragStarted) {
79 if (hovered) {
80 hovered = null;
Simon Huntf542d842015-02-11 16:20:33 -080081 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -080082 }
Simon Hunt36a58c62015-04-08 11:00:07 -070083 //$log.debug("MouseOut()...", m);
Simon Hunt08f841d02015-02-10 14:39:20 -080084 }
85 }
86
87 // ==========================
88
89 function selectObject(obj) {
90 var el = this,
91 ev = d3.event.sourceEvent,
92 n;
93
94 if (api.zoomingOrPanning(ev)) {
95 return;
96 }
97
98 if (el) {
99 n = d3.select(el);
100 } else {
101 api.node().each(function (d) {
102 if (d == obj) {
103 n = d3.select(el = this);
104 }
105 });
106 }
107 if (!n) return;
108
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700109 consumeClick = true;
110 api.deselectLink();
111
Simon Hunt08f841d02015-02-10 14:39:20 -0800112 if (ev.shiftKey && n.classed('selected')) {
113 deselectObject(obj.id);
114 updateDetail();
115 return;
116 }
117
118 if (!ev.shiftKey) {
119 deselectAll();
120 }
121
122 selections[obj.id] = { obj: obj, el: el };
123 selectOrder.push(obj.id);
124
125 n.classed('selected', true);
126 api.updateDeviceColors(obj);
127 updateDetail();
Simon Hunt08f841d02015-02-10 14:39:20 -0800128 }
129
130 function deselectObject(id) {
131 var obj = selections[id];
132 if (obj) {
133 d3.select(obj.el).classed('selected', false);
134 delete selections[id];
135 fs.removeFromArray(id, selectOrder);
136 api.updateDeviceColors(obj.obj);
137 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800138 }
139
140 function deselectAll() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700141 var something = (selectOrder.length > 0);
142
Simon Hunt08f841d02015-02-10 14:39:20 -0800143 // deselect all nodes in the network...
144 api.node().classed('selected', false);
145 selections = {};
146 selectOrder = [];
147 api.updateDeviceColors();
148 updateDetail();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700149
150 // return true if something was selected
151 return something;
Simon Hunt08f841d02015-02-10 14:39:20 -0800152 }
153
154 // === -----------------------------------------------------
155
156 function requestDetails() {
157 var data = getSel(0).obj;
Simon Hunt237676b52015-03-10 19:04:26 -0700158 wss.sendEvent('requestDetails', {
Simon Hunt08f841d02015-02-10 14:39:20 -0800159 id: data.id,
160 class: data.class
161 });
162 }
163
164 // === -----------------------------------------------------
165
166 function updateDetail() {
167 var nSel = selectOrder.length;
168 if (!nSel) {
169 emptySelect();
170 } else if (nSel === 1) {
171 singleSelect();
172 } else {
173 multiSelect();
174 }
175 }
176
177 function emptySelect() {
Simon Huntf542d842015-02-11 16:20:33 -0800178 tts.cancelTraffic();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700179 tps.displayNothing();
Simon Hunt08f841d02015-02-10 14:39:20 -0800180 }
181
182 function singleSelect() {
183 // NOTE: detail is shown from 'showDetails' event callback
184 requestDetails();
Simon Huntf542d842015-02-11 16:20:33 -0800185 tts.cancelTraffic();
186 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -0800187 }
188
189 function multiSelect() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800190 // display the selected nodes in the detail panel
191 tps.displayMulti(selectOrder);
192
193 // always add the 'show traffic' action
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700194 tps.addAction({
195 id: '-mult-rel-traf-btn',
196 gid: 'allTraffic',
197 cb: tts.showRelatedIntentsAction,
198 tt: 'Show Related Traffic'
199 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800200
201 // add other actions, based on what is selected...
202 if (nSel() === 2 && allSelectionsClass('host')) {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700203 tps.addAction({
204 id: 'host-flow-btn',
205 gid: 'endstation',
206 cb: tts.addHostIntentAction,
207 tt: 'Create Host-to-Host Flow'
208 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800209 } else if (nSel() >= 2 && allSelectionsClass('host')) {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700210 tps.addAction({
211 id: 'mult-src-flow-btn',
212 gid: 'flows',
213 cb: tts.addMultiSourceIntentAction,
214 tt: 'Create Multi-Source Flow'
215 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800216 }
217
Simon Huntf542d842015-02-11 16:20:33 -0800218 tts.cancelTraffic();
219 tts.requestTrafficForMode();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700220 tps.displaySomething();
Simon Hunt08f841d02015-02-10 14:39:20 -0800221 }
222
223
224 // === -----------------------------------------------------
225 // Event Handlers
226
227 function showDetails(data) {
Simon Hunt08f841d02015-02-10 14:39:20 -0800228 // display the data for the single selected node
229 tps.displaySingle(data);
230
231 // always add the 'show traffic' action
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700232 tps.addAction({
233 id: '-sin-rel-traf-btn',
234 gid: 'intentTraffic',
235 cb: tts.showRelatedIntentsAction,
236 tt: 'Show Related Traffic'
237 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800238
239 // add other actions, based on what is selected...
240 if (data.type === 'switch') {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -0700241 tps.addAction({
242 id: 'sin-dev-flows-btn',
243 gid: 'flows',
244 cb: tts.showDeviceLinkFlowsAction,
245 tt: 'Show Device Flows'
246 });
Simon Hunt08f841d02015-02-10 14:39:20 -0800247 }
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700248 // TODO: have the server return explicit class and ID of each node
249 // for now, we assume the node is a device if it has a URI
250 if ((data.props).hasOwnProperty('URI')) {
251 tps.addAction({
252 id: 'flows-table-btn',
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700253 gid: 'flowTable',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700254 cb: function () {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700255 ns.navTo(flowPath, { devId: data.props['URI'] });
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700256 },
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700257 tt: 'Show flow view for this device'
258 });
259 tps.addAction({
260 id: 'ports-table-btn',
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700261 gid: 'portTable',
Bri Prebilic Coleac829e42015-05-05 13:42:06 -0700262 cb: function () {
263 ns.navTo(portPath, { devId: data.props['URI'] });
264 },
265 tt: 'Show port view for this device'
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700266 });
Bri Prebilic Cole5c60d6e2015-05-12 09:11:03 -0700267 tps.addAction({
268 id: 'groups-table-btn',
269 gid: 'groupTable',
270 cb: function () {
271 ns.navTo(groupPath, { devId: data.props['URI'] });
272 },
273 tt: 'Show group view for this device'
274 });
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700275 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800276
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700277 tps.displaySomething();
Simon Hunt6036b192015-02-11 11:20:26 -0800278 }
279
Simon Huntf542d842015-02-11 16:20:33 -0800280 function validateSelectionContext() {
281 if (!hovered && !nSel()) {
282 tts.cancelTraffic();
283 return false;
284 }
285 return true;
Simon Hunt08f841d02015-02-10 14:39:20 -0800286 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800287
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700288 function clickConsumed(x) {
289 var cc = consumeClick;
290 consumeClick = !!x;
291 return cc;
292 }
293
Simon Hunt08f841d02015-02-10 14:39:20 -0800294 // === -----------------------------------------------------
295 // === MODULE DEFINITION ===
296
297 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800298 .factory('TopoSelectService',
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700299 ['$log', 'FnService', 'WebSocketService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700300 'TopoPanelService', 'TopoTrafficService', 'NavService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800301
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700302 function (_$log_, _fs_, _wss_, _tps_, _tts_, _ns_) {
Simon Hunt6036b192015-02-11 11:20:26 -0800303 $log = _$log_;
304 fs = _fs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700305 wss = _wss_;
Simon Hunt6036b192015-02-11 11:20:26 -0800306 tps = _tps_;
Simon Huntf542d842015-02-11 16:20:33 -0800307 tts = _tts_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700308 ns = _ns_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800309
Simon Hunt6036b192015-02-11 11:20:26 -0800310 function initSelect(_api_) {
311 api = _api_;
312 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800313
Simon Hunt6036b192015-02-11 11:20:26 -0800314 function destroySelect() { }
Simon Hunt08f841d02015-02-10 14:39:20 -0800315
Simon Hunt6036b192015-02-11 11:20:26 -0800316 return {
317 initSelect: initSelect,
318 destroySelect: destroySelect,
Simon Hunt08f841d02015-02-10 14:39:20 -0800319
Simon Hunt6036b192015-02-11 11:20:26 -0800320 showDetails: showDetails,
Simon Hunt08f841d02015-02-10 14:39:20 -0800321
Simon Hunt6036b192015-02-11 11:20:26 -0800322 nodeMouseOver: nodeMouseOver,
323 nodeMouseOut: nodeMouseOut,
324 selectObject: selectObject,
325 deselectObject: deselectObject,
326 deselectAll: deselectAll,
Simon Huntf542d842015-02-11 16:20:33 -0800327
Simon Hunta0eb0a82015-02-11 12:30:06 -0800328 hovered: function () { return hovered; },
Simon Huntf542d842015-02-11 16:20:33 -0800329 selectOrder: function () { return selectOrder; },
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700330 validateSelectionContext: validateSelectionContext,
331
332 clickConsumed: clickConsumed
Simon Hunt6036b192015-02-11 11:20:26 -0800333 };
334 }]);
Simon Hunt08f841d02015-02-10 14:39:20 -0800335}());