Fix test identities
[groupbasedpolicy.git] / groupbasedpolicy-ui / module / src / main / resources / gbp / common / topology / next_topology.service.js
1 define(['next-ui'], function () {
2     'use strict';
3
4     /**
5      * Service to handle next directive
6      */
7     function NextTopologyService() {
8         /**
9          * Create base nx topology object
10          * @param nx
11          * @returns {nx.graphic.Topology}
12          */
13         this.getNxTopClass = function (topoColors) {
14             return new nx.graphic.Topology({
15                         adaptive: true,
16                         scalable: true,
17                         nodeConfig: {
18                             label: 'model.label',
19                             //iconType: 'model.icon',
20                             iconType: 'unlinked',
21                             color: function (node, model) {
22                                 return topoColors.getItem('forwarding-box');
23                             }
24                         },
25                         linkConfig: {
26                             // connected to hosts links have different colors
27                             width: function (model, link) {
28                                 return (model._data.gLinks && model._data.gLinks.length > 2) ? 5 : 3;
29                             },
30                             linkType: 'curve'
31                         },
32                         tooltipManagerConfig: {
33                             nodeTooltipContentClass: 'ExtendedTooltip',
34                             showLinkTooltip: false
35                         },
36                         dataProcessor: 'force',
37                         identityKey: 'id',
38                         showIcon: true,
39                         theme: 'blue',
40                         enableSmartNode: false,
41                         linkInstanceClass: 'ExtendedLink',
42                         nodeInstanceClass: 'ExtendedNode'
43                     });
44         };
45
46         /**
47          * Method for getting right link's color based on status
48          * @param status
49          * @param topoColors
50          * @returns {*}
51          */
52         this.getLinkColor = function (status, topoColors) {
53             var color = null;
54
55             if ( status ) {
56                 // all links are operational
57                 if ( status.operational === status.configured ) {
58                     color =  topoColors.getItem('operational');
59                 } else {
60                     // operational less than configured
61                     if (status.operational < status.configured && status.operational) {
62                         color = topoColors.getItem('operational-mixed');
63                     } else {
64                         // if operational and something else presented
65                         if (!status.operational && status.configured) {
66                             // if the link is between forwarding boxes, it's considered down
67                             /*if ( model.source()._data.type === 'forwarding-box' && model.target()._data.type === 'forwarding-box' ) {
68                                 color = topoColors.getItem('down');
69                             } else {
70                                 // otherwise just configured connection
71                                 color = topoColors.getItem('configured');
72                             }*/
73
74                             color = topoColors.getItem('down');
75                             // otherwise
76                         } else {
77                             color = topoColors.getItem('default');
78                         }
79                     }
80                 }
81
82             } else {
83                 color = topoColors.getItem('default');
84             }
85
86             return color;
87         };
88
89         /**
90          * Service for reading topo nodes data from local storage
91          * @returns {*}
92          */
93         this.readTopologyDataFromLS = function(){
94             var data = null;
95
96             try {
97                 data = JSON.parse(localStorage.getItem("verizonTopologyData"));
98             } catch(e) {
99                 console.info('Local Storage read parse error:', e);
100             }
101
102             return data;
103         };
104
105         /**
106          * Set loaded nodes data from local storage
107          * @param data
108          * @param topo
109          * @param nodesDict
110          */
111         this.setTopologyDataFromLS = function (data, topo, nodesDict) {
112             if ( data && data.nodes ) {
113
114                 data.nodes.forEach(function(node, index){
115                     var nodeInst = topo.getNode(nodesDict.getItem(node.nodeName));
116                     if(nodeInst !== undefined)
117                         nodeInst.position({'x': node.x, 'y': node.y});
118                 });
119             }
120         };
121
122         /**
123          * Prepare and save data to local storage
124          * @param topo
125          */
126         this.saveTopologyDataToLS = function (topo) {
127             var data = {'nodes': []},
128                 nodesLayer = topo.getLayer('nodes');
129
130             // prepare data for writing
131             nodesLayer.eachNode(function(node){
132                 data.nodes.push({
133                     'x': node.x(),
134                     'y': node.y(),
135                     'nodeName': node.model()._data['node-id']
136                 });
137             });
138
139             // save to local storage
140             try {
141                 localStorage.setItem("verizonTopologyData", JSON.stringify(data));
142             } catch(e) {
143                 console.info('Local Storage save error:', e);
144             }
145         };
146
147         /**
148          * Service for fade out all topo layers
149          * @param topo
150          */
151         this.fadeOutAllLayers = function(topo){
152             nx.each(topo.layers(), function(layer) {
153                 layer.fadeOut(true);
154             }, this);
155         };
156
157         /**
158          * Service for fade in all topo layers
159          * @param topo
160          */
161         this.fadeInAllLayers = function(topo){
162             //fade out all layers
163             var linksLayerHighlightElements = topo.getLayer('links').highlightedElements(),
164                 nodeLayerHighlightElements = topo.getLayer('nodes').highlightedElements();
165
166             //Clears previous
167             nodeLayerHighlightElements.clear();
168             linksLayerHighlightElements.clear();
169
170             nx.each(topo.layers(), function(layer) {
171                 layer.fadeIn(true);
172             }, this);
173         };
174
175         /**
176          * Service for highlighting node with(without) links
177          * @param topo
178          * @param targetId
179          * @param noLinks
180          */
181         this.highlightNode = function (topo, targetId, noLinks) {
182             var nodeLayer = topo.getLayer('nodes'),
183                 linksLayer = topo.getLayer('links'),
184                 linksLayerHighlightElements = linksLayer.highlightedElements(),
185                 nodeLayerHighlightElements = nodeLayer.highlightedElements();
186
187             //Clears previous
188             nodeLayerHighlightElements.clear();
189             linksLayerHighlightElements.clear();
190
191             //Highlight node
192             nodeLayerHighlightElements.add(topo.getNode(targetId));
193             if(!noLinks) {
194                 //highlight links
195                 linksLayerHighlightElements.addRange(nx.util.values(topo.getNode(targetId).links()));
196             }
197             else{
198                 linksLayer.fadeOut(true);
199             }
200         };
201
202         /**
203          * Service for highlighting link
204          * @param topo
205          * @param linkId
206          */
207         this.highlightLink = function(topo, linkId) {
208             var nodeLayer = topo.getLayer('nodes'),
209                 linksLayer = topo.getLayer('links'),
210                 linksLayerHighlightElements = linksLayer.highlightedElements(),
211                 nodeLayerHighlightElements = nodeLayer.highlightedElements(),
212                 link = topo.getLink(linkId);
213
214             //Clears previous
215             nodeLayerHighlightElements.clear();
216             linksLayerHighlightElements.clear();
217
218             //highlight link
219             linksLayerHighlightElements.add(link);
220             //highlight connected nodes
221             nodeLayerHighlightElements.addRange(nx.util.values({source: topo.getNode(link.model().sourceID()), target: topo.getNode(link.model().targetID())}));
222         };
223
224         /**
225          * Service for highlighting selected links path
226          * @param topo
227          * @param links - array of nx links obj
228          */
229         this.highlightPath = function(topo, links){
230                 // clear the path layer and get its instance
231             var pathLayer = this.clearPathLayer(topo),
232                 // define a path
233                 path = new nx.graphic.Topology.Path({
234                     'pathWidth': 5,
235                     'links': links,
236                     'arrow': 'cap'
237                 });
238
239             // add the path
240             pathLayer.addPath(path);
241         };
242
243         /**
244          * Completely clear all paths from path layer
245          * @param topo
246          * @returns {*} path instance
247          */
248         this.clearPathLayer = function(topo){
249             var pathLayer = topo.getLayer("paths");
250             pathLayer.clear();
251             return pathLayer;
252         };
253
254         /**
255          * Service for returning nx tooltip skeleton
256          * @returns {{content: *[]}}
257          */
258         this.getTooltipContent  = function () {
259             return {
260                 content: [
261                     {
262                         tag: "div",
263                         props: {
264                             class: "n-topology-tooltip-header"
265                         },
266                         content: [
267                             {
268                                 tag: 'span',
269                                 props: {
270                                     class: "n-topology-tooltip-header-text"
271                                 },
272                                 content: '{#node.model.label}'
273                             }
274                         ]
275                     },
276                     {
277                         tag: "div",
278                         props: {
279                             class: "n-topology-tooltip-content n-list"
280                         },
281                         content: [
282                             {
283                                 tag: 'ul',
284                                 props: {
285                                     class: "n-list-wrap",
286                                     style: "font-size: 0.8em"
287                                 },
288                                 content: [
289                                     {
290                                         tag: 'li',
291                                         props: {
292                                             class: "n-list-item-i",
293                                             role: "listitem"
294                                         },
295                                         content: [
296                                             {
297                                                 tag: "label",
298                                                 content: "Status",
299                                                 props: {
300                                                     style: "display: block; margin-top: 10px;"
301                                                 }
302                                             },
303                                             {
304                                                 tag: "span",
305                                                 content: "{#node.model.status}"
306                                             }
307                                         ]
308                                     },
309                                     {
310                                         tag: 'li',
311                                         props: {
312                                             class: "n-list-item-i",
313                                             role: "listitem"
314                                         },
315                                         content: [
316                                             {
317                                                 tag: "label",
318                                                 content: "DataPath ID",
319                                                 props: {
320                                                     style: "display: block; margin-top: 10px;"
321                                                 }
322                                             },
323                                             {
324                                                 tag: "span",
325                                                 content: "{#node.model.datapath-id}"
326                                             }
327                                         ]
328                                     },
329                                     {
330                                         tag: 'li',
331                                         props: {
332                                             class: "n-list-item-i",
333                                             role: "listitem"
334                                         },
335                                         content: [
336                                             {
337                                                 tag: "label",
338                                                 content: "Type",
339                                                 props: {
340                                                     style: "display: block; margin-top: 10px;"
341                                                 }
342                                             },
343                                             {
344                                                 tag: "span",
345                                                 content: "{#node.model.data.type}"
346                                             }
347                                         ]
348                                     }
349                                 ]
350                             }
351                         ]
352                     }
353                 ]
354             };
355         };
356     }
357
358     NextTopologyService.$inject=[];
359
360     return NextTopologyService;
361
362 });