blob: 44c5ec9d769c609654a3894f41c10e0f953b7d73 [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 Links Module.
19 Module that holds the links for a region
20 */
21
22(function () {
23 'use strict';
24
Steven Burrowsec1f45c2016-08-08 16:14:41 +010025 var Collection, Model, region, ts;
Steven Burrows57e24e92016-08-04 18:38:24 +010026
Steven Burrowsec1f45c2016-08-08 16:14:41 +010027 var widthRatio = 1.4,
28 linkScale = d3.scale.linear()
29 .domain([1, 12])
30 .range([widthRatio, 12 * widthRatio])
31 .clamp(true),
32 allLinkTypes = 'direct indirect optical tunnel UiDeviceLink',
33 allLinkSubTypes = 'inactive not-permitted';
34
35 // configuration
36 var linkConfig = {
37 light: {
38 baseColor: '#939598',
39 inColor: '#66f',
40 outColor: '#f00'
41 },
42 dark: {
43 // TODO : theme
44 baseColor: '#939598',
45 inColor: '#66f',
46 outColor: '#f00'
47 },
48 inWidth: 12,
49 outWidth: 10
50 };
51
52 var defaultLinkType = 'direct',
53 nearDist = 15;
54
55 function createLink() {
56
57 var linkPoints = this.linkEndPoints(this.get('epA'), this.get('epB'));
58 console.log(this);
59
60 var attrs = angular.extend({}, linkPoints, {
61 key: this.get('id'),
62 class: 'link',
63 weight: 1,
64 srcPort: this.get('srcPort'),
65 tgtPort: this.get('dstPort'),
66 position: {
67 x1: 0,
68 y1: 0,
69 x2: 0,
70 y2: 0
71 }
72 // functions to aggregate dual link state
73// extra: link.extra
74 });
75
76 this.set(attrs);
77 }
78
79 function linkEndPoints(srcId, dstId) {
80
81 var sourceNode = this.region.get('devices').get(srcId.substring(0, srcId.length -2));
82 var targetNode = this.region.get('devices').get(dstId.substring(0, dstId.length -2));
83
84// var srcNode = lu[srcId],
85// dstNode = lu[dstId],
86// sMiss = !srcNode ? missMsg('src', srcId) : '',
87// dMiss = !dstNode ? missMsg('dst', dstId) : '';
88//
89// if (sMiss || dMiss) {
90// $log.error('Node(s) not on map for link:' + sMiss + dMiss);
91// //logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
92// return null;
93// }
94
95 this.source = sourceNode.toJSON();
96 this.target = targetNode.toJSON();
97
98 return {
99 source: sourceNode,
100 target: targetNode
101 };
102 }
103
104 function createLinkCollection(data, _region) {
105
106 var LinkModel = Model.extend({
107 region: _region,
108 createLink: createLink,
109 linkEndPoints: linkEndPoints,
110 type: function () {
111 return this.get('type');
112 },
113 expected: function () {
114 //TODO: original code is: (s && s.expected) && (t && t.expected);
115 return true;
116 },
117 online: function () {
118 return true;
119 return both && (s && s.online) && (t && t.online);
120 },
121 linkWidth: function () {
122 var s = this.get('fromSource'),
123 t = this.get('fromTarget'),
124 ws = (s && s.linkWidth) || 0,
125 wt = (t && t.linkWidth) || 0;
126
127 // console.log(s);
128 // TODO: Current json is missing linkWidth
129 return 1.2;
130 return this.get('position').multiLink ? 5 : Math.max(ws, wt);
131 },
132
133 restyleLinkElement: function (immediate) {
134 // this fn's job is to look at raw links and decide what svg classes
135 // need to be applied to the line element in the DOM
136 var th = ts.theme(),
137 el = this.el,
138 type = this.get('type'),
139 lw = this.linkWidth(),
140 online = this.online(),
141 modeCls = this.expected() ? 'inactive' : 'not-permitted',
142 delay = immediate ? 0 : 1000;
143
144 console.log(type);
145
146 // NOTE: understand why el is sometimes undefined on addLink events...
147 // Investigated:
148 // el is undefined when it's a reverse link that is being added.
149 // updateLinks (which sets ldata.el) isn't called before this is called.
150 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
151 // a more efficient way to fix it.
152 if (el && !el.empty()) {
153 el.classed('link', true);
154 el.classed(allLinkSubTypes, false);
155 el.classed(modeCls, !online);
156 el.classed(allLinkTypes, false);
157 if (type) {
158 el.classed(type, true);
159 }
160 el.transition()
161 .duration(delay)
162 .attr('stroke-width', linkScale(lw))
163 .attr('stroke', linkConfig[th].baseColor);
164 }
165 },
166
167 onEnter: function (el) {
168 var link = d3.select(el);
169 this.el = link;
170
171 this.restyleLinkElement();
172
173 if (this.get('type') === 'hostLink') {
174 sus.visible(link, api.showHosts());
175 }
176 }
177 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100178
179 var LinkCollection = Collection.extend({
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100180 model: LinkModel,
Steven Burrows57e24e92016-08-04 18:38:24 +0100181 });
182
183 return new LinkCollection(data);
184 }
185
186 angular.module('ovTopo2')
187 .factory('Topo2LinkService',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100188 ['Topo2Collection', 'Topo2Model', 'ThemeService',
Steven Burrows57e24e92016-08-04 18:38:24 +0100189
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100190 function (_Collection_, _Model_, _ts_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100191
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100192 ts = _ts_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100193 Collection = _Collection_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100194 Model = _Model_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100195
196 return {
197 createLinkCollection: createLinkCollection
198 };
199 }
200 ]);
201
202})();