blob: 4247f641bbe878f7954622ce2942097658892fee [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 Burrowscc6f6de2017-08-30 12:50:38 +010081 labelIndex: function () {
82 return t2ps.get('hlbls');
83 },
Steven Burrows583f4be2016-11-04 14:06:50 +010084 label: function () {
Steven Burrowscc6f6de2017-08-30 12:50:38 +010085 var props = this.get('props'),
86 id = this.get('ips')[0] || 'unknown',
87 friendlyName = props && props.name ? props.name : id,
88 labels = ['', friendlyName || id, id, this.get('id')],
89 nli = this.labelIndex(),
90 idx = (nli < labels.length) ? nli : 0;
91
92 return labels[idx];
Steven Burrows583f4be2016-11-04 14:06:50 +010093 },
Steven Burrows1270c182017-09-14 10:14:58 +010094 updateLabel: function () {
95 var node = this.el,
96 label = this.trimLabel(this.label()),
97 labelWidth;
98
99 node.select('text').text(label);
100 labelWidth = !_.isEmpty(this.label()) ? this.computeLabelWidth(node) + (labelPadding * 2) : 0;
101
102 node.select('rect')
103 .transition()
104 .attr({
105 width: labelWidth,
106 });
107 },
Steven Burrowsbeef9352016-10-21 14:09:50 -0500108 setScale: function () {
109
Steven Burrows8909c5c2017-04-10 09:56:52 -0700110 if (!this.el) return;
111
Steven Burrowsbeef9352016-10-21 14:09:50 -0500112 var dim = hostIconDim,
113 multipler = 1;
114
115 if (dim * zs.scale() < hostIconDimMin) {
116 multipler = hostIconDimMin / (dim * zs.scale());
117 } else if (dim * zs.scale() > hostIconDimMax) {
118 multipler = hostIconDimMax / (dim * zs.scale());
119 }
120
121 this.el.select('g').selectAll('*')
122 .style('transform', 'scale(' + multipler + ')');
123 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700124 setVisibility: function () {
125 var visible = ps.getPrefs('topo2_prefs')['hosts'];
126 this.el.style('visibility', visible ? 'visible' : 'hidden');
127 },
Steven Burrowsbeef9352016-10-21 14:09:50 -0500128 onEnter: function (el) {
129 var node = d3.select(el),
130 icon = this.icon(),
Steven Burrows583f4be2016-11-04 14:06:50 +0100131 textDy = 5,
Steven Burrows1270c182017-09-14 10:14:58 +0100132 textDx = (hostIconDim * 2);
Steven Burrowsbeef9352016-10-21 14:09:50 -0500133
134 this.el = node;
135
136 var g = node.append('g')
137 .attr('class', 'svgIcon hostIcon');
138
Steven Burrows1270c182017-09-14 10:14:58 +0100139 // Add Label background to host
140
141 var rect = g.append('rect').attr({
142 width: 0,
143 height: hostIconDim * 2,
144 y: - hostIconDim,
145 });
146
Steven Burrowsbeef9352016-10-21 14:09:50 -0500147 g.append('circle').attr('r', hostIconDim);
Steven Burrowse1ea27b2017-04-12 09:53:41 -0700148
149 var glyphSize = hostIconDim * 1.5;
Steven Burrowsbeef9352016-10-21 14:09:50 -0500150 g.append('use').attr({
151 'xlink:href': '#' + icon,
Steven Burrowse1ea27b2017-04-12 09:53:41 -0700152 width: glyphSize,
153 height: glyphSize,
154 x: -glyphSize / 2,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100155 y: -glyphSize / 2,
Steven Burrowsbeef9352016-10-21 14:09:50 -0500156 });
157
Steven Burrows583f4be2016-11-04 14:06:50 +0100158 var labelText = this.label();
159
Steven Burrowsbeef9352016-10-21 14:09:50 -0500160 g.append('text')
Steven Burrows583f4be2016-11-04 14:06:50 +0100161 .text(labelText)
Steven Burrowsbeef9352016-10-21 14:09:50 -0500162 .attr('dy', textDy)
Steven Burrows583f4be2016-11-04 14:06:50 +0100163 .attr('dx', textDx)
Steven Burrows1270c182017-09-14 10:14:58 +0100164 .attr('text-anchor', 'left');
Steven Burrowsbeef9352016-10-21 14:09:50 -0500165
166 this.setScale();
Steven Burrows86af4352016-11-16 18:19:12 -0600167 this.setUpEvents();
Steven Burrowsaea509d2017-04-12 14:17:47 -0700168 this.setVisibility();
Steven Burrows1270c182017-09-14 10:14:58 +0100169
170 var labelWidth = !_.isEmpty(this.label()) ? this.computeLabelWidth(node) + (labelPadding * 2) : 0;
171 rect.attr({ width: labelWidth });
Steven Burrows1c2a9682017-07-14 16:52:46 +0100172 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100173 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100174
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100175 return {
Steven Burrows1c2a9682017-07-14 16:52:46 +0100176 createHostCollection: createHostCollection,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100177 };
Steven Burrows1c2a9682017-07-14 16:52:46 +0100178 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100179 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100180
181})();