Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 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 Instances Module. |
| 19 | Defines modeling of ONOS instances. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 26 | var $log, ps, sus, gs, ts, fs, flash; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 27 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 28 | // api from topo |
| 29 | var api; |
| 30 | /* |
| 31 | showMastership( id ) |
| 32 | */ |
| 33 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 34 | // configuration |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 35 | var showLogicErrors = true, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 36 | idIns = 'topo-p-instance', |
| 37 | instOpts = { |
| 38 | edge: 'left', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 39 | width: 20, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | // internal state |
| 43 | var onosInstances, |
| 44 | onosOrder, |
| 45 | oiShowMaster, |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 46 | oiBox; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 47 | |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 48 | // function to be replaced by the localization bundle function |
| 49 | var topoLion = function (x) { |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 50 | return '#tis#' + x + '#'; |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 53 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 54 | function addInstance(data) { |
| 55 | var id = data.id; |
| 56 | |
| 57 | if (onosInstances[id]) { |
| 58 | updateInstance(data); |
| 59 | return; |
| 60 | } |
| 61 | onosInstances[id] = data; |
| 62 | onosOrder.push(data); |
| 63 | updateInstances(); |
| 64 | } |
| 65 | |
| 66 | function updateInstance(data) { |
| 67 | var id = data.id, |
| 68 | d = onosInstances[id]; |
| 69 | if (d) { |
| 70 | angular.extend(d, data); |
| 71 | updateInstances(); |
| 72 | } else { |
| 73 | logicError('updateInstance: lookup fail: ID = "' + id + '"'); |
| 74 | } |
| 75 | } |
| 76 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 77 | function removeInstance(data) { |
| 78 | var id = data.id, |
| 79 | d = onosInstances[id]; |
| 80 | if (d) { |
| 81 | var idx = fs.find(id, onosOrder); |
| 82 | if (idx >= 0) { |
| 83 | onosOrder.splice(idx, 1); |
| 84 | } |
| 85 | delete onosInstances[id]; |
| 86 | updateInstances(); |
| 87 | } else { |
| 88 | logicError('removeInstance lookup fail. ID = "' + id + '"'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // ========================== |
| 93 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 94 | function clickInst(d) { |
| 95 | var el = d3.select(this), |
| 96 | aff = el.classed('affinity'); |
| 97 | if (!aff) { |
| 98 | setAffinity(el, d); |
| 99 | } else { |
| 100 | cancelAffinity(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function setAffinity(el, d) { |
| 105 | d3.selectAll('.onosInst') |
| 106 | .classed('mastership', true) |
| 107 | .classed('affinity', false); |
| 108 | el.classed('affinity', true); |
| 109 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 110 | // suppress all elements except nodes whose master is this instance |
| 111 | api.showMastership(d.id); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 112 | oiShowMaster = true; |
| 113 | } |
| 114 | |
| 115 | function cancelAffinity() { |
| 116 | d3.selectAll('.onosInst') |
| 117 | .classed('mastership affinity', false); |
| 118 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 119 | api.showMastership(null); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 120 | oiShowMaster = false; |
| 121 | } |
| 122 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 123 | function attachUiBadge(svg) { |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 124 | gs.addGlyph(svg, 'uiAttached', 24, true, [14, 54]) |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 125 | .classed('badgeIcon uiBadge', true); |
| 126 | } |
| 127 | |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 128 | function attachReadyBadge(svg) { |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 129 | gs.addGlyph(svg, 'checkMark', 16, true, [18, 40]) |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 130 | .classed('badgeIcon readyBadge', true); |
| 131 | } |
| 132 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 133 | function instColor(id, online) { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 134 | return sus.cat7().getColor(id, !online, ts.theme()); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // ============================== |
| 138 | |
| 139 | function updateInstances() { |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 140 | var rox = 5, |
| 141 | roy = 5, |
| 142 | rw = 160, |
| 143 | rhh = 30, |
| 144 | rbh = 45, |
| 145 | tx = 48, |
| 146 | instSvg = { |
| 147 | width: 170, |
| 148 | height: 85, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 149 | viewBox: '0 0 170 85', |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 150 | }, |
| 151 | headRect = { |
| 152 | x: rox, |
| 153 | y: roy, |
| 154 | width: rw, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 155 | height: rhh, |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 156 | }, |
| 157 | bodyRect = { |
| 158 | x: rox, |
| 159 | y: roy + rhh, |
| 160 | width: rw, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 161 | height: rbh, |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 162 | }, |
| 163 | titleAttr = { |
| 164 | class: 'instTitle', |
| 165 | x: tx, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 166 | y: 27, |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 169 | var onoses = oiBox.el().selectAll('.onosInst') |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 170 | .data(onosOrder, function (d) { return d.id; }); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 171 | |
| 172 | function nSw(n) { |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 173 | return topoLion('devices') + ': ' + n; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // operate on existing onos instances if necessary |
| 177 | onoses.each(function (d) { |
| 178 | var el = d3.select(this), |
| 179 | svg = el.select('svg'); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 180 | |
| 181 | // update online state |
| 182 | el.classed('online', d.online); |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 183 | el.classed('ready', d.ready); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 184 | |
| 185 | // update ui-attached state |
| 186 | svg.select('use.uiBadge').remove(); |
| 187 | if (d.uiAttached) { |
| 188 | attachUiBadge(svg); |
| 189 | } |
| 190 | |
| 191 | function updAttr(id, value) { |
| 192 | svg.select('text.instLabel.'+id).text(value); |
| 193 | } |
| 194 | |
| 195 | updAttr('ip', d.ip); |
| 196 | updAttr('ns', nSw(d.switches)); |
| 197 | }); |
| 198 | |
| 199 | |
| 200 | // operate on new onos instances |
| 201 | var entering = onoses.enter() |
| 202 | .append('div') |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 203 | .classed('onosInst', true) |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 204 | .classed('online', function (d) { return d.online; }) |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 205 | .classed('ready', function (d) { return d.ready; }) |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 206 | .on('click', clickInst); |
| 207 | |
| 208 | entering.each(function (d) { |
| 209 | var el = d3.select(this), |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 210 | svg = el.append('svg').attr(instSvg); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 211 | |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 212 | svg.append('rect').attr(headRect); |
| 213 | svg.append('rect').attr(bodyRect); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 214 | |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 215 | gs.addGlyph(svg, 'bird', 20, false, [15, 10]) |
| 216 | .classed('badgeIcon bird', true); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 217 | |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 218 | attachReadyBadge(svg); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 219 | |
| 220 | if (d.uiAttached) { |
| 221 | attachUiBadge(svg); |
| 222 | } |
| 223 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 224 | svg.append('text') |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 225 | .attr(titleAttr) |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 226 | .text(d.id); |
| 227 | |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 228 | var ty = 55; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 229 | function addAttr(id, label) { |
| 230 | svg.append('text').attr({ |
| 231 | class: 'instLabel ' + id, |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 232 | x: tx, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 233 | y: ty, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 234 | }).text(label); |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 235 | ty += 18; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | addAttr('ip', d.ip); |
| 239 | addAttr('ns', nSw(d.switches)); |
| 240 | }); |
| 241 | |
| 242 | // operate on existing + new onoses here |
| 243 | // set the affinity colors... |
| 244 | onoses.each(function (d) { |
| 245 | var el = d3.select(this), |
| 246 | rect = el.select('svg').select('rect'), |
| 247 | col = instColor(d.id, d.online); |
| 248 | rect.style('fill', col); |
| 249 | }); |
| 250 | |
| 251 | // adjust the panel size appropriately... |
Simon Hunt | a976134 | 2016-06-10 18:02:53 -0700 | [diff] [blame] | 252 | oiBox.width(instSvg.width * onosOrder.length); |
| 253 | oiBox.height(instSvg.height); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 254 | |
| 255 | // remove any outgoing instances |
| 256 | onoses.exit().remove(); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | // ========================== |
| 261 | |
| 262 | function logicError(msg) { |
| 263 | if (showLogicErrors) { |
| 264 | $log.warn('TopoInstService: ' + msg); |
| 265 | } |
| 266 | } |
| 267 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 268 | function initInst(_api_) { |
| 269 | api = _api_; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 270 | oiBox = ps.createPanel(idIns, instOpts); |
| 271 | oiBox.show(); |
| 272 | |
| 273 | onosInstances = {}; |
| 274 | onosOrder = []; |
| 275 | oiShowMaster = false; |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 276 | |
| 277 | // we want to update the instances, each time the theme changes |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 278 | ts.addListener(updateInstances); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | function destroyInst() { |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 282 | ts.removeListener(updateInstances); |
Simon Hunt | 245a88e | 2015-02-02 13:26:04 -0800 | [diff] [blame] | 283 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 284 | ps.destroyPanel(idIns); |
| 285 | oiBox = null; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 286 | |
| 287 | onosInstances = {}; |
| 288 | onosOrder = []; |
| 289 | oiShowMaster = false; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 292 | function showInsts() { |
| 293 | oiBox.show(); |
| 294 | } |
| 295 | |
| 296 | function hideInsts() { |
| 297 | oiBox.hide(); |
| 298 | } |
| 299 | |
| 300 | function toggleInsts(x) { |
| 301 | var kev = (x === 'keyev'), |
| 302 | on, |
| 303 | verb; |
| 304 | |
| 305 | if (kev) { |
| 306 | on = oiBox.toggle(); |
| 307 | } else { |
| 308 | on = !!x; |
| 309 | if (on) { |
| 310 | showInsts(); |
| 311 | } else { |
| 312 | hideInsts(); |
| 313 | } |
| 314 | } |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 315 | verb = on ? topoLion('show') : topoLion('hide'); |
| 316 | flash.flash(verb + ' ' + topoLion('fl_panel_instances')); |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 317 | return on; |
| 318 | } |
| 319 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 320 | // ========================== |
| 321 | |
| 322 | angular.module('ovTopo') |
| 323 | .factory('TopoInstService', |
| 324 | ['$log', 'PanelService', 'SvgUtilService', 'GlyphService', |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 325 | 'ThemeService', 'FnService', 'FlashService', |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 326 | |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 327 | function (_$log_, _ps_, _sus_, _gs_, _ts_, _fs_, _flash_) { |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 328 | $log = _$log_; |
| 329 | ps = _ps_; |
| 330 | sus = _sus_; |
| 331 | gs = _gs_; |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 332 | ts = _ts_; |
| 333 | fs = _fs_; |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 334 | flash = _flash_; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 335 | |
| 336 | return { |
| 337 | initInst: initInst, |
| 338 | destroyInst: destroyInst, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 339 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 340 | addInstance: addInstance, |
| 341 | updateInstance: updateInstance, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 342 | removeInstance: removeInstance, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 343 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 344 | cancelAffinity: cancelAffinity, |
| 345 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 346 | isVisible: function () { return oiBox.isVisible(); }, |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 347 | show: showInsts, |
| 348 | hide: hideInsts, |
| 349 | toggle: toggleInsts, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 350 | showMaster: function () { return oiShowMaster; }, |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 351 | setLionBundle: function (bundle) { topoLion = bundle; }, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 352 | }; |
| 353 | }]); |
| 354 | }()); |