blob: b2e5e875d49abc3cb1cd42b104458a813cfbc831 [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) {
146
147 }
148
149 // ========================== Event Handlers
150
151 function allInstances(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700152 $log.debug('>> topo2AllInstances event:', data);
153 doTmpInstances(data);
Simon Hunt98189192016-07-29 19:02:27 -0700154 }
155
156 function currentLayout(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700157 $log.debug('>> topo2CurrentLayout event:', data);
158 doTmpCurrentLayout(data);
Simon Hunt98189192016-07-29 19:02:27 -0700159 }
160
161 function currentRegion(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700162 $log.debug('>> topo2CurrentRegion event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700163 doTmpCurrentRegion(data);
164 }
165
166 function peerRegions(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700167 $log.debug('>> topo2PeerRegions event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700168 doTmpPeerRegions(data);
169 }
170
171 function startDone(data) {
Simon Hunt377f5d22016-09-01 16:27:21 -0700172 $log.debug('>> topo2StartDone event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700173 }
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}());