blob: c8478cf1f1b994feace4c3a6d69e7b64cceef984 [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,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010033 hostRadius = 14,
34 badgeConfig = {
35 radius: 12,
36 yoff: 5,
37 gdelta: 10
38 },
39 halfDevIcon = devIconDim / 2,
40 devBadgeOff = { dx: -halfDevIcon, dy: -halfDevIcon },
41 hostBadgeOff = { dx: -hostRadius, dy: -hostRadius },
42 status = {
43 i: 'badgeInfo',
44 w: 'badgeWarn',
45 e: 'badgeError'
Steven Burrows6deb4ce2016-08-26 16:06:23 +010046 };
Steven Burrows57e24e92016-08-04 18:38:24 +010047
48 function createDeviceCollection(data, region) {
49
50 var DeviceCollection = Collection.extend({
51 model: Model,
Steven Burrows57e24e92016-08-04 18:38:24 +010052 comparator: function(a, b) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010053 var order = region.get('layerOrder');
Steven Burrows57e24e92016-08-04 18:38:24 +010054 return order.indexOf(a.get('layer')) - order.indexOf(b.get('layer'));
55 }
56 });
57
58 var devices = [];
59 data.forEach(function (deviceLayer) {
60 deviceLayer.forEach(function (device) {
61 devices.push(device);
62 });
63 });
64
65 var deviceCollection = new DeviceCollection(devices);
66 deviceCollection.sort();
67
68 return deviceCollection;
69 }
70
Steven Burrowsec1f45c2016-08-08 16:14:41 +010071 function mapDeviceTypeToGlyph(type) {
72 return remappedDeviceTypes[type] || type || 'unknown';
73 }
74
Steven Burrowsec1f45c2016-08-08 16:14:41 +010075 function iconBox(dim, labelWidth) {
76 return {
77 x: -dim / 2,
78 y: -dim / 2,
79 width: dim + labelWidth,
80 height: dim
81 }
82 }
83
84 function deviceGlyphColor(d) {
85
86 var o = this.node.online,
Steven Burrows6deb4ce2016-08-26 16:06:23 +010087 id = this.node.master, // TODO: This should be from node.master
Steven Burrowsec1f45c2016-08-08 16:14:41 +010088 otag = o ? 'online' : 'offline';
89 return o ? sus.cat7().getColor(id, 0, ts.theme())
Steven Burrowsaf3159d2016-08-25 14:54:30 +010090 : '#ff0000';
Steven Burrowsec1f45c2016-08-08 16:14:41 +010091 }
92
93 function setDeviceColor() {
94 this.el.select('use')
95 .style('fill', this.deviceGlyphColor());
96 }
97
Steven Burrows57e24e92016-08-04 18:38:24 +010098 angular.module('ovTopo2')
99 .factory('Topo2DeviceService',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100100 ['Topo2Collection', 'Topo2NodeModel', 'IconService', 'SvgUtilService',
101 'ThemeService', 'Topo2ViewService',
Steven Burrows57e24e92016-08-04 18:38:24 +0100102
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100103 function (_Collection_, _NodeModel_, _is_, _sus_, _ts_, classnames, _t2vs_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100104
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100105 t2vs = _t2vs_;
106 is = _is_;
107 sus = _sus_;
108 ts = _ts_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100109 Collection = _Collection_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100110
111 Model = _NodeModel_.extend({
112 initialize: function () {
113 this.set('weight', 0);
114 this.constructor.__super__.initialize.apply(this, arguments);
115 },
116 nodeType: 'device',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100117 deviceGlyphColor: deviceGlyphColor,
118 mapDeviceTypeToGlyph: mapDeviceTypeToGlyph,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100119 setDeviceColor: setDeviceColor,
120 onEnter: function (el) {
121
122 var node = d3.select(el),
123 glyphId = mapDeviceTypeToGlyph(this.get('type')),
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100124 label = this.trimLabel(this.label()),
125 glyph, labelWidth;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100126
127 this.el = node;
128
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100129 // Label
130 var labelElements = this.addLabelElements(label);
131 labelWidth = label ? this.computeLabelWidth(node) : 0;
132 labelElements.rect.attr(iconBox(devIconDim, labelWidth));
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100133
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100134 // Icon
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100135 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100136 glyph.attr(iconBox(devIconDim, 0));
137
138 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
139 this.render();
140 },
141 onExit: function () {},
142 render: function () {
143 this.setDeviceColor();
144 }
145 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100146
147 return {
148 createDeviceCollection: createDeviceCollection
149 };
150 }
151 ]);
152
153})();