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