blob: 6e48b8307f7d1664c676985b016476368ab13244 [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 Burrows9edc7e02016-08-29 11:52:07 +010033 labelPad = 10,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010034 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'
Steven Burrows9edc7e02016-08-29 11:52:07 +010047 },
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
Steven Burrowsec1f45c2016-08-08 16:14:41 +010077 function iconBox(dim, labelWidth) {
78 return {
79 x: -dim / 2,
80 y: -dim / 2,
81 width: dim + labelWidth,
82 height: dim
83 }
84 }
85
Steven Burrows9edc7e02016-08-29 11:52:07 +010086 // note: these are the device icon colors without affinity (no master)
87 var dColTheme = {
88 light: {
89 online: '#444444',
90 offline: '#cccccc'
91 },
92 dark: {
93 // TODO: theme
94 online: '#444444',
95 offline: '#cccccc'
96 }
97 };
Steven Burrowsec1f45c2016-08-08 16:14:41 +010098
Steven Burrows9edc7e02016-08-29 11:52:07 +010099 function deviceGlyphColor(d) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100100 var o = this.node.online,
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100101 id = this.node.master, // TODO: This should be from node.master
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100102 otag = o ? 'online' : 'offline';
103 return o ? sus.cat7().getColor(id, 0, ts.theme())
Steven Burrows9edc7e02016-08-29 11:52:07 +0100104 : dColTheme[ts.theme()][otag];
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100105 }
106
107 function setDeviceColor() {
108 this.el.select('use')
109 .style('fill', this.deviceGlyphColor());
110 }
111
Steven Burrows57e24e92016-08-04 18:38:24 +0100112 angular.module('ovTopo2')
113 .factory('Topo2DeviceService',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100114 ['Topo2Collection', 'Topo2NodeModel', 'IconService', 'SvgUtilService',
115 'ThemeService', 'Topo2ViewService',
Steven Burrows57e24e92016-08-04 18:38:24 +0100116
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100117 function (_Collection_, _NodeModel_, _is_, _sus_, _ts_, classnames, _t2vs_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100118
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100119 t2vs = _t2vs_;
120 is = _is_;
121 sus = _sus_;
122 ts = _ts_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100123 Collection = _Collection_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100124
125 Model = _NodeModel_.extend({
126 initialize: function () {
127 this.set('weight', 0);
128 this.constructor.__super__.initialize.apply(this, arguments);
129 },
130 nodeType: 'device',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100131 deviceGlyphColor: deviceGlyphColor,
132 mapDeviceTypeToGlyph: mapDeviceTypeToGlyph,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100133 setDeviceColor: setDeviceColor,
134 onEnter: function (el) {
135
136 var node = d3.select(el),
137 glyphId = mapDeviceTypeToGlyph(this.get('type')),
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100138 label = this.trimLabel(this.label()),
139 glyph, labelWidth;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100140
141 this.el = node;
142
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100143 // Label
144 var labelElements = this.addLabelElements(label);
145 labelWidth = label ? this.computeLabelWidth(node) : 0;
146 labelElements.rect.attr(iconBox(devIconDim, labelWidth));
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100147
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100148 // Icon
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100149 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100150 glyph.attr(iconBox(devIconDim, 0));
151
152 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
153 this.render();
154 },
155 onExit: function () {},
156 render: function () {
157 this.setDeviceColor();
158 }
159 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100160
161 return {
162 createDeviceCollection: createDeviceCollection
163 };
164 }
165 ]);
166
167})();