99830aa9cbc362971a0e90fcb8d77340a1b60771
[VSoRC/.git] / js / topology / flowtables.js
1 // Copyright (c) 2018 Maen Artimy
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //   http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 // Main code to handle flow tables
17 $(function () {
18     var tabsObj = new Tabs('switches');
19     var tablesObj = new Tables('flow');
20
21     // Flow fields supported by OF1.3
22     var header_of13 = {
23         "priority": ["Priority", "number"],
24         "match": ["Match Fields", "alphanum"],
25         "cookie": ["Cookie", "number"],
26         "duration_sec": ["Duration", "number"],
27         "idle_timeout": ["Idle\nTimeout", "number"],
28         "hard_timeout": ["Hard\nTimeout", "number"],
29         "actions": ["Instructions", "alphanum"],
30         "packet_count": ["Packet\nCount", "number"],
31         "byte_count": ["Byte\nCount", "number"],
32         "flags": ["Flags", "number"]
33     }
34
35     // Table Header Mapping Function
36     function headerMapping(orgstr) {
37         var map = header_of13;
38         var newstr = map[orgstr] != null ? map[orgstr][0] : hc(orgstr);
39         var newtype = map[orgstr] != null ? map[orgstr][1] : "number";
40         return [newstr, newtype];
41     }
42
43     // Create Flow Table database
44     function buildFlowTables(dpid, flwlist) {
45         var thelist = JSON.parse(fix_compatibility(JSON.stringify(flwlist)));
46
47         var fields = Object.keys(header_of13);
48         var dp_tables = {};
49
50         thelist.forEach(function (flow) {
51             var table_id = flow['table_id'];
52             if (!dp_tables[table_id]) {
53                 dp_tables[table_id] = new DPTable(table_id, "flows", "Flow Table", fields, [], dpid);
54             }
55             dp_tables[table_id].data.push(flow);
56         });
57
58         //console.log(db_tables)
59         var $envelope = $('<div></div>');
60         for (var i in dp_tables) {
61             var dp_table = dp_tables[i]
62             tablesObj.makeRows(dpid, dp_table, headerMapping, cellFormating);
63             var $card = tablesObj.buildTableCard(dp_table);
64             $envelope.append($card);
65         }
66         return $envelope;
67     }
68
69     // Get flows data from swicthes
70     function loadFlows() {
71         getSwitchData(
72             "flows",
73             function (sw_list) {
74                 tabsObj.buildTabs("#main", sw_list, "Not flows to show!");
75             },
76             function (all_flows) {
77                 for (var i in all_flows) {
78                     var sw = Object.keys(all_flows[i])[0] // the first key is the datapath id
79                     var flows = all_flows[i][sw]
80                     if (flows.length > 0) {
81                         var $envelope = buildFlowTables(sw, flows);
82                         tabsObj.buildContent(sw, $envelope);
83                     }
84                 }
85                 tabsObj.setActive();
86             }
87         );
88     }
89
90     // When the refresh button is clicked, clear the page and start over
91     $("[name='refresh']").on('click', function () {
92         loadFlows();
93     })
94
95     loadFlows();
96
97
98     function startRefresh() {
99         setTimeout(startRefresh,10000);
100         loadFlows();
101     }
102
103     startRefresh()
104
105     // function autoRefreshPage() {
106     //     loadFlows();
107     // }
108     // setInterval(loadFlows, 5000);
109
110 })