blob: 4a2ae8a3294cd2c506d9d54672af529141a7248c [file] [log] [blame]
Simon Huntef31fb22014-12-19 13:16:44 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Simon Huntef31fb22014-12-19 13:16:44 -08003 *
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 View Module
Simon Huntef31fb22014-12-19 13:16:44 -080019 */
20
21(function () {
22 'use strict';
Simon Hunt6cc53692015-01-07 11:33:45 -080023
24 var moduleDependencies = [
25 'onosUtil',
Simon Hunt1e4a0012015-01-21 11:36:08 -080026 'onosSvg',
27 'onosRemote'
Simon Hunt6cc53692015-01-07 11:33:45 -080028 ];
29
30 // references to injected services etc.
Simon Huntac4c6f72015-02-03 19:50:53 -080031 var $log, fs, ks, zs, gs, ms, sus, tfs, tis;
Simon Hunt6cc53692015-01-07 11:33:45 -080032
33 // DOM elements
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080034 var ovtopo, svg, defs, zoomLayer, mapG, forceG, noDevsLayer;
Simon Hunt6cc53692015-01-07 11:33:45 -080035
36 // Internal state
Simon Huntedf5c0e2015-01-29 15:00:53 -080037 var zoomer;
Simon Hunt6cc53692015-01-07 11:33:45 -080038
39 // Note: "exported" state should be properties on 'self' variable
40
Simon Huntcacce342015-01-07 16:13:05 -080041 // --- Short Cut Keys ------------------------------------------------
42
Simon Hunt6cc53692015-01-07 11:33:45 -080043 var keyBindings = {
Simon Huntac4c6f72015-02-03 19:50:53 -080044 //O: [toggleSummary, 'Toggle ONOS summary pane'],
45 I: [toggleInstances, 'Toggle ONOS instances pane'],
46 //D: [toggleDetails, 'Disable / enable details pane'],
47
48 //H: [toggleHosts, 'Toggle host visibility'],
49 //M: [toggleOffline, 'Toggle offline visibility'],
50 //B: [toggleBg, 'Toggle background image'],
51 //P: togglePorts,
52
53 //X: [toggleNodeLock, 'Lock / unlock node positions'],
54 //Z: [toggleOblique, 'Toggle oblique view (Experimental)'],
55 L: [cycleLabels, 'Cycle device labels'],
56 //U: [unpin, 'Unpin node (hover mouse over)'],
57 R: [resetZoom, 'Reset pan / zoom'],
58
59 //V: [showRelatedIntentsAction, 'Show all related intents'],
60 //rightArrow: [showNextIntentAction, 'Show next related intent'],
61 //leftArrow: [showPrevIntentAction, 'Show previous related intent'],
62 //W: [showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
63 //A: [showAllTrafficAction, 'Monitor all traffic'],
64 //F: [showDeviceLinkFlowsAction, 'Show device link flows'],
65
66 //E: [equalizeMasters, 'Equalize mastership roles'],
67
68 //esc: handleEscape,
69
70 _helpFormat: [
71 ['O', 'I', 'D', '-', 'H', 'M', 'B', 'P' ],
72 ['X', 'Z', 'L', 'U', 'R' ],
73 ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ]
74 ]
75
Simon Hunt6cc53692015-01-07 11:33:45 -080076 };
77
Simon Huntac4c6f72015-02-03 19:50:53 -080078 // mouse gestures
79 var gestures = [
80 ['click', 'Select the item and show details'],
81 ['shift-click', 'Toggle selection state'],
82 ['drag', 'Reposition (and pin) device / host'],
83 ['cmd-scroll', 'Zoom in / out'],
84 ['cmd-drag', 'Pan']
85 ];
86
87 function toggleInstances() {
88 if (tis.isVisible()) {
89 tis.hide();
90 } else {
91 tis.show();
92 }
93 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -080094 }
Simon Huntac4c6f72015-02-03 19:50:53 -080095
96 function cycleLabels() {
97 $log.debug('Cycle Labels.....');
Simon Hunt6cc53692015-01-07 11:33:45 -080098 }
Simon Hunt6cc53692015-01-07 11:33:45 -080099
Simon Huntcacce342015-01-07 16:13:05 -0800100 function resetZoom() {
101 zoomer.reset();
102 }
103
Simon Hunt6cc53692015-01-07 11:33:45 -0800104 function setUpKeys() {
105 ks.keyBindings(keyBindings);
106 }
107
Simon Huntcacce342015-01-07 16:13:05 -0800108
109 // --- Glyphs, Icons, and the like -----------------------------------
110
Simon Hunt6cc53692015-01-07 11:33:45 -0800111 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800112 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800113 gs.loadDefs(defs);
114 }
115
116
Simon Huntcacce342015-01-07 16:13:05 -0800117 // --- Pan and Zoom --------------------------------------------------
118
119 // zoom enabled predicate. ev is a D3 source event.
120 function zoomEnabled(ev) {
121 return (ev.metaKey || ev.altKey);
122 }
123
124 function zoomCallback() {
125 var tr = zoomer.translate(),
126 sc = zoomer.scale();
Simon Huntcacce342015-01-07 16:13:05 -0800127
Simon Hunt0541fb82015-01-14 18:59:57 -0800128 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800129 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800130 }
131
132 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800133 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800134 zoomer = zs.createZoomer({
135 svg: svg,
136 zoomLayer: zoomLayer,
137 zoomEnabled: zoomEnabled,
138 zoomCallback: zoomCallback
139 });
140 }
141
142
Simon Hunt0541fb82015-01-14 18:59:57 -0800143 // callback invoked when the SVG view has been resized..
Simon Huntb0ec1e52015-01-28 18:13:49 -0800144 function svgResized(dim) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800145 tfs.resize(dim);
Simon Hunt0541fb82015-01-14 18:59:57 -0800146 }
147
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800148 // --- Background Map ------------------------------------------------
149
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800150 function setUpNoDevs() {
151 var g, box;
152 noDevsLayer = svg.append('g').attr({
153 id: 'topo-noDevsLayer',
154 transform: sus.translate(500,500)
155 });
156 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
157 // We are translating this layer to have its origin at the center
158
159 g = noDevsLayer.append('g');
160 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
161 g.append('text').text('No devices are connected')
162 .attr({ x: 120, y: 80});
163
164 box = g.node().getBBox();
165 box.x -= box.width/2;
166 box.y -= box.height/2;
167 g.attr('transform', sus.translate(box.x, box.y));
168
169 showNoDevs(true);
170 }
171
172 function showNoDevs(b) {
173 sus.makeVisible(noDevsLayer, b);
174 }
175
Simon Hunt0541fb82015-01-14 18:59:57 -0800176 function showCallibrationPoints() {
Simon Hunt426bd862015-01-14 16:48:41 -0800177 // temp code for calibration
178 var points = [
179 [0, 0], [0, 1000], [1000, 0], [1000, 1000]
180 ];
Simon Hunt737c89f2015-01-28 12:23:19 -0800181 mapG.selectAll('circle')
Simon Hunt426bd862015-01-14 16:48:41 -0800182 .data(points)
183 .enter()
184 .append('circle')
185 .attr('cx', function (d) { return d[0]; })
186 .attr('cy', function (d) { return d[1]; })
187 .attr('r', 5)
188 .style('fill', 'red');
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800189 }
190
Simon Hunt0541fb82015-01-14 18:59:57 -0800191 function setUpMap() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800192 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Huntac4c6f72015-02-03 19:50:53 -0800193
Simon Hunt0541fb82015-01-14 18:59:57 -0800194 //showCallibrationPoints();
Simon Huntac4c6f72015-02-03 19:50:53 -0800195 //return ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5});
196
197 // returns a promise for the projection...
198 return ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800199 }
200
Simon Hunt737c89f2015-01-28 12:23:19 -0800201
Simon Huntcacce342015-01-07 16:13:05 -0800202 // --- Controller Definition -----------------------------------------
203
Simon Hunt6cc53692015-01-07 11:33:45 -0800204 angular.module('ovTopo', moduleDependencies)
205
206 .controller('OvTopoCtrl', [
Simon Hunt54442fa2015-01-26 14:17:38 -0800207 '$scope', '$log', '$location', '$timeout',
Simon Hunt4b668592015-01-29 17:33:53 -0800208 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800209 'GlyphService', 'MapService', 'SvgUtilService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800210 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Hunt4b668592015-01-29 17:33:53 -0800211 'TopoInstService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800212
Simon Hunta11b4eb2015-01-28 16:20:50 -0800213 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Huntac4c6f72015-02-03 19:50:53 -0800214 _ks_, _zs_, _gs_, _ms_, _sus_, tes, _tfs_, tps, _tis_) {
215 var self = this,
Simon Hunt1894d792015-02-04 17:09:20 -0800216 projection,
217 uplink = {
218 // provides function calls back into this space
219 showNoDevs: showNoDevs,
220 projection: function () { return projection; },
221 sendEvent: tes.sendEvent
Simon Huntac4c6f72015-02-03 19:50:53 -0800222 };
223
Simon Hunt6cc53692015-01-07 11:33:45 -0800224 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800225 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800226 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800227 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800228 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800229 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800230 sus = _sus_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800231 tfs = _tfs_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800232 tis = _tis_;
Simon Huntef31fb22014-12-19 13:16:44 -0800233
Simon Hunt426bd862015-01-14 16:48:41 -0800234 self.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800235 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800236 };
Simon Huntef31fb22014-12-19 13:16:44 -0800237
Simon Hunt54442fa2015-01-26 14:17:38 -0800238 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800239 $scope.$on('$destroy', function () {
240 $log.log('OvTopoCtrl is saying Buh-Bye!');
Simon Huntfd1231a2015-01-26 22:14:51 -0800241 tes.closeSock();
Simon Hunt626d2102015-01-29 11:54:50 -0800242 tps.destroyPanels();
Simon Hunt4b668592015-01-29 17:33:53 -0800243 tis.destroyInst();
Simon Hunt584122a2015-01-21 15:32:40 -0800244 });
245
Simon Huntcacce342015-01-07 16:13:05 -0800246 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800247 ovtopo = d3.select('#ov-topo');
248 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800249 // set the svg size to match that of the window, less the masthead
250 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800251
Simon Hunt6cc53692015-01-07 11:33:45 -0800252 setUpKeys();
253 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800254 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800255 setUpNoDevs();
Simon Hunt1894d792015-02-04 17:09:20 -0800256 setUpMap().then(
257 function (proj) {
258 projection = proj;
259 $log.debug('** We installed the projection: ', proj);
260 }
261 );
Simon Huntfd1231a2015-01-26 22:14:51 -0800262
Simon Hunt1894d792015-02-04 17:09:20 -0800263 forceG = zoomLayer.append('g').attr('id', 'topo-force');
264 tfs.initForce(forceG, uplink, svg.attr('width'), svg.attr('height'));
Simon Hunt4b668592015-01-29 17:33:53 -0800265 tis.initInst();
Simon Hunt626d2102015-01-29 11:54:50 -0800266 tps.initPanels();
Simon Huntfd1231a2015-01-26 22:14:51 -0800267 tes.openSock();
Simon Hunt6cc53692015-01-07 11:33:45 -0800268
269 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800270 }]);
271}());