blob: 40755fe254d7d69adf58a89202d5eb39ce0d5e68 [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 Burrowsaf96a212016-12-28 12:57:02 +000051 'IconService', 'Topo2ZoomService', 'Topo2HostsPanelService',
52 function (_c_, NodeModel, _t2vs_, is, zs, t2hds) {
Steven Burrows57e24e92016-08-04 18:38:24 +010053
Steven Burrowsaf96a212016-12-28 12:57:02 +000054 Collection = _c_;
Steven Burrows57e24e92016-08-04 18:38:24 +010055
Steven Burrowsaf96a212016-12-28 12:57:02 +000056 Model = NodeModel.extend({
Steven Burrows86af4352016-11-16 18:19:12 -060057 initialize: function () {
58 this.super = this.constructor.__super__;
59 this.super.initialize.apply(this, arguments);
60 },
61 events: {
62 'click': 'onClick'
63 },
64 onChange: function () {
65 // Update class names when the model changes
66 if (this.el) {
67 this.el.attr('class', this.svgClassName());
68 }
69 },
70 onClick: function () {
71 var selected = this.select(d3.select);
72
73 if (selected.length > 0) {
74 t2hds.displayPanel(this);
75 } else {
76 t2hds.hide();
77 }
78 },
Steven Burrowsbeef9352016-10-21 14:09:50 -050079 nodeType: 'host',
80 icon: function () {
81 var type = this.get('type');
Steven Burrows1aa4f582016-12-13 15:05:41 -050082 return remappedDeviceTypes[type] || type || 'm_endstation';
Steven Burrowsbeef9352016-10-21 14:09:50 -050083 },
Steven Burrows583f4be2016-11-04 14:06:50 +010084 label: function () {
85 var labelText = this.get('id'),
86 ips = this.get('ips');
87
88 if (this.labelIndex() === 0) {
89 return '';
90 }
91
92 if (ips && ips.length > 0) {
93 labelText = ips[0];
94 }
95
96 return labelText;
97 },
Steven Burrowsbeef9352016-10-21 14:09:50 -050098 setScale: function () {
99
100 var dim = hostIconDim,
101 multipler = 1;
102
103 if (dim * zs.scale() < hostIconDimMin) {
104 multipler = hostIconDimMin / (dim * zs.scale());
105 } else if (dim * zs.scale() > hostIconDimMax) {
106 multipler = hostIconDimMax / (dim * zs.scale());
107 }
108
109 this.el.select('g').selectAll('*')
110 .style('transform', 'scale(' + multipler + ')');
111 },
112 onEnter: function (el) {
113 var node = d3.select(el),
114 icon = this.icon(),
Steven Burrows583f4be2016-11-04 14:06:50 +0100115 iconDim = hostIconDim,
116 textDy = 5,
117 textDx = (hostIconDim * 2) + 20;
Steven Burrowsbeef9352016-10-21 14:09:50 -0500118
119 this.el = node;
120
121 var g = node.append('g')
122 .attr('class', 'svgIcon hostIcon');
123
124 g.append('circle').attr('r', hostIconDim);
125 g.append('use').attr({
126 'xlink:href': '#' + icon,
127 width: iconDim,
128 height: iconDim,
129 x: -iconDim / 2,
130 y: -iconDim / 2
131 });
132
Steven Burrows583f4be2016-11-04 14:06:50 +0100133 var labelText = this.label();
134
Steven Burrowsbeef9352016-10-21 14:09:50 -0500135 g.append('text')
Steven Burrows583f4be2016-11-04 14:06:50 +0100136 .text(labelText)
Steven Burrowsbeef9352016-10-21 14:09:50 -0500137 .attr('dy', textDy)
Steven Burrows583f4be2016-11-04 14:06:50 +0100138 .attr('dx', textDx)
Steven Burrowsbeef9352016-10-21 14:09:50 -0500139 .attr('text-anchor', 'middle');
140
141 this.setScale();
Steven Burrows86af4352016-11-16 18:19:12 -0600142 this.setUpEvents();
Steven Burrowsbeef9352016-10-21 14:09:50 -0500143 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100144 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100145
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100146 return {
147 createHostCollection: createHostCollection
148 };
149 }
150 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100151
152})();