blob: ac3e24496ee1357a8e40337f19157d8a9712986c [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 Burrows9edc7e02016-08-29 11:52:07 +010064 // note: these are the device icon colors without affinity (no master)
65 var dColTheme = {
66 light: {
67 online: '#444444',
68 offline: '#cccccc'
69 },
70 dark: {
71 // TODO: theme
72 online: '#444444',
73 offline: '#cccccc'
74 }
75 };
Steven Burrowsec1f45c2016-08-08 16:14:41 +010076
Steven Burrows9edc7e02016-08-29 11:52:07 +010077 function deviceGlyphColor(d) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010078 var o = this.node.online,
Steven Burrows6deb4ce2016-08-26 16:06:23 +010079 id = this.node.master, // TODO: This should be from node.master
Steven Burrowsec1f45c2016-08-08 16:14:41 +010080 otag = o ? 'online' : 'offline';
Steven Burrowsdfa52b02016-09-02 13:50:43 +010081
82 return o ? sus.cat7().getColor(id, 0, ts.theme()) :
83 dColTheme[ts.theme()][otag];
Steven Burrowsec1f45c2016-08-08 16:14:41 +010084 }
85
86 function setDeviceColor() {
87 this.el.select('use')
88 .style('fill', this.deviceGlyphColor());
89 }
90
Steven Burrows57e24e92016-08-04 18:38:24 +010091 angular.module('ovTopo2')
92 .factory('Topo2DeviceService',
Steven Burrowsdfa52b02016-09-02 13:50:43 +010093 ['Topo2Collection', 'Topo2NodeModel', 'IconService',
94 'SvgUtilService', 'ThemeService',
Steven Burrows57e24e92016-08-04 18:38:24 +010095
Steven Burrowsdfa52b02016-09-02 13:50:43 +010096 function (_c_, _nm_, _is_, _sus_, _ts_, classnames) {
Steven Burrows57e24e92016-08-04 18:38:24 +010097
Steven Burrowsec1f45c2016-08-08 16:14:41 +010098 is = _is_;
99 sus = _sus_;
100 ts = _ts_;
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100101 Collection = _c_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100102
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100103 Model = _nm_.extend({
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100104 initialize: function () {
105 this.set('weight', 0);
106 this.constructor.__super__.initialize.apply(this, arguments);
107 },
108 nodeType: 'device',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100109 deviceGlyphColor: deviceGlyphColor,
110 mapDeviceTypeToGlyph: mapDeviceTypeToGlyph,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100111 setDeviceColor: setDeviceColor,
112 onEnter: function (el) {
113
114 var node = d3.select(el),
115 glyphId = mapDeviceTypeToGlyph(this.get('type')),
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100116 label = this.trimLabel(this.label()),
117 glyph, labelWidth;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100118
119 this.el = node;
120
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100121 // Label
122 var labelElements = this.addLabelElements(label);
123 labelWidth = label ? this.computeLabelWidth(node) : 0;
Steven Burrows37549ee2016-09-21 14:41:39 +0100124 labelElements.rect.attr(this.iconBox(devIconDim, labelWidth));
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100125
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100126 // Icon
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100127 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
Steven Burrows37549ee2016-09-21 14:41:39 +0100128 glyph.attr(this.iconBox(devIconDim, 0));
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100129
130 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
131 this.render();
132 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100133 onExit: function () {
134 var node = this.el;
135 node.select('use')
136 .style('opacity', 0.5)
137 .transition()
138 .duration(800)
139 .style('opacity', 0);
140
141 node.selectAll('rect')
142 .style('stroke-fill', '#555')
143 .style('fill', '#888')
144 .style('opacity', 0.5);
145 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100146 render: function () {
147 this.setDeviceColor();
148 }
149 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100150
151 return {
152 createDeviceCollection: createDeviceCollection
153 };
154 }
155 ]);
156
157})();