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