blob: 3f515095fa6fc467b8f5901c375a18470cb0b49e [file] [log] [blame]
Simon Hunt98189192016-07-29 19:02:27 -07001/*
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 Hunt377f5d22016-09-01 16:27:21 -070028 // selected DOM refs
29 var topdiv;
30
Simon Hunt98189192016-07-29 19:02:27 -070031 // ========================== Helper Functions
32
33 function init() {
Simon Hunt377f5d22016-09-01 16:27:21 -070034 topdiv = d3.select('#topoXtmp');
Simon Hunt98189192016-07-29 19:02:27 -070035 $log.debug('Initialize topo force layout');
36 }
37
38 function destroy() {
39 $log.debug('Destroy topo force layout');
40 }
41
Simon Hunt377f5d22016-09-01 16:27:21 -070042 function rmP(div) {
43 div.selectAll('p').remove();
44 }
Simon Hunt98189192016-07-29 19:02:27 -070045
Simon Hunt377f5d22016-09-01 16:27:21 -070046 function navRequest(rid) {
Simon Hunt98189192016-07-29 19:02:27 -070047 wss.sendEvent('topo2navRegion', {
Simon Hunt98189192016-07-29 19:02:27 -070048 rid: rid
49 });
50 }
51
Simon Hunt377f5d22016-09-01 16:27:21 -070052 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 Hunt98189192016-07-29 19:02:27 -070059 function doTmpCurrentLayout(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -070060 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 Hunt98189192016-07-29 19:02:27 -0700103 }
104
105 function doTmpCurrentRegion(data) {
Simon Hunt98189192016-07-29 19:02:27 -0700106 var span = topdiv.select('.thisRegion').select('span');
107 var div;
Simon Hunt98189192016-07-29 19:02:27 -0700108 span.text(data.id);
109
110 div = topdiv.select('.subRegions').select('div');
Simon Hunt377f5d22016-09-01 16:27:21 -0700111 rmP(div);
Simon Hunt98189192016-07-29 19:02:27 -0700112 data.subregions.forEach(function (r) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700113 addNavigable(div, 'p', r.id);
Simon Hunt98189192016-07-29 19:02:27 -0700114 });
115
116 div = topdiv.select('.devices').select('div');
Simon Hunt377f5d22016-09-01 16:27:21 -0700117 rmP(div);
Simon Hunt98189192016-07-29 19:02:27 -0700118 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 Hunt377f5d22016-09-01 16:27:21 -0700128 rmP(div);
Simon Hunt98189192016-07-29 19:02:27 -0700129 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 Hunt377f5d22016-09-01 16:27:21 -0700138 rmP(div);
139 data.links.forEach(function (lnk) {
Simon Hunt98189192016-07-29 19:02:27 -0700140 div.append('p')
141 .text(lnk.id);
142 });
143 }
144
145 function doTmpPeerRegions(data) {
Simon Hunte43cccebf2016-09-01 17:01:58 -0700146 var peerDiv = topdiv.select('.peers').select('div');
147 rmP(peerDiv);
Simon Hunt98189192016-07-29 19:02:27 -0700148
Simon Hunte43cccebf2016-09-01 17:01:58 -0700149 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 Hunt98189192016-07-29 19:02:27 -0700159 }
160
161 // ========================== Event Handlers
162
163 function allInstances(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700164 $log.debug('>> topo2AllInstances event:', data);
165 doTmpInstances(data);
Simon Hunt98189192016-07-29 19:02:27 -0700166 }
167
168 function currentLayout(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700169 $log.debug('>> topo2CurrentLayout event:', data);
170 doTmpCurrentLayout(data);
Simon Hunt98189192016-07-29 19:02:27 -0700171 }
172
173 function currentRegion(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700174 $log.debug('>> topo2CurrentRegion event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700175 doTmpCurrentRegion(data);
176 }
177
178 function peerRegions(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700179 $log.debug('>> topo2PeerRegions event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700180 doTmpPeerRegions(data);
181 }
182
183 function startDone(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700184 $log.debug('>> topo2StartDone event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700185 }
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}());