Added images and made the visualizator work
[VSoRC/.git] / js / flow-stats.js
1 /*
2  * Copyright (C) 2014 SDN Hub
3  *
4  * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.
5  * You may not use this file except in compliance with this License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.gnu.org/licenses/gpl-3.0.txt
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
13  * implied.
14  */
15
16 var url = "http://" + location.hostname + ":8080";
17
18 function updateFlowStats() {
19     var statsTableBody = document.getElementById('flow-stats-data');
20     while (statsTableBody.firstChild) {
21             statsTableBody.removeChild(statsTableBody.firstChild);
22     }
23
24     $.getJSON(url.concat("/stats/switches"), function(switches){
25         $.each(switches, function(index, dpid){
26             var hex_dpid = parseInt(dpid).toString(16);
27
28             $.getJSON(url.concat("/stats/flow/").concat(dpid), function(flows) {
29                 var flowStats = flows[dpid];
30
31                 var tr = document.createElement('TR');
32                 var numFlows = 0;
33                 var switchColTd = document.createElement('TD');
34                 switchColTd.appendChild(document.createTextNode(hex_dpid));
35                 tr.appendChild(switchColTd);
36
37                 var td;
38
39                 $.each(flowStats, function(index, obj) {
40                     var outPorts = [];
41                     if ("actions" in obj) {
42                         $.each(obj.actions, function(index, action) {
43                             var command = action.split(':')[0];
44                             var param = action.split(':')[1];
45
46                             if (command == "OUTPUT") {
47                                 if (param < 65280) 
48                                     outPorts.push(param);
49                             }
50                         });
51                     }
52                     if (outPorts.length > 0) {
53                         numFlows += 1;
54                         var matchFields = new Array("in_port", "dl_src", "dl_dst", "dl_type",
55                             "nw_src", "nw_dst", "nw_proto", "tp_src", "tp_dst");
56
57                         if (!("match" in obj)) {
58                             obj.match = {};
59                         }
60
61                         $.each(matchFields, function(index, field) {
62                             td = document.createElement('TD');
63                             if (field in obj.match)  {
64                                 value = obj.match[field];
65                                 if (field == "dl_type")
66                                     value = ethertypeToString(obj.match[field]);
67                                 else if (field == "nw_proto")
68                                     value = nwprotoToString(obj.match[field]);
69
70                                 td.appendChild(document.createTextNode(value));
71                             }
72                             else
73                                 td.appendChild(document.createTextNode("*"));
74                             tr.appendChild(td);
75                         });
76
77                         td = document.createElement('TD');
78                         td.appendChild(document.createTextNode(outPorts));
79                         tr.appendChild(td);
80
81                         td = document.createElement('TD');
82                         var duration = obj.duration_sec + obj.duration_nsec/1000000000;
83                         td.appendChild(document.createTextNode(duration));
84                         tr.appendChild(td);
85
86                         td = document.createElement('TD');
87                         td.appendChild(document.createTextNode(obj.packet_count));
88                         tr.appendChild(td);
89
90                         td = document.createElement('TD');
91                         td.appendChild(document.createTextNode(obj.byte_count));
92                         tr.appendChild(td);
93
94                         statsTableBody.appendChild(tr);
95                         tr = document.createElement('TR');
96                     }
97                 });
98
99                 switchColTd.rowSpan = numFlows;
100             });
101         });
102     });
103 }
104
105 updateFlowStats();
106
107 var flowStatsIntervalID = setInterval(function(){updateFlowStats()}, 5000);
108
109 function stopFlowStatsTableRefresh() {
110     clearInterval(flowStatsIntervalID);
111 }