blob: 9c4ccc2b702f20e5e1975a5e48f1e6ee3bad71f0 [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 Burrowsdfa52b02016-09-02 13:50:43 +010025 var Collection, Model, is, sus, ts;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010026
27 var remappedDeviceTypes = {
28 virtual: 'cord'
29 };
30
31 // configuration
32 var devIconDim = 36,
Steven Burrowsdfa52b02016-09-02 13:50:43 +010033 halfDevIcon = devIconDim / 2;
Steven Burrows57e24e92016-08-04 18:38:24 +010034
35 function createDeviceCollection(data, region) {
36
37 var DeviceCollection = Collection.extend({
38 model: Model,
Steven Burrowsdfa52b02016-09-02 13:50:43 +010039 comparator: function (a, b) {
40 var order = region.get('layerOrder'),
41 aLayer = a.get('layer'),
42 bLayer = b.get('layer');
43 return order.indexOf(aLayer - order.indexOf(bLayer));
Steven Burrows57e24e92016-08-04 18:38:24 +010044 }
45 });
46
47 var devices = [];
48 data.forEach(function (deviceLayer) {
49 deviceLayer.forEach(function (device) {
50 devices.push(device);
51 });
52 });
53
54 var deviceCollection = new DeviceCollection(devices);
55 deviceCollection.sort();
56
57 return deviceCollection;
58 }
59
Steven Burrowsec1f45c2016-08-08 16:14:41 +010060 function mapDeviceTypeToGlyph(type) {
61 return remappedDeviceTypes[type] || type || 'unknown';
62 }
63
Steven Burrowsec1f45c2016-08-08 16:14:41 +010064 function iconBox(dim, labelWidth) {
65 return {
66 x: -dim / 2,
67 y: -dim / 2,
68 width: dim + labelWidth,
69 height: dim
Steven Burrowsdfa52b02016-09-02 13:50:43 +010070 };
Steven Burrowsec1f45c2016-08-08 16:14:41 +010071 }
72
Steven Burrows9edc7e02016-08-29 11:52:07 +010073 // note: these are the device icon colors without affinity (no master)
74 var dColTheme = {
75 light: {
76 online: '#444444',
77 offline: '#cccccc'
78 },
79 dark: {
80 // TODO: theme
81 online: '#444444',
82 offline: '#cccccc'
83 }
84 };
Steven Burrowsec1f45c2016-08-08 16:14:41 +010085
Steven Burrows9edc7e02016-08-29 11:52:07 +010086 function deviceGlyphColor(d) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010087 var o = this.node.online,
Steven Burrows6deb4ce2016-08-26 16:06:23 +010088 id = this.node.master, // TODO: This should be from node.master
Steven Burrowsec1f45c2016-08-08 16:14:41 +010089 otag = o ? 'online' : 'offline';
Steven Burrowsdfa52b02016-09-02 13:50:43 +010090
91 return o ? sus.cat7().getColor(id, 0, ts.theme()) :
92 dColTheme[ts.theme()][otag];
Steven Burrowsec1f45c2016-08-08 16:14:41 +010093 }
94
95 function setDeviceColor() {
96 this.el.select('use')
97 .style('fill', this.deviceGlyphColor());
98 }
99
Steven Burrows57e24e92016-08-04 18:38:24 +0100100 angular.module('ovTopo2')
101 .factory('Topo2DeviceService',
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100102 ['Topo2Collection', 'Topo2NodeModel', 'IconService',
103 'SvgUtilService', 'ThemeService',
Steven Burrows57e24e92016-08-04 18:38:24 +0100104
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100105 function (_c_, _nm_, _is_, _sus_, _ts_, classnames) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100106
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100107 is = _is_;
108 sus = _sus_;
109 ts = _ts_;
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100110 Collection = _c_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100111
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100112 Model = _nm_.extend({
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100113 initialize: function () {
114 this.set('weight', 0);
115 this.constructor.__super__.initialize.apply(this, arguments);
116 },
117 nodeType: 'device',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100118 deviceGlyphColor: deviceGlyphColor,
119 mapDeviceTypeToGlyph: mapDeviceTypeToGlyph,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100120 setDeviceColor: setDeviceColor,
121 onEnter: function (el) {
122
123 var node = d3.select(el),
124 glyphId = mapDeviceTypeToGlyph(this.get('type')),
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100125 label = this.trimLabel(this.label()),
126 glyph, labelWidth;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100127
128 this.el = node;
129
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100130 // Label
131 var labelElements = this.addLabelElements(label);
132 labelWidth = label ? this.computeLabelWidth(node) : 0;
133 labelElements.rect.attr(iconBox(devIconDim, labelWidth));
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100134
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100135 // Icon
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100136 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100137 glyph.attr(iconBox(devIconDim, 0));
138
139 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
140 this.render();
141 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100142 onExit: function () {
143 var node = this.el;
144 node.select('use')
145 .style('opacity', 0.5)
146 .transition()
147 .duration(800)
148 .style('opacity', 0);
149
150 node.selectAll('rect')
151 .style('stroke-fill', '#555')
152 .style('fill', '#888')
153 .style('opacity', 0.5);
154 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100155 render: function () {
156 this.setDeviceColor();
157 }
158 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100159
160 return {
161 createDeviceCollection: createDeviceCollection
162 };
163 }
164 ]);
165
166})();