blob: 3fdbeb531573fd76c8a4520033fd4e1eea975e02 [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 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 Burrowsbeef9352016-10-21 14:09:50 -050027 var hostIconDim = 20,
28 hostIconDimMin = 15,
29 hostIconDimMax = 20,
30 remappedDeviceTypes = {};
31
Steven Burrows57e24e92016-08-04 18:38:24 +010032 function createHostCollection(data, region) {
33
34 var HostCollection = Collection.extend({
35 model: Model
36 });
37
38 var hosts = [];
39 data.forEach(function (hostsLayer) {
40 hostsLayer.forEach(function (host) {
41 hosts.push(host);
42 });
43 });
44
45 return new HostCollection(hosts);
46 }
47
48 angular.module('ovTopo2')
Steven Burrowsdfa52b02016-09-02 13:50:43 +010049 .factory('Topo2HostService', [
Steven Burrowsec1f45c2016-08-08 16:14:41 +010050 'Topo2Collection', 'Topo2NodeModel', 'Topo2ViewService',
Steven Burrowsbeef9352016-10-21 14:09:50 -050051 'IconService', 'Topo2ZoomService',
52 function (_Collection_, _NodeModel_, _t2vs_, is, zs) {
Steven Burrows57e24e92016-08-04 18:38:24 +010053
Steven Burrowsec1f45c2016-08-08 16:14:41 +010054 Collection = _Collection_;
Steven Burrows57e24e92016-08-04 18:38:24 +010055
Steven Burrowsec1f45c2016-08-08 16:14:41 +010056 Model = _NodeModel_.extend({
Steven Burrowsbeef9352016-10-21 14:09:50 -050057 nodeType: 'host',
58 icon: function () {
59 var type = this.get('type');
60 return remappedDeviceTypes[type] || type || 'endstation';
61 },
Steven Burrows583f4be2016-11-04 14:06:50 +010062 label: function () {
63 var labelText = this.get('id'),
64 ips = this.get('ips');
65
66 if (this.labelIndex() === 0) {
67 return '';
68 }
69
70 if (ips && ips.length > 0) {
71 labelText = ips[0];
72 }
73
74 return labelText;
75 },
Steven Burrowsbeef9352016-10-21 14:09:50 -050076 setScale: function () {
77
78 var dim = hostIconDim,
79 multipler = 1;
80
81 if (dim * zs.scale() < hostIconDimMin) {
82 multipler = hostIconDimMin / (dim * zs.scale());
83 } else if (dim * zs.scale() > hostIconDimMax) {
84 multipler = hostIconDimMax / (dim * zs.scale());
85 }
86
87 this.el.select('g').selectAll('*')
88 .style('transform', 'scale(' + multipler + ')');
89 },
90 onEnter: function (el) {
91 var node = d3.select(el),
92 icon = this.icon(),
Steven Burrows583f4be2016-11-04 14:06:50 +010093 iconDim = hostIconDim,
94 textDy = 5,
95 textDx = (hostIconDim * 2) + 20;
Steven Burrowsbeef9352016-10-21 14:09:50 -050096
97 this.el = node;
98
99 var g = node.append('g')
100 .attr('class', 'svgIcon hostIcon');
101
102 g.append('circle').attr('r', hostIconDim);
103 g.append('use').attr({
104 'xlink:href': '#' + icon,
105 width: iconDim,
106 height: iconDim,
107 x: -iconDim / 2,
108 y: -iconDim / 2
109 });
110
Steven Burrows583f4be2016-11-04 14:06:50 +0100111 var labelText = this.label();
112
Steven Burrowsbeef9352016-10-21 14:09:50 -0500113 g.append('text')
Steven Burrows583f4be2016-11-04 14:06:50 +0100114 .text(labelText)
Steven Burrowsbeef9352016-10-21 14:09:50 -0500115 .attr('dy', textDy)
Steven Burrows583f4be2016-11-04 14:06:50 +0100116 .attr('dx', textDx)
Steven Burrowsbeef9352016-10-21 14:09:50 -0500117 .attr('text-anchor', 'middle');
118
119 this.setScale();
120 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100121 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100122
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100123 return {
124 createHostCollection: createHostCollection
125 };
126 }
127 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100128
129})();