Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | // injected refs |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 5 | var $log, ps, sus, gs, flash, ts, t2mss; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 6 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 7 | // configuration |
| 8 | var showLogicErrors = true, |
Steven Burrows | 7a9d04e | 2016-09-26 17:05:37 -0700 | [diff] [blame] | 9 | idIns = 'topo2-p-instance', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 10 | instOpts = { |
| 11 | edge: 'left', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 12 | width: 20, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | // internal state |
| 16 | var onosInstances, |
| 17 | onosOrder, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 18 | oiBox; |
| 19 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 20 | function addInstance(data) { |
| 21 | var id = data.id; |
| 22 | |
| 23 | if (onosInstances[id]) { |
| 24 | updateInstance(data); |
| 25 | return; |
| 26 | } |
| 27 | onosInstances[id] = data; |
| 28 | onosOrder.push(data); |
| 29 | updateInstances(); |
| 30 | } |
| 31 | |
| 32 | function updateInstance(data) { |
| 33 | var id = data.id, |
| 34 | d = onosInstances[id]; |
| 35 | if (d) { |
| 36 | angular.extend(d, data); |
| 37 | updateInstances(); |
| 38 | } else { |
| 39 | logicError('updateInstance: lookup fail: ID = "' + id + '"'); |
| 40 | } |
| 41 | } |
| 42 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 43 | // ========================== |
| 44 | |
| 45 | function clickInst(d) { |
| 46 | var el = d3.select(this), |
| 47 | aff = el.classed('affinity'); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 48 | if (aff) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 49 | cancelAffinity(); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 50 | } else { |
| 51 | setAffinity(el, d); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | function setAffinity(el, d) { |
| 56 | d3.selectAll('.onosInst') |
| 57 | .classed('mastership', true) |
| 58 | .classed('affinity', false); |
| 59 | el.classed('affinity', true); |
| 60 | |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 61 | t2mss.setMastership(d.id); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | function cancelAffinity() { |
| 65 | d3.selectAll('.onosInst') |
| 66 | .classed('mastership affinity', false); |
| 67 | |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 68 | t2mss.setMastership(null); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | function attachUiBadge(svg) { |
| 72 | gs.addGlyph(svg, 'uiAttached', 24, true, [14, 54]) |
| 73 | .classed('badgeIcon uiBadge', true); |
| 74 | } |
| 75 | |
| 76 | function attachReadyBadge(svg) { |
| 77 | gs.addGlyph(svg, 'checkMark', 16, true, [18, 40]) |
| 78 | .classed('badgeIcon readyBadge', true); |
| 79 | } |
| 80 | |
| 81 | function instColor(id, online) { |
| 82 | return sus.cat7().getColor(id, !online, ts.theme()); |
| 83 | } |
| 84 | |
| 85 | // ============================== |
| 86 | |
| 87 | function updateInstances() { |
| 88 | var rox = 5, |
| 89 | roy = 5, |
| 90 | rw = 160, |
| 91 | rhh = 30, |
| 92 | rbh = 45, |
| 93 | tx = 48, |
| 94 | instSvg = { |
| 95 | width: 170, |
| 96 | height: 85, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 97 | viewBox: '0 0 170 85', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 98 | }, |
| 99 | headRect = { |
| 100 | x: rox, |
| 101 | y: roy, |
| 102 | width: rw, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 103 | height: rhh, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 104 | }, |
| 105 | bodyRect = { |
| 106 | x: rox, |
| 107 | y: roy + rhh, |
| 108 | width: rw, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 109 | height: rbh, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 110 | }, |
| 111 | titleAttr = { |
| 112 | class: 'instTitle', |
| 113 | x: tx, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 114 | y: 27, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | var onoses = oiBox.el().selectAll('.onosInst') |
| 118 | .data(onosOrder, function (d) { return d.id; }); |
| 119 | |
| 120 | function nSw(n) { |
| 121 | return 'Devices: ' + n; |
| 122 | } |
| 123 | |
| 124 | // operate on existing onos instances if necessary |
| 125 | onoses.each(function (d) { |
| 126 | var el = d3.select(this), |
| 127 | svg = el.select('svg'); |
| 128 | |
| 129 | // update online state |
| 130 | el.classed('online', d.online); |
| 131 | el.classed('ready', d.ready); |
| 132 | |
| 133 | // update ui-attached state |
| 134 | svg.select('use.uiBadge').remove(); |
| 135 | if (d.uiAttached) { |
| 136 | attachUiBadge(svg); |
| 137 | } |
| 138 | |
| 139 | function updAttr(id, value) { |
| 140 | svg.select('text.instLabel.' + id).text(value); |
| 141 | } |
| 142 | |
| 143 | updAttr('ip', d.ip); |
| 144 | updAttr('ns', nSw(d.switches)); |
| 145 | }); |
| 146 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 147 | // operate on new onos instances |
| 148 | var entering = onoses.enter() |
| 149 | .append('div') |
| 150 | .classed('onosInst', true) |
| 151 | .classed('online', function (d) { return d.online; }) |
| 152 | .classed('ready', function (d) { return d.ready; }) |
| 153 | .on('click', clickInst); |
| 154 | |
| 155 | entering.each(function (d) { |
| 156 | var el = d3.select(this), |
| 157 | svg = el.append('svg').attr(instSvg); |
| 158 | |
| 159 | svg.append('rect').attr(headRect); |
| 160 | svg.append('rect').attr(bodyRect); |
| 161 | |
| 162 | gs.addGlyph(svg, 'bird', 20, false, [15, 10]) |
| 163 | .classed('badgeIcon bird', true); |
| 164 | |
| 165 | attachReadyBadge(svg); |
| 166 | |
| 167 | if (d.uiAttached) { |
| 168 | attachUiBadge(svg); |
| 169 | } |
| 170 | |
| 171 | svg.append('text') |
| 172 | .attr(titleAttr) |
| 173 | .text(d.id); |
| 174 | |
| 175 | var ty = 55; |
| 176 | function addAttr(id, label) { |
| 177 | svg.append('text').attr({ |
| 178 | class: 'instLabel ' + id, |
| 179 | x: tx, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 180 | y: ty, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 181 | }).text(label); |
| 182 | ty += 18; |
| 183 | } |
| 184 | |
| 185 | addAttr('ip', d.ip); |
| 186 | addAttr('ns', nSw(d.switches)); |
| 187 | }); |
| 188 | |
| 189 | // operate on existing + new onoses here |
| 190 | // set the affinity colors... |
| 191 | onoses.each(function (d) { |
| 192 | |
| 193 | var el = d3.select(this), |
| 194 | rect = el.select('svg').select('rect'), |
| 195 | col = instColor(d.id, d.online); |
| 196 | |
| 197 | rect.style('fill', col); |
| 198 | }); |
| 199 | |
| 200 | // adjust the panel size appropriately... |
| 201 | oiBox.width(instSvg.width * onosOrder.length); |
| 202 | oiBox.height(instSvg.height); |
| 203 | |
| 204 | // remove any outgoing instances |
| 205 | onoses.exit().remove(); |
| 206 | } |
| 207 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 208 | // ========================== |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 209 | function logicError(msg) { |
| 210 | if (showLogicErrors) { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 211 | $log.warn('Topo2InstService: ' + msg); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 215 | function initInst() { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 216 | oiBox = ps.createPanel(idIns, instOpts); |
| 217 | oiBox.show(); |
| 218 | |
| 219 | onosInstances = {}; |
| 220 | onosOrder = []; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 221 | |
| 222 | // we want to update the instances, each time the theme changes |
| 223 | ts.addListener(updateInstances); |
| 224 | } |
| 225 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 226 | function allInstances(data) { |
| 227 | $log.debug('Update all instances', data); |
| 228 | |
| 229 | var members = data.members; |
| 230 | |
| 231 | members.forEach(function (member) { |
| 232 | addInstance(member); |
| 233 | }); |
| 234 | } |
| 235 | |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 236 | function toggle(x) { |
| 237 | var kev = (x === 'keyev'), |
| 238 | on, |
| 239 | verb; |
| 240 | |
| 241 | if (kev) { |
| 242 | on = oiBox.toggle(); |
| 243 | } else { |
| 244 | on = Boolean(x); |
| 245 | if (on) { |
| 246 | oiBox.show(); |
| 247 | } else { |
| 248 | oiBox.hide(); |
| 249 | } |
| 250 | } |
| 251 | verb = on ? 'Show' : 'Hide'; |
| 252 | flash.flash(verb + ' instances panel'); |
| 253 | return on; |
| 254 | } |
| 255 | |
Steven Burrows | 7a9d04e | 2016-09-26 17:05:37 -0700 | [diff] [blame] | 256 | function destroy() { |
| 257 | ts.removeListener(updateInstances); |
| 258 | |
| 259 | ps.destroyPanel(idIns); |
| 260 | oiBox = null; |
| 261 | |
| 262 | onosInstances = {}; |
| 263 | onosOrder = []; |
| 264 | } |
| 265 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 266 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 267 | .factory('Topo2InstanceService', [ |
| 268 | '$log', 'PanelService', 'SvgUtilService', 'GlyphService', |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 269 | 'FlashService', 'ThemeService', 'Topo2MastershipService', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 270 | |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 271 | function (_$log_, _ps_, _sus_, _gs_, _flash_, _ts_, _t2mss_) { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 272 | $log = _$log_; |
| 273 | ps = _ps_; |
| 274 | sus = _sus_; |
| 275 | gs = _gs_; |
| 276 | flash = _flash_; |
| 277 | ts = _ts_; |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 278 | t2mss = _t2mss_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 279 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 280 | return { |
| 281 | initInst: initInst, |
| 282 | allInstances: allInstances, |
| 283 | destroy: destroy, |
| 284 | toggle: toggle, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 285 | isVisible: function () { return oiBox.isVisible(); }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 286 | }; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 287 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 288 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 289 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 290 | })(); |