blob: 5a4b52862eefcd9c6766d7d73bcfc6b1dd319fcf [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
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 Devices Module.
19 Module that holds the devices for a region
20 */
21
22(function () {
23 'use strict';
24
Steven Burrowsec1f45c2016-08-08 16:14:41 +010025 var Collection, Model, is, sus, ts, t2vs;
26
27 var remappedDeviceTypes = {
28 virtual: 'cord'
29 };
30
31 // configuration
32 var devIconDim = 36,
33 labelPad = 10,
34 hostRadius = 14,
35 badgeConfig = {
36 radius: 12,
37 yoff: 5,
38 gdelta: 10
39 },
40 halfDevIcon = devIconDim / 2,
41 devBadgeOff = { dx: -halfDevIcon, dy: -halfDevIcon },
42 hostBadgeOff = { dx: -hostRadius, dy: -hostRadius },
43 status = {
44 i: 'badgeInfo',
45 w: 'badgeWarn',
46 e: 'badgeError'
47 },
48 deviceLabelIndex = 0;
Steven Burrows57e24e92016-08-04 18:38:24 +010049
50 function createDeviceCollection(data, region) {
51
52 var DeviceCollection = Collection.extend({
53 model: Model,
Steven Burrows57e24e92016-08-04 18:38:24 +010054 comparator: function(a, b) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010055 var order = region.get('layerOrder');
Steven Burrows57e24e92016-08-04 18:38:24 +010056 return order.indexOf(a.get('layer')) - order.indexOf(b.get('layer'));
57 }
58 });
59
60 var devices = [];
61 data.forEach(function (deviceLayer) {
62 deviceLayer.forEach(function (device) {
63 devices.push(device);
64 });
65 });
66
67 var deviceCollection = new DeviceCollection(devices);
68 deviceCollection.sort();
69
70 return deviceCollection;
71 }
72
Steven Burrowsec1f45c2016-08-08 16:14:41 +010073 function mapDeviceTypeToGlyph(type) {
74 return remappedDeviceTypes[type] || type || 'unknown';
75 }
76
77 function deviceLabel(d) {
78 //TODO: Device Json is missing labels array
79 return "";
80 var labels = this.get('labels'),
81 idx = (deviceLabelIndex < labels.length) ? deviceLabelIndex : 0;
82 return labels[idx];
83 }
84
85 function trimLabel(label) {
86 return (label && label.trim()) || '';
87 }
88
89 function computeLabelWidth() {
90 var text = this.select('text'),
91 box = text.node().getBBox();
92 return box.width + labelPad * 2;
93 }
94
95 function iconBox(dim, labelWidth) {
96 return {
97 x: -dim / 2,
98 y: -dim / 2,
99 width: dim + labelWidth,
100 height: dim
101 }
102 }
103
104 function deviceGlyphColor(d) {
105
106 var o = this.node.online,
107 id = "127.0.0.1", // TODO: This should be from node.master
108 otag = o ? 'online' : 'offline';
109 return o ? sus.cat7().getColor(id, 0, ts.theme())
Steven Burrowsaf3159d2016-08-25 14:54:30 +0100110 : '#ff0000';
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100111 }
112
113 function setDeviceColor() {
114 this.el.select('use')
115 .style('fill', this.deviceGlyphColor());
116 }
117
Steven Burrows57e24e92016-08-04 18:38:24 +0100118 angular.module('ovTopo2')
119 .factory('Topo2DeviceService',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100120 ['Topo2Collection', 'Topo2NodeModel', 'IconService', 'SvgUtilService',
121 'ThemeService', 'Topo2ViewService',
Steven Burrows57e24e92016-08-04 18:38:24 +0100122
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100123 function (_Collection_, _NodeModel_, _is_, _sus_, _ts_, classnames, _t2vs_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100124
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100125 t2vs = _t2vs_;
126 is = _is_;
127 sus = _sus_;
128 ts = _ts_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100129 Collection = _Collection_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100130
131 Model = _NodeModel_.extend({
132 initialize: function () {
133 this.set('weight', 0);
134 this.constructor.__super__.initialize.apply(this, arguments);
135 },
136 nodeType: 'device',
137 deviceLabel: deviceLabel,
138 deviceGlyphColor: deviceGlyphColor,
139 mapDeviceTypeToGlyph: mapDeviceTypeToGlyph,
140 trimLabel: trimLabel,
141 setDeviceColor: setDeviceColor,
142 onEnter: function (el) {
143
144 var node = d3.select(el),
145 glyphId = mapDeviceTypeToGlyph(this.get('type')),
146 label = trimLabel(this.deviceLabel()),
147 rect, text, glyph, labelWidth;
148
149 this.el = node;
150
151 rect = node.append('rect');
152
153 text = node.append('text').text(label)
154 .attr('text-anchor', 'left')
155 .attr('y', '0.3em')
156 .attr('x', halfDevIcon + labelPad);
157
158 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
159
160 labelWidth = label ? computeLabelWidth(node) : 0;
161
162 rect.attr(iconBox(devIconDim, labelWidth));
163 glyph.attr(iconBox(devIconDim, 0));
164
165 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
166 this.render();
167 },
168 onExit: function () {},
169 render: function () {
170 this.setDeviceColor();
171 }
172 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100173
174 return {
175 createDeviceCollection: createDeviceCollection
176 };
177 }
178 ]);
179
180})();