Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
| 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 25 | var Collection, Model, is, sus, ts, t2vs; |
| 26 | |
| 27 | var remappedDeviceTypes = { |
| 28 | virtual: 'cord' |
| 29 | }; |
| 30 | |
| 31 | // configuration |
| 32 | var devIconDim = 36, |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame^] | 33 | labelPad = 10, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 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' |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame^] | 47 | }, |
| 48 | deviceLabelIndex = 0; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 49 | |
| 50 | function createDeviceCollection(data, region) { |
| 51 | |
| 52 | var DeviceCollection = Collection.extend({ |
| 53 | model: Model, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 54 | comparator: function(a, b) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 55 | var order = region.get('layerOrder'); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 56 | 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 73 | function mapDeviceTypeToGlyph(type) { |
| 74 | return remappedDeviceTypes[type] || type || 'unknown'; |
| 75 | } |
| 76 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 77 | 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 Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame^] | 86 | // 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 98 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame^] | 99 | function deviceGlyphColor(d) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 100 | var o = this.node.online, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 101 | id = this.node.master, // TODO: This should be from node.master |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 102 | otag = o ? 'online' : 'offline'; |
| 103 | return o ? sus.cat7().getColor(id, 0, ts.theme()) |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame^] | 104 | : dColTheme[ts.theme()][otag]; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | function setDeviceColor() { |
| 108 | this.el.select('use') |
| 109 | .style('fill', this.deviceGlyphColor()); |
| 110 | } |
| 111 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 112 | angular.module('ovTopo2') |
| 113 | .factory('Topo2DeviceService', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 114 | ['Topo2Collection', 'Topo2NodeModel', 'IconService', 'SvgUtilService', |
| 115 | 'ThemeService', 'Topo2ViewService', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 116 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 117 | function (_Collection_, _NodeModel_, _is_, _sus_, _ts_, classnames, _t2vs_) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 118 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 119 | t2vs = _t2vs_; |
| 120 | is = _is_; |
| 121 | sus = _sus_; |
| 122 | ts = _ts_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 123 | Collection = _Collection_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 124 | |
| 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 131 | deviceGlyphColor: deviceGlyphColor, |
| 132 | mapDeviceTypeToGlyph: mapDeviceTypeToGlyph, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 133 | setDeviceColor: setDeviceColor, |
| 134 | onEnter: function (el) { |
| 135 | |
| 136 | var node = d3.select(el), |
| 137 | glyphId = mapDeviceTypeToGlyph(this.get('type')), |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 138 | label = this.trimLabel(this.label()), |
| 139 | glyph, labelWidth; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 140 | |
| 141 | this.el = node; |
| 142 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 143 | // Label |
| 144 | var labelElements = this.addLabelElements(label); |
| 145 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 146 | labelElements.rect.attr(iconBox(devIconDim, labelWidth)); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 147 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 148 | // Icon |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 149 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 150 | 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 Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 160 | |
| 161 | return { |
| 162 | createDeviceCollection: createDeviceCollection |
| 163 | }; |
| 164 | } |
| 165 | ]); |
| 166 | |
| 167 | })(); |