Added images and made the visualizator work
[VSoRC/.git] / js / tap.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 var originalMain;
18
19 var portMap = {};
20
21 function updateSwitchList() {
22     var switchSelect = document.getElementById("switch");
23     $.getJSON(url.concat("/v1.0/topology/switches"), function(switches){
24         $.each(switches, function(index, value){
25             var el = document.createElement("option");
26             el.textContent = value.dpid;
27             el.value = value.dpid;
28             switchSelect.appendChild(el);
29
30             portMap[value.dpid] = value.ports;
31         });
32     }).then(updatePorts);
33 }
34
35 function updatePorts() {
36     var srcPortSelect = document.getElementById("src-ports");
37     removeAllChildren(srcPortSelect);
38
39     var allEl = document.createElement("option");
40     allEl.textContent = "all";
41     allEl.value = "all";
42     allEl.setAttribute('selected', 'selected');
43     srcPortSelect.appendChild(allEl);
44
45     var sinkPortSelect = document.getElementById("sink-ports");
46     removeAllChildren(sinkPortSelect);
47
48     var dpid = $('#switch').val();
49     $.each(portMap[dpid], function(key, value) {
50         var portNum = parseInt(value.port_no);
51         var el = document.createElement("option");
52         el.textContent = portNum;
53         el.value = portNum;
54         srcPortSelect.appendChild(el);
55
56         el = document.createElement("option");
57         el.textContent = portNum;
58         el.value = portNum;
59         sinkPortSelect.appendChild(el);
60    });
61 }
62
63 /* Format of the POST data is as follows:
64
65 {'fields': {'  'dl_src': mac string,
66                'dl_dst': mac string,
67                'dl_type': int,
68                'dl_vlan': int,
69                'nw_src': ip string,
70                'nw_dst': ip string,
71                'nw_proto': int,
72                'tp_src': int,
73                'tp_dst': int},
74 'sources': list of {'dpid': int, 'port_no': int},
75 'sinks': list of {'dpid': int, 'port_no': int}
76 }
77
78  */
79
80 function makePostData() {
81     var tapInfo = {};
82     var dpid = $('#switch').val();
83     var srcPorts = $('#src-ports').val();
84     var sinkPorts = $('#sink-ports').val();
85
86     if (sinkPorts == undefined) {
87         alert("Sink ports need to be specified.");
88         return undefined;
89     } 
90
91     tapInfo['sources'] = [];
92     tapInfo['sinks'] = [];
93     tapInfo['fields'] = {};
94
95     if ($.inArray('all', srcPorts) != -1)
96          tapInfo.sources.push({'dpid': parseInt(dpid), 'port_no': 'all'});
97     else {
98         $.each(srcPorts, function(index, value) {
99             port = {'dpid': parseInt(dpid), 'port_no': parseInt(value)};
100             tapInfo.sources.push(port);
101         });
102     }
103     $.each(sinkPorts, function(index, value) {
104         var port = {'dpid': parseInt(dpid), 'port_no': parseInt(value)};
105         tapInfo.sinks.push(port);
106     });
107
108     var macStr = $('#mac-addr').val();
109     var ipStr = $('#ip-addr').val();
110     var trafficType = $('#traffic-type').val();
111     var macClass = $('#mac-class').val();
112     var ipClass = $('#ip-class').val();
113
114     if (macClass != "--Ignore--") {
115         if (macStr == undefined || macStr=="") {
116             alert("MAC address needs to be specified.");
117             return undefined;
118         }
119     }
120     if (macClass == 'Source') 
121         tapInfo.fields['dl_src'] = macStr;
122     else if (macClass == 'Destination') 
123         tapInfo.fields['dl_dst'] = macStr;
124     else if (macClass == 'Src or Dest') 
125         tapInfo.fields['dl_host'] = macStr;
126
127     if (ipClass != "--Ignore--") {
128         if (ipStr == undefined || ipStr=="") {
129             alert("MAC address needs to be specified.");
130             return undefined;
131         }
132         tapInfo.fields['dl_type'] = 0x800;
133     }
134     if (ipClass == 'Source') 
135         tapInfo.fields['nw_src'] = ipStr;
136     else if (ipClass == 'Destination') 
137         tapInfo.fields['nw_dst'] = ipStr;
138     else if (ipClass == 'Src or Dest') 
139         tapInfo.fields['nw_host'] = ipStr;
140
141     if (trafficType == 'ARP') {
142         tapInfo.fields['dl_type'] = 0x806;
143     }
144
145     // Set prerequisite of IPv4 for all other types
146     else if (trafficType == 'ICMP') {
147         tapInfo.fields['dl_type'] = 0x800;
148         tapInfo.fields['nw_proto'] = 1;
149
150     } else if (trafficType == 'TCP') {
151         tapInfo.fields['dl_type'] = 0x800;
152         tapInfo.fields['nw_proto'] = 6;
153     }
154     else if (trafficType == 'HTTP') {
155         tapInfo.fields['dl_type'] = 0x800;
156         tapInfo.fields['nw_proto'] = 6;
157         tapInfo.fields['tp_port'] = 80;
158     }
159     else if (trafficType == 'HTTPS') {
160         tapInfo.fields['dl_type'] = 0x800;
161         tapInfo.fields['tp_port'] = 443;
162         tapInfo.fields['nw_proto'] = 6;
163     }
164     else if (trafficType == 'UDP') {
165         tapInfo.fields['dl_type'] = 0x800;
166         tapInfo.fields['nw_proto'] = 0x11;
167     }
168     else if (trafficType == 'DNS') {
169         tapInfo.fields['dl_type'] = 0x800;
170         tapInfo.fields['tp_port'] = 53;
171         tapInfo.fields['nw_proto'] = 0x11;
172     } else if (trafficType == 'DHCP') {
173         tapInfo.fields['dl_type'] = 0x800;
174         tapInfo.fields['tp_port'] = 67;
175         tapInfo.fields['nw_proto'] = 0x11;
176     } 
177     console.log(tapInfo.fields);
178
179     return tapInfo;
180 }
181
182 function restoreMain() {
183     $("#main").replaceWith(originalMain);
184     $('#post-status').html('');
185 }
186
187 function setTap() {
188     var tapInfo = makePostData();
189     if (tapInfo == undefined)
190         return;
191
192     $.post(url.concat("/v1.0/tap/create"), JSON.stringify(tapInfo), function() { 
193     }, "json")
194     .done(function() {
195         originalMain = $('#main').clone();
196         $('#post-status').html('');
197         $('#main').html('<h2>Tap created</h2><p>Successfully created tap. Check the <a href="/web/stats.html#flow">flow statistics</a> to verify that the rules have been created.</p><button class="pure-button pure-button-primary" onclick="restoreMain()">Create another tap</button>');
198     })
199     .fail(function() {
200         $('#post-status').html('<p style="color:red; background:silver;">Error: Tap creation failed. Please verify your input.');
201     });
202 }
203
204
205 function clearTap() {
206     var tapInfo = makePostData();
207     if (tapInfo == undefined)
208         return;
209
210     $.post(url.concat("/v1.0/tap/delete"), JSON.stringify(tapInfo), function() { 
211     }, "json")
212     .done(function() {
213         originalMain = $('#main').clone();
214         $('#post-status').html('');
215         $('#main').html('<h2>Tap deleted</h2><p>Successfully deleted tap. Check the <a href="/web/stats.html#flow">flow statistics</a> to verify that the rules have been deleted.</p><button class="pure-button pure-button-primary" onclick="restoreMain()">Create another tap</button>');
216     })
217     .fail(function() {
218         $('#post-status').html('<p style="color:red; background:silver;">Error: Tap deletion failed. Please verify your input.');
219     });
220 }
221
222 updateSwitchList();
223