blob: c56b400ddf39ca966c585ee952e5a91d1809a909 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Steven Burrows57e24e92016-08-04 18:38:24 +01003 *
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 Hosts Module.
19 Module that holds the hosts for a region
20 */
21
22(function () {
23 'use strict';
24
Steven Burrowsdfa52b02016-09-02 13:50:43 +010025 var Collection, Model;
Steven Burrows57e24e92016-08-04 18:38:24 +010026
Steven Burrowse1ea27b2017-04-12 09:53:41 -070027 var hostIconDim = 15,
28 hostIconDimMin = 8,
29 hostIconDimMax = 15,
Steven Burrows1270c182017-09-14 10:14:58 +010030 labelPadding = 20,
Steven Burrowsbeef9352016-10-21 14:09:50 -050031 remappedDeviceTypes = {};
32
Steven Burrows57e24e92016-08-04 18:38:24 +010033 function createHostCollection(data, region) {
34
35 var HostCollection = Collection.extend({
Steven Burrows1c2a9682017-07-14 16:52:46 +010036 model: Model,
Steven Burrows57e24e92016-08-04 18:38:24 +010037 });
38
39 var hosts = [];
40 data.forEach(function (hostsLayer) {
41 hostsLayer.forEach(function (host) {
42 hosts.push(host);
43 });
44 });
45
46 return new HostCollection(hosts);
47 }
48
49 angular.module('ovTopo2')
Steven Burrowsdfa52b02016-09-02 13:50:43 +010050 .factory('Topo2HostService', [
Steven Burrowsec1f45c2016-08-08 16:14:41 +010051 'Topo2Collection', 'Topo2NodeModel', 'Topo2ViewService',
Steven Burrowsaea509d2017-04-12 14:17:47 -070052 'IconService', 'Topo2ZoomService', 'Topo2HostsPanelService', 'PrefsService',
Steven Burrowscc6f6de2017-08-30 12:50:38 +010053 'Topo2PrefsService',
54 function (_c_, NodeModel, _t2vs_, is, zs, t2hds, ps, t2ps) {
Steven Burrows57e24e92016-08-04 18:38:24 +010055
Steven Burrowsaf96a212016-12-28 12:57:02 +000056 Collection = _c_;
Steven Burrows57e24e92016-08-04 18:38:24 +010057
Steven Burrowsaf96a212016-12-28 12:57:02 +000058 Model = NodeModel.extend({
Steven Burrows5fa057e2017-03-15 17:07:56 +000059
60 nodeType: 'host',
61 events: {
Steven Burrows1c2a9682017-07-14 16:52:46 +010062 'click': 'onClick',
Steven Burrows5fa057e2017-03-15 17:07:56 +000063 },
Steven Burrows86af4352016-11-16 18:19:12 -060064 initialize: function () {
65 this.super = this.constructor.__super__;
66 this.super.initialize.apply(this, arguments);
67 },
Steven Burrows86af4352016-11-16 18:19:12 -060068 onChange: function () {
69 // Update class names when the model changes
70 if (this.el) {
71 this.el.attr('class', this.svgClassName());
72 }
73 },
Steven Burrows1c2a9682017-07-14 16:52:46 +010074 showDetails: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +000075 t2hds.displayPanel(this);
Steven Burrows86af4352016-11-16 18:19:12 -060076 },
Steven Burrowsbeef9352016-10-21 14:09:50 -050077 icon: function () {
78 var type = this.get('type');
Steven Burrows1aa4f582016-12-13 15:05:41 -050079 return remappedDeviceTypes[type] || type || 'm_endstation';
Steven Burrowsbeef9352016-10-21 14:09:50 -050080 },
Steven Burrows22cb5502018-02-06 11:28:33 +000081 title: function () {
82 var props = this.get('props');
83 return props.name || this.get('ips')[0] || 'unknown';
84 },
Steven Burrowscc6f6de2017-08-30 12:50:38 +010085 labelIndex: function () {
86 return t2ps.get('hlbls');
87 },
Steven Burrows583f4be2016-11-04 14:06:50 +010088 label: function () {
Steven Burrowscc6f6de2017-08-30 12:50:38 +010089 var props = this.get('props'),
90 id = this.get('ips')[0] || 'unknown',
91 friendlyName = props && props.name ? props.name : id,
92 labels = ['', friendlyName || id, id, this.get('id')],
93 nli = this.labelIndex(),
94 idx = (nli < labels.length) ? nli : 0;
95
96 return labels[idx];
Steven Burrows583f4be2016-11-04 14:06:50 +010097 },
Steven Burrows1270c182017-09-14 10:14:58 +010098 updateLabel: function () {
99 var node = this.el,
100 label = this.trimLabel(this.label()),
101 labelWidth;
102
103 node.select('text').text(label);
104 labelWidth = !_.isEmpty(this.label()) ? this.computeLabelWidth(node) + (labelPadding * 2) : 0;
105
106 node.select('rect')
107 .transition()
108 .attr({
109 width: labelWidth,
110 });
111 },
Steven Burrowsbeef9352016-10-21 14:09:50 -0500112 setScale: function () {
113
Steven Burrows8909c5c2017-04-10 09:56:52 -0700114 if (!this.el) return;
115
Steven Burrowsbeef9352016-10-21 14:09:50 -0500116 var dim = hostIconDim,
117 multipler = 1;
118
119 if (dim * zs.scale() < hostIconDimMin) {
120 multipler = hostIconDimMin / (dim * zs.scale());
121 } else if (dim * zs.scale() > hostIconDimMax) {
122 multipler = hostIconDimMax / (dim * zs.scale());
123 }
124
125 this.el.select('g').selectAll('*')
126 .style('transform', 'scale(' + multipler + ')');
127 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700128 setVisibility: function () {
129 var visible = ps.getPrefs('topo2_prefs')['hosts'];
130 this.el.style('visibility', visible ? 'visible' : 'hidden');
131 },
Steven Burrowsbeef9352016-10-21 14:09:50 -0500132 onEnter: function (el) {
133 var node = d3.select(el),
134 icon = this.icon(),
Steven Burrows583f4be2016-11-04 14:06:50 +0100135 textDy = 5,
Steven Burrows1270c182017-09-14 10:14:58 +0100136 textDx = (hostIconDim * 2);
Steven Burrowsbeef9352016-10-21 14:09:50 -0500137
138 this.el = node;
139
140 var g = node.append('g')
141 .attr('class', 'svgIcon hostIcon');
142
Steven Burrows1270c182017-09-14 10:14:58 +0100143 // Add Label background to host
144
145 var rect = g.append('rect').attr({
146 width: 0,
147 height: hostIconDim * 2,
148 y: - hostIconDim,
149 });
150
Steven Burrowsbeef9352016-10-21 14:09:50 -0500151 g.append('circle').attr('r', hostIconDim);
Steven Burrowse1ea27b2017-04-12 09:53:41 -0700152
153 var glyphSize = hostIconDim * 1.5;
Steven Burrowsbeef9352016-10-21 14:09:50 -0500154 g.append('use').attr({
155 'xlink:href': '#' + icon,
Steven Burrowse1ea27b2017-04-12 09:53:41 -0700156 width: glyphSize,
157 height: glyphSize,
158 x: -glyphSize / 2,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100159 y: -glyphSize / 2,
Steven Burrowsbeef9352016-10-21 14:09:50 -0500160 });
161
Steven Burrows583f4be2016-11-04 14:06:50 +0100162 var labelText = this.label();
163
Steven Burrowsbeef9352016-10-21 14:09:50 -0500164 g.append('text')
Steven Burrows583f4be2016-11-04 14:06:50 +0100165 .text(labelText)
Steven Burrowsbeef9352016-10-21 14:09:50 -0500166 .attr('dy', textDy)
Steven Burrows583f4be2016-11-04 14:06:50 +0100167 .attr('dx', textDx)
Steven Burrows1270c182017-09-14 10:14:58 +0100168 .attr('text-anchor', 'left');
Steven Burrowsbeef9352016-10-21 14:09:50 -0500169
170 this.setScale();
Steven Burrows86af4352016-11-16 18:19:12 -0600171 this.setUpEvents();
Steven Burrowsaea509d2017-04-12 14:17:47 -0700172 this.setVisibility();
Steven Burrows1270c182017-09-14 10:14:58 +0100173
174 var labelWidth = !_.isEmpty(this.label()) ? this.computeLabelWidth(node) + (labelPadding * 2) : 0;
175 rect.attr({ width: labelWidth });
Steven Burrows1c2a9682017-07-14 16:52:46 +0100176 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100177 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100178
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100179 return {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100180 createHostCollection: createHostCollection,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100181 };
Steven Burrows1c2a9682017-07-14 16:52:46 +0100182 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100183 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100184
185})();