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