blob: de7c09e37357c6fc0af39ecdac1ca41d05086140 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001(function () {
2 'use strict';
3
4 // injected refs
Steven Burrowsc515e602017-04-13 11:17:40 -07005 var $log, ps, sus, gs, flash, ts, t2mss;
Steven Burrows57e24e92016-08-04 18:38:24 +01006
Steven Burrows57e24e92016-08-04 18:38:24 +01007 // configuration
8 var showLogicErrors = true,
Steven Burrows7a9d04e2016-09-26 17:05:37 -07009 idIns = 'topo2-p-instance',
Steven Burrows57e24e92016-08-04 18:38:24 +010010 instOpts = {
11 edge: 'left',
Steven Burrows1c2a9682017-07-14 16:52:46 +010012 width: 20,
Steven Burrows57e24e92016-08-04 18:38:24 +010013 };
14
15 // internal state
16 var onosInstances,
17 onosOrder,
Steven Burrows57e24e92016-08-04 18:38:24 +010018 oiBox;
19
Steven Burrows57e24e92016-08-04 18:38:24 +010020 function addInstance(data) {
21 var id = data.id;
22
23 if (onosInstances[id]) {
24 updateInstance(data);
25 return;
26 }
27 onosInstances[id] = data;
28 onosOrder.push(data);
29 updateInstances();
30 }
31
32 function updateInstance(data) {
33 var id = data.id,
34 d = onosInstances[id];
35 if (d) {
36 angular.extend(d, data);
37 updateInstances();
38 } else {
39 logicError('updateInstance: lookup fail: ID = "' + id + '"');
40 }
41 }
42
Steven Burrows57e24e92016-08-04 18:38:24 +010043 // ==========================
44
45 function clickInst(d) {
46 var el = d3.select(this),
47 aff = el.classed('affinity');
Steven Burrowsdfa52b02016-09-02 13:50:43 +010048 if (aff) {
Steven Burrows57e24e92016-08-04 18:38:24 +010049 cancelAffinity();
Steven Burrowsdfa52b02016-09-02 13:50:43 +010050 } else {
51 setAffinity(el, d);
Steven Burrows57e24e92016-08-04 18:38:24 +010052 }
53 }
54
55 function setAffinity(el, d) {
56 d3.selectAll('.onosInst')
57 .classed('mastership', true)
58 .classed('affinity', false);
59 el.classed('affinity', true);
60
Steven Burrowsc515e602017-04-13 11:17:40 -070061 t2mss.setMastership(d.id);
Steven Burrows57e24e92016-08-04 18:38:24 +010062 }
63
64 function cancelAffinity() {
65 d3.selectAll('.onosInst')
66 .classed('mastership affinity', false);
67
Steven Burrowsc515e602017-04-13 11:17:40 -070068 t2mss.setMastership(null);
Steven Burrows57e24e92016-08-04 18:38:24 +010069 }
70
71 function attachUiBadge(svg) {
72 gs.addGlyph(svg, 'uiAttached', 24, true, [14, 54])
73 .classed('badgeIcon uiBadge', true);
74 }
75
76 function attachReadyBadge(svg) {
77 gs.addGlyph(svg, 'checkMark', 16, true, [18, 40])
78 .classed('badgeIcon readyBadge', true);
79 }
80
81 function instColor(id, online) {
82 return sus.cat7().getColor(id, !online, ts.theme());
83 }
84
85 // ==============================
86
87 function updateInstances() {
88 var rox = 5,
89 roy = 5,
90 rw = 160,
91 rhh = 30,
92 rbh = 45,
93 tx = 48,
94 instSvg = {
95 width: 170,
96 height: 85,
Steven Burrows1c2a9682017-07-14 16:52:46 +010097 viewBox: '0 0 170 85',
Steven Burrows57e24e92016-08-04 18:38:24 +010098 },
99 headRect = {
100 x: rox,
101 y: roy,
102 width: rw,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100103 height: rhh,
Steven Burrows57e24e92016-08-04 18:38:24 +0100104 },
105 bodyRect = {
106 x: rox,
107 y: roy + rhh,
108 width: rw,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100109 height: rbh,
Steven Burrows57e24e92016-08-04 18:38:24 +0100110 },
111 titleAttr = {
112 class: 'instTitle',
113 x: tx,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100114 y: 27,
Steven Burrows57e24e92016-08-04 18:38:24 +0100115 };
116
117 var onoses = oiBox.el().selectAll('.onosInst')
118 .data(onosOrder, function (d) { return d.id; });
119
120 function nSw(n) {
121 return 'Devices: ' + n;
122 }
123
124 // operate on existing onos instances if necessary
125 onoses.each(function (d) {
126 var el = d3.select(this),
127 svg = el.select('svg');
128
129 // update online state
130 el.classed('online', d.online);
131 el.classed('ready', d.ready);
132
133 // update ui-attached state
134 svg.select('use.uiBadge').remove();
135 if (d.uiAttached) {
136 attachUiBadge(svg);
137 }
138
139 function updAttr(id, value) {
140 svg.select('text.instLabel.' + id).text(value);
141 }
142
143 updAttr('ip', d.ip);
144 updAttr('ns', nSw(d.switches));
145 });
146
Steven Burrows57e24e92016-08-04 18:38:24 +0100147 // operate on new onos instances
148 var entering = onoses.enter()
149 .append('div')
150 .classed('onosInst', true)
151 .classed('online', function (d) { return d.online; })
152 .classed('ready', function (d) { return d.ready; })
153 .on('click', clickInst);
154
155 entering.each(function (d) {
156 var el = d3.select(this),
157 svg = el.append('svg').attr(instSvg);
158
159 svg.append('rect').attr(headRect);
160 svg.append('rect').attr(bodyRect);
161
162 gs.addGlyph(svg, 'bird', 20, false, [15, 10])
163 .classed('badgeIcon bird', true);
164
165 attachReadyBadge(svg);
166
167 if (d.uiAttached) {
168 attachUiBadge(svg);
169 }
170
171 svg.append('text')
172 .attr(titleAttr)
173 .text(d.id);
174
175 var ty = 55;
176 function addAttr(id, label) {
177 svg.append('text').attr({
178 class: 'instLabel ' + id,
179 x: tx,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100180 y: ty,
Steven Burrows57e24e92016-08-04 18:38:24 +0100181 }).text(label);
182 ty += 18;
183 }
184
185 addAttr('ip', d.ip);
186 addAttr('ns', nSw(d.switches));
187 });
188
189 // operate on existing + new onoses here
190 // set the affinity colors...
191 onoses.each(function (d) {
192
193 var el = d3.select(this),
194 rect = el.select('svg').select('rect'),
195 col = instColor(d.id, d.online);
196
197 rect.style('fill', col);
198 });
199
200 // adjust the panel size appropriately...
201 oiBox.width(instSvg.width * onosOrder.length);
202 oiBox.height(instSvg.height);
203
204 // remove any outgoing instances
205 onoses.exit().remove();
206 }
207
Steven Burrows57e24e92016-08-04 18:38:24 +0100208 // ==========================
Steven Burrows57e24e92016-08-04 18:38:24 +0100209 function logicError(msg) {
210 if (showLogicErrors) {
Simon Hunt95f4b422017-03-03 13:49:05 -0800211 $log.warn('Topo2InstService: ' + msg);
Steven Burrows57e24e92016-08-04 18:38:24 +0100212 }
213 }
214
Steven Burrows1c2a9682017-07-14 16:52:46 +0100215 function initInst() {
Steven Burrows57e24e92016-08-04 18:38:24 +0100216 oiBox = ps.createPanel(idIns, instOpts);
217 oiBox.show();
218
219 onosInstances = {};
220 onosOrder = [];
Steven Burrows57e24e92016-08-04 18:38:24 +0100221
222 // we want to update the instances, each time the theme changes
223 ts.addListener(updateInstances);
224 }
225
Steven Burrows57e24e92016-08-04 18:38:24 +0100226 function allInstances(data) {
227 $log.debug('Update all instances', data);
228
229 var members = data.members;
230
231 members.forEach(function (member) {
232 addInstance(member);
233 });
234 }
235
Steven Burrows01ddf602016-09-28 14:21:00 -0700236 function toggle(x) {
237 var kev = (x === 'keyev'),
238 on,
239 verb;
240
241 if (kev) {
242 on = oiBox.toggle();
243 } else {
244 on = Boolean(x);
245 if (on) {
246 oiBox.show();
247 } else {
248 oiBox.hide();
249 }
250 }
251 verb = on ? 'Show' : 'Hide';
252 flash.flash(verb + ' instances panel');
253 return on;
254 }
255
Steven Burrows7a9d04e2016-09-26 17:05:37 -0700256 function destroy() {
257 ts.removeListener(updateInstances);
258
259 ps.destroyPanel(idIns);
260 oiBox = null;
261
262 onosInstances = {};
263 onosOrder = [];
264 }
265
Steven Burrows57e24e92016-08-04 18:38:24 +0100266 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000267 .factory('Topo2InstanceService', [
268 '$log', 'PanelService', 'SvgUtilService', 'GlyphService',
Steven Burrowsc515e602017-04-13 11:17:40 -0700269 'FlashService', 'ThemeService', 'Topo2MastershipService',
Steven Burrows57e24e92016-08-04 18:38:24 +0100270
Steven Burrowsc515e602017-04-13 11:17:40 -0700271 function (_$log_, _ps_, _sus_, _gs_, _flash_, _ts_, _t2mss_) {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000272 $log = _$log_;
273 ps = _ps_;
274 sus = _sus_;
275 gs = _gs_;
276 flash = _flash_;
277 ts = _ts_;
Steven Burrowsc515e602017-04-13 11:17:40 -0700278 t2mss = _t2mss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100279
Steven Burrowsaf96a212016-12-28 12:57:02 +0000280 return {
281 initInst: initInst,
282 allInstances: allInstances,
283 destroy: destroy,
284 toggle: toggle,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100285 isVisible: function () { return oiBox.isVisible(); },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000286 };
Steven Burrows1c2a9682017-07-14 16:52:46 +0100287 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000288 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100289
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100290})();