Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 Force Module. |
| 19 | Visualization of the topology in an SVG layer, using a D3 Force Layout. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
| 26 | var $log, wss; |
| 27 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 28 | // selected DOM refs |
| 29 | var topdiv; |
| 30 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 31 | // ========================== Helper Functions |
| 32 | |
| 33 | function init() { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 34 | topdiv = d3.select('#topoXtmp'); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 35 | $log.debug('Initialize topo force layout'); |
| 36 | } |
| 37 | |
| 38 | function destroy() { |
| 39 | $log.debug('Destroy topo force layout'); |
| 40 | } |
| 41 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 42 | function rmP(div) { |
| 43 | div.selectAll('p').remove(); |
| 44 | } |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 45 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 46 | function navRequest(rid) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 47 | wss.sendEvent('topo2navRegion', { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 48 | rid: rid |
| 49 | }); |
| 50 | } |
| 51 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 52 | function doTmpInstances(data) { |
| 53 | var idiv = topdiv.select('.instances').select('div'); |
| 54 | data.members.forEach(function (m) { |
| 55 | idiv.append('div').text(m.id); |
| 56 | }); |
| 57 | } |
| 58 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 59 | function doTmpCurrentLayout(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 60 | var ldDiv = topdiv.select('.layoutData'), |
| 61 | bcs = ldDiv.select('.l_crumbs'); |
| 62 | |
| 63 | function setSpan(v, val) { |
| 64 | var cls = '.l_' + v, |
| 65 | span = ldDiv.select(cls).select('span'), |
| 66 | value = val || data[v]; |
| 67 | span.html(value); |
| 68 | } |
| 69 | |
| 70 | setSpan('id'); |
| 71 | setSpan('parent'); |
| 72 | setSpan('region'); |
| 73 | setSpan('regionName'); |
| 74 | |
| 75 | addCrumbNav(bcs, data.crumbs, data.region); |
| 76 | } |
| 77 | |
| 78 | function addCrumbNav(span, array, id) { |
| 79 | var rev = []; |
| 80 | |
| 81 | span.selectAll('span').remove(); |
| 82 | |
| 83 | array.forEach(function (a) { |
| 84 | rev.unshift(a.id); |
| 85 | }); |
| 86 | |
| 87 | rev.forEach(function (rid, idx) { |
| 88 | if (idx) { |
| 89 | span.append('span').text(' +++ '); |
| 90 | } |
| 91 | if (rid != id) { |
| 92 | addNavigable(span, 'span', rid); |
| 93 | } else { |
| 94 | span.append('span').text(rid); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | function addNavigable(span, what, rid) { |
| 100 | span.append(what).classed('nav-me', true) |
| 101 | .text(rid) |
| 102 | .on('click', function () { navRequest(rid); }); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | function doTmpCurrentRegion(data) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 106 | var span = topdiv.select('.thisRegion').select('span'); |
| 107 | var div; |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 108 | span.text(data.id); |
| 109 | |
| 110 | div = topdiv.select('.subRegions').select('div'); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 111 | rmP(div); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 112 | data.subregions.forEach(function (r) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 113 | addNavigable(div, 'p', r.id); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 114 | }); |
| 115 | |
| 116 | div = topdiv.select('.devices').select('div'); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 117 | rmP(div); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 118 | data.layerOrder.forEach(function (tag, idx) { |
| 119 | var devs = data.devices[idx]; |
| 120 | devs.forEach(function (d) { |
| 121 | div.append('p') |
| 122 | .text('[' + tag + '] ' + d.id); |
| 123 | }); |
| 124 | |
| 125 | }); |
| 126 | |
| 127 | div = topdiv.select('.hosts').select('div'); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 128 | rmP(div); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 129 | data.layerOrder.forEach(function (tag, idx) { |
| 130 | var hosts = data.hosts[idx]; |
| 131 | hosts.forEach(function (h) { |
| 132 | div.append('p') |
| 133 | .text('[' + tag + '] ' + h.id); |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | div = topdiv.select('.links').select('div'); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 138 | rmP(div); |
| 139 | data.links.forEach(function (lnk) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 140 | div.append('p') |
| 141 | .text(lnk.id); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | function doTmpPeerRegions(data) { |
Simon Hunt | e43cccebf | 2016-09-01 17:01:58 -0700 | [diff] [blame] | 146 | var peerDiv = topdiv.select('.peers').select('div'); |
| 147 | rmP(peerDiv); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 148 | |
Simon Hunt | e43cccebf | 2016-09-01 17:01:58 -0700 | [diff] [blame] | 149 | function logPeer(p) { |
| 150 | var o = peerDiv.append('p'), |
| 151 | id = p.id, |
| 152 | nt = p.nodeType; |
| 153 | o.text('[' + nt + '] id = ' + id); |
| 154 | } |
| 155 | |
| 156 | data.peers.forEach(function (p) { |
| 157 | logPeer(p); |
| 158 | }); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // ========================== Event Handlers |
| 162 | |
| 163 | function allInstances(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 164 | $log.debug('>> topo2AllInstances event:', data); |
| 165 | doTmpInstances(data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | function currentLayout(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 169 | $log.debug('>> topo2CurrentLayout event:', data); |
| 170 | doTmpCurrentLayout(data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | function currentRegion(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 174 | $log.debug('>> topo2CurrentRegion event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 175 | doTmpCurrentRegion(data); |
| 176 | } |
| 177 | |
| 178 | function peerRegions(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 179 | $log.debug('>> topo2PeerRegions event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 180 | doTmpPeerRegions(data); |
| 181 | } |
| 182 | |
| 183 | function startDone(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 184 | $log.debug('>> topo2StartDone event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // ========================== Main Service Definition |
| 188 | |
| 189 | angular.module('ovTopoX') |
| 190 | .factory('TopoXForceService', |
| 191 | ['$log', 'WebSocketService', |
| 192 | |
| 193 | function (_$log_, _wss_) { |
| 194 | $log = _$log_; |
| 195 | wss = _wss_; |
| 196 | |
| 197 | return { |
| 198 | init: init, |
| 199 | destroy: destroy, |
| 200 | topo2AllInstances: allInstances, |
| 201 | topo2CurrentLayout: currentLayout, |
| 202 | topo2CurrentRegion: currentRegion, |
| 203 | topo2PeerRegions: peerRegions, |
| 204 | topo2StartDone: startDone |
| 205 | }; |
| 206 | }]); |
| 207 | }()); |