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