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) { |
| 146 | |
| 147 | } |
| 148 | |
| 149 | // ========================== Event Handlers |
| 150 | |
| 151 | function allInstances(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 152 | $log.debug('>> topo2AllInstances event:', data); |
| 153 | doTmpInstances(data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | function currentLayout(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 157 | $log.debug('>> topo2CurrentLayout event:', data); |
| 158 | doTmpCurrentLayout(data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | function currentRegion(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 162 | $log.debug('>> topo2CurrentRegion event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 163 | doTmpCurrentRegion(data); |
| 164 | } |
| 165 | |
| 166 | function peerRegions(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 167 | $log.debug('>> topo2PeerRegions event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 168 | doTmpPeerRegions(data); |
| 169 | } |
| 170 | |
| 171 | function startDone(data) { |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 172 | $log.debug('>> topo2StartDone event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // ========================== Main Service Definition |
| 176 | |
| 177 | angular.module('ovTopoX') |
| 178 | .factory('TopoXForceService', |
| 179 | ['$log', 'WebSocketService', |
| 180 | |
| 181 | function (_$log_, _wss_) { |
| 182 | $log = _$log_; |
| 183 | wss = _wss_; |
| 184 | |
| 185 | return { |
| 186 | init: init, |
| 187 | destroy: destroy, |
| 188 | topo2AllInstances: allInstances, |
| 189 | topo2CurrentLayout: currentLayout, |
| 190 | topo2CurrentRegion: currentRegion, |
| 191 | topo2PeerRegions: peerRegions, |
| 192 | topo2StartDone: startDone |
| 193 | }; |
| 194 | }]); |
| 195 | }()); |