3c1aa494d1b0a766758044e108f28dc05c27e95b
[nemo.git] / nemo-ui / src / main / resources / nemo / js / phyNetwork.js
1 //var phy_hosts; move to nemo_main.js
2 //var phy_hosts; move to nemo_main.js
3
4 var phy_links,phy_nodes;
5 var host_count,node_count_phy,link_count;
6 var Node_phy_Id = 0;
7 var Edge_phy_Id = 0;
8 nodes_phy = new vis.DataSet();
9 edges_phy = new vis.DataSet();
10 var host_id = [];
11 var host_name = [];
12 var phy_host_ip = [];
13 var phy_host_mac = [];
14 var node_map_id = [];
15
16 var physicalData=null;
17 jQuery(document).ready(function ($) {
18 getPhysicalDatas();
19 if(!physicalData) return;
20 //analy_topo(physicalData);
21 //create_physical_topo();
22 });
23  //physicalJson
24  function getPhysicalDatas(){
25         var PhyData=null;
26         jQuery.ajax({
27                         url: "/restconf/operational/generic-physical-network:physical-network/", 
28                         type:"GET",
29                         dataType:"json",
30                         async:false,
31                         success: function(data){
32                                 console.log(data);
33                                 if(data != null)
34                                 {
35                                         PhyData=data;
36                                         physicalData=data;
37                                         //analy_topo(physicalData);
38                     //create_physical_topo();
39                                 }
40                                 else
41                                         alert("No Physical Data");
42                         },
43                         error:function(data){
44                          alert("Get Physical Data Error!");
45                         }
46                 }); 
47         return PhyData;
48  }
49
50  function getPhysicalData(){
51  physicalData=getPhysicalDatas();
52  console.log(physicalData)
53  if(!physicalData) physicalData=physicalJson;
54  if(!physicalData) return;
55  }
56 // getPhysicalData();
57
58 function createPhysicalTopo(Data){
59         nodes_phy.clear();
60         edges_phy.clear();
61         if(!Data) Data=physicalData;
62         if(!Data) return;
63         var physicalHost=Data['physical-network']['physical-hosts'];
64         var physicalNode=Data['physical-network']['physical-nodes']['physical-node'];
65         var physicalLink=Data['physical-network']['physical-links'];
66         var myHost=[];//host_id----->[vis_node_id,connected node_id,host_name]
67         var myHostPort=[];//host_id----->host_port_id
68         var myNode=[];//node_id----->[vis_node_id,node_type]
69         var externalNode=[];//external_port_id----->[vis_node_id,node_id]
70         var myLink=[];//null
71         if(physicalHost){
72                 physicalHost=physicalHost['physical-host'];             
73                 for(var i in physicalHost){
74                         myHost[physicalHost[i]['host-id']]=[+i+1,physicalHost[i]['node-id'],physicalHost[i]['host-name']];
75                         myHostPort[physicalHost[i]['host-id']]=physicalHost[i]['port-id'];
76                         nodes_phy.add({
77                                 id: +i+1,
78                 label: physicalHost[i]["host-name"],
79                 image: "src/app/nemo/images/host.png",
80                 shape: 'image',
81                             fontSize: 15
82                         });
83                 }
84         }
85         console.log(myHost);
86         if(physicalNode){
87                 var nodeslen=nodes_phy.get().length;
88                 for(var i in physicalNode){
89                         myNode[physicalNode[i]['node-id']]=[nodeslen+1,physicalNode[i]['node-type'],''];
90                         nodes_phy.add({
91                                 id: ++nodeslen,
92                 label: physicalNode[i]["node-type"]+physicalNode[i]["node-id"].split(":")[1],
93                 group: physicalNode[i]["node-type"],
94                         });
95                         var physicalPort=physicalNode[i]['physical-port'];
96                         if(physicalPort){
97                                 for(var item in physicalPort){
98                                         if(physicalPort[item]['port-type']=='external')
99                                         {
100                                         var flag=true;
101                                         for(var hostPort in myHostPort){
102                                                 if(physicalPort[item]['port-id']==myHostPort[hostPort])
103                                                         flag=false;
104                                         }
105                                         if(flag)        
106                                         externalNode[physicalPort[item]['port-id']]=[physicalNode[i]['node-id']];
107                                     }
108                                 }
109                         }
110                 }
111         }
112         console.log(myNode);
113         console.log(externalNode);
114
115         var externalnodecount=0;
116     for(var x in externalNode){externalnodecount++;}
117     if(externalnodecount>0){
118         var nodeslen=nodes_phy.get().length;
119         for(var i in externalNode){
120                 externalNode[i]=[nodeslen+1,externalNode[i][0]];
121                 nodes_phy.add({
122                                 id: ++nodeslen,
123                 label: 'ext-network',
124                 image: "src/app/nemo/images/ext-group.png",
125                 shape: 'image',
126                         });
127         }
128         console.log(externalNode);
129         var edgeslen=edges_phy.get().length;
130         for(var i in externalNode){
131                 edges_phy.add({
132                                 id: ++edgeslen,
133                 from:externalNode[i][0],
134                             to:myNode[externalNode[i][1]][0],
135                             length:150,
136                             width: 1
137                         });
138         }
139     }
140         if(physicalLink){
141                 physicalLink=physicalLink['physical-link'];
142                 var edgeslen=edges_phy.get().length;
143                 for(var i in physicalLink){
144                         // myLink[]
145                         edges_phy.add({
146                      id:++edgeslen,
147                         from:myNode[physicalLink[i]['src-node-id']][0],
148                         to:myNode[physicalLink[i]['dest-node-id']][0],
149                         length:150,
150                         width: 2
151                         });
152                 }
153         }
154
155     var hostcount=0;
156         for(var x in physicalHost){hostcount++;}
157         if(hostcount>0){
158                 var edgeslen=edges_phy.get().length;
159                 console.log(edgeslen);
160                 for(var i in myHost){
161                         // myLink[]
162                         edges_phy.add({
163                     id:++edgeslen,
164                         from:myHost[i][0],
165                         to:myNode[myHost[i][1]][0],
166                         length:150,
167                         width: 1
168                         });
169                 }
170         }
171
172 var data = {
173                 nodes: nodes_phy,
174                 edges: edges_phy
175         };
176
177 var options_phy = {
178                 /* physics: {
179                 repulsion: {
180                         centralGravity: 0,
181                         springLength: 200,//弹簧长度
182                         springConstant: 0,//弹簧常数
183                         nodeDistance: 0,
184                         damping: 0 //阻尼,减幅,衰减
185                 }}, */
186                 smoothCurves: false,
187                 stabilize: false,
188                 nodes: {
189           // default for all nodes
190                         shape: 'dot', 
191                         fontSize:16,
192                         radius:23,
193                         fixed:true
194                 },
195                 edges:{
196                         // length:200,
197                         width: 1
198                 },
199                 groups:{
200                         switch:{
201                                 color: {
202                                         border: 'black',
203                                         background: '#B0E2FF',                          
204                                 }
205             },
206                                 
207                         router:{
208                                 color: {
209                                         border: 'black',
210                                         background: '#7FFF00',
211                                 }       
212
213                         }
214                 }
215                 
216         };
217         console.log(jQuery('#phy_graph').width());
218         jQuery('#phy_graph').width(800).height(500);
219         console.log(jQuery('#phy_graph').width());
220         var container = document.getElementById('phy_graph');
221         if(!container) return;
222         var graph = new vis.Graph(container, data, options_phy);
223 }
224
225 function analy_topo(topo_data)
226 {
227         phy_hosts = [];
228         phy_nodes = [];
229         phy_links = [];
230         host_count = 0;
231         node_count_phy = 0;
232         link_count = 0;
233         if(topo_data["physical-network"]["physical-nodes"]!=null)
234         {
235                 for(var i in topo_data["physical-network"]["physical-nodes"]["physical-node"])
236                 {
237                         var node_data_temp = [];
238                         var data_temp;
239                         var json_temp = topo_data["physical-network"]["physical-nodes"]["physical-node"][i];
240 /*                      for(var key in json_temp)
241                         {
242                                 data_temp = json_temp[key];
243                         } */
244                         node_data_temp["node-id"] = json_temp["node-id"];
245                         node_data_temp["node-type"] = json_temp["node-type"];
246                         phy_nodes[node_count_phy] = node_data_temp;
247                         node_count_phy++;
248                 }
249
250         }
251         if(topo_data["physical-network"]["physical-hosts"]!=null)
252         {
253                 for(var i in topo_data["physical-network"]["physical-hosts"]["physical-host"])
254                 {
255                         //var host_name_temp = 'host' + i;
256                         var host_data_temp = [];
257                         var data_temp;
258                         var json_temp = topo_data["physical-network"]["physical-hosts"]["physical-host"][i];
259                         /* for(var key in json_temp)
260                         {
261                                 data_temp = json_temp[key];
262                         } */
263                         host_data_temp["node-id"] = json_temp["node-id"];
264                         host_data_temp["host-name"] = json_temp["host-name"];
265                         host_data_temp["host-id"] = json_temp["host-id"];
266                         phy_hosts[host_count] = host_data_temp;
267                         host_count++;
268                         host_id[json_temp["host-name"]] = json_temp["host-id"];
269                         host_name[json_temp["host-id"]] = json_temp["host-name"];
270                         phy_host_ip[json_temp["ip-addresses"]["ip-address"][0]] = json_temp["host-name"];
271                         phy_host_mac[json_temp["mac-address"]] = json_temp["host-name"];
272                 }
273         }
274         if(topo_data["physical-network"]["physical-links"]!=null)
275         {
276                 for(var i in topo_data["physical-network"]["physical-links"]["physical-link"])
277                 {
278                         var link_data_temp = [];
279                         var data_temp;
280                         var json_temp = topo_data["physical-network"]["physical-links"]["physical-link"][i];
281                 /*      for(var key in json_temp)
282                         {
283                                 data_temp = json_temp[key];
284                         } */
285                         link_data_temp["link-id"] = json_temp["link-id"];
286                         link_data_temp["src-node-id"] = json_temp["src-node-id"];
287                         link_data_temp["dest-node-id"] = json_temp["dest-node-id"];
288                         phy_links[link_count] = link_data_temp;
289                         link_count++;
290                 }
291         }
292 }
293
294 function create_physical_topo()
295 {
296         nodes_phy.clear();
297         edges_phy.clear();
298         node_map_id = [];
299         Node_phy_Id = 0;
300         Edge_phy_Id = 0;
301         console.log(phy_nodes);
302         for(var i in phy_nodes)
303         {
304                 nodes_phy.add({
305             id: ++Node_phy_Id,
306             label: phy_nodes[i]["node-type"]+phy_nodes[i]["node-id"].split(":")[1],
307             group: phy_nodes[i]["node-type"],
308         });
309                 node_map_id[phy_nodes[i]["node-id"]] = Node_phy_Id;
310         }
311         for(var i in phy_hosts)
312         {
313                 nodes_phy.add({
314             id: ++Node_phy_Id,
315             label: phy_hosts[i]["host-name"],
316             image: "src/app/nemo/images/host.png",
317             shape: 'image',
318                         fontSize: 15
319         });
320                 var to_full_id;
321                 for(var node_id_cursor in phy_nodes)
322                 {
323                         if(phy_nodes[node_id_cursor]["node-id"].indexOf(phy_hosts[i]["node-id"]) > -1)
324                         {
325                                 to_full_id = phy_nodes[node_id_cursor]["node-id"];
326                                 break;
327                         }
328                 }
329                 edges_phy.add({
330                         id:++Edge_phy_Id,
331                         from:Node_phy_Id,
332                         to:node_map_id[to_full_id],
333                         length:150
334                 });
335                 node_map_id[phy_nodes[i]["host-id"]] = Node_phy_Id;
336         }
337         for(var i in phy_links)
338         {
339             var from_full_id;
340                 for(var node_id_cursor in phy_nodes)
341                 {
342                         if(phy_nodes[node_id_cursor]["node-id"].indexOf(phy_links[i]["src-node-id"]) > -1)
343                         {
344                                 from_full_id = phy_nodes[node_id_cursor]["node-id"];
345                                 break;
346                         }
347                 }
348                 var to_full_id;
349                 for(var node_id_cursor in phy_nodes)
350                 {
351                         if(phy_nodes[node_id_cursor]["node-id"].indexOf(phy_links[i]["dest-node-id"]) > -1)
352                         {
353                                 to_full_id = phy_nodes[node_id_cursor]["node-id"];
354                                 break;
355                         }
356                 }
357                 edges_phy.add({
358                         id:++Edge_phy_Id,
359                         from:node_map_id[from_full_id],
360                         to:node_map_id[to_full_id],
361                         length:150,
362                         width: 2
363                 });
364         }
365         // console.log(1);
366         var data = {
367                 nodes: nodes_phy,
368                 edges: edges_phy
369         };
370         var options_phy = {
371                 /* physics: {
372                 repulsion: {
373                         centralGravity: 0,
374                         springLength: 200,//弹簧长度
375                         springConstant: 0,//弹簧常数
376                         nodeDistance: 0,
377                         damping: 0 //阻尼,减幅,衰减
378                 }}, */
379                 smoothCurves: false,
380                 stabilize: false,
381                 nodes: {
382           // default for all nodes
383                         shape: 'dot', 
384                         fontSize:16,
385                         radius:23,
386                         fixed:true
387                 },
388                 edges:{
389                         // length:200,
390                         width: 1
391                 },
392                 groups:{
393                         switch:{
394                                 color: {
395                                         border: 'black',
396                                         background: '#B0E2FF',                          
397                                 }
398             },
399                                 
400                         router:{
401                                 color: {
402                                         border: 'black',
403                                         background: '#7FFF00',
404                                 }       
405
406                         }
407                 }
408                 
409         };
410         console.log(jQuery('#phy_graph').width());
411         jQuery('#phy_graph').width(800).height(500);
412         console.log(jQuery('#phy_graph').width());
413         var container = document.getElementById('phy_graph');
414         if(!container) return;
415         var graph = new vis.Graph(container, data, options_phy)
416 }
417
418 var creataPhysicalTables={
419 createPhyicalNodeTable:function (id,Data){
420         if(!Data) Data=physicalData;
421         if(!Data) return;
422         var physicalnodes=[];//physical_node_id---->[physical_node_type,[internal physical port number,external physical port number]]
423         var Mynode=Data['physical-network']['physical-nodes']['physical-node'];
424         for(var i in Mynode){
425                 physicalnodes[Mynode[i]['node-id']]=[Mynode[i]['node-type']];
426             var physicalPorts=Mynode[i]['physical-port'];
427             if(!physicalPorts) {physicalnodes[Mynode[i]['node-id']].push(['','']);continue;}
428             var interPort=exterPort=0;
429             for(var port in physicalPorts){
430                 if(physicalPorts[port]['port-type']=='internal') interPort++;
431                 if(physicalPorts[port]['port-type']=='external') exterPort++;           
432             }
433             physicalnodes[Mynode[i]['node-id']].push([interPort,exterPort]);
434         }
435         console.log(physicalnodes);
436         jQuery("#"+id).find('tr').remove();
437         for(var item in physicalnodes){
438                 var $tr='<tr><td>'+item+'</td><td>'+physicalnodes[item][0]+'</td>'
439                 $tr+='<td>'+physicalnodes[item][1][0]+'</td><td>'+physicalnodes[item][1][1]+'</td>'
440                 $tr+='</tr>'
441                 jQuery("#"+id).append($tr);
442         }
443         var node_count = jQuery("#"+id).find('tr').length;
444         console.log("node_count:",node_count);
445         if(!node_count) return;
446         if(node_count<10){
447                 jQuery("div.tableBodyContainer:first").height(node_count*22+5);
448         }
449         else{
450                 jQuery("div.tableBodyContainer:first").height(200);
451         }
452 },
453 createPhyicalLinkTable:function (id,Data){
454         if(!Data) Data=physicalData;
455         if(!Data) return;
456         var physaicallinks=[];
457         var mylink=Data['physical-network']['physical-links']['physical-link'];
458         for(var i in mylink){
459                 if(!mylink[i]['metric'])
460                 physaicallinks[mylink[i]['link-id']]=[mylink[i]['src-node-id'],mylink[i]['dest-node-id'],'',mylink[i]['bandwidth'],mylink[i]['delay']];
461             else
462                 physaicallinks[mylink[i]['link-id']]=[mylink[i]['src-node-id'],mylink[i]['dest-node-id'],mylink[i]['metric'],mylink[i]['bandwidth'],mylink[i]['delay']];
463
464         }
465         console.log(physaicallinks);
466         jQuery("#"+id).find('tr').remove();
467         for(var item in physaicallinks){
468                 var $tr='<tr><td>'+item+'</td><td>'+physaicallinks[item][0]+'</td>'
469                  $tr+='<td>'+physaicallinks[item][1]+'</td><td>'+physaicallinks[item][2]+'</td>';
470                  $tr+='<td>'+physaicallinks[item][3]+'kbps'+'</td><td>'+physaicallinks[item][4]+'ms'+'</td>';
471                  $tr+='</tr>'
472                 jQuery("#"+id).append($tr);
473         }
474 }
475 }
476
477