table on topology views add and start router controller
[VSoRC/.git] / js / topology / main.js
1 // Copyright (c) 2018 Maen Artimy\r
2 // \r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 // \r
7 //   http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 /**\r
16  * All global objects are here.\r
17  *\r
18  **/\r
19 var myGlobalObject = (function () {\r
20         console.log("myGlobalObject");\r
21         var dpid = 1; //current datapath (switch) ID\r
22         var myMasterURL = ""; //"http://localhost:8080";\r
23 \r
24         // returns full url for the command sent to the SDN controller\r
25         var url = function(command) {\r
26                 var cmd = command.replace("<dpid>",dpid); //dpidStr(dpid));\r
27                 return myMasterURL + cmd;\r
28         };\r
29 \r
30         // sends asynchronous request to the SDN controller\r
31         function httpGetAsync(command, callback, element) {\r
32                 //console.log("httpGetAsync");\r
33                 var COMPLETE = 4;\r
34                 var OK = 200;\r
35                 var NOTFOUND = 404;\r
36                 var UNAVAILABLE = 503;\r
37                 // this script is for sending a GET request to a server\r
38                 var xmlHttp = new XMLHttpRequest();\r
39                 xmlHttp.onreadystatechange = function() {\r
40                         if (xmlHttp.readyState == COMPLETE) {\r
41                                 if(xmlHttp.status == OK) {\r
42                                         callback(xmlHttp.responseText, element);\r
43                                 } else {\r
44                                         console.log(xmlHttp.status);\r
45                                 }\r
46                         }\r
47                 }\r
48                 xmlHttp.open("GET", url(command), true); // true for asynchronous\r
49                 xmlHttp.send(null);\r
50         }\r
51 \r
52         function httpPostAsync(command, parm, callback, element) {\r
53                 //console.log("httpPostAsync");\r
54                 var COMPLETE = 4;\r
55                 var OK = 200;\r
56                 var NOTFOUND = 404;\r
57                 var UNAVAILABLE = 503;\r
58                 // this script is for sending a GET request to a server\r
59                 var xmlHttp = new XMLHttpRequest();\r
60                 xmlHttp.onreadystatechange = function() {\r
61                         if (xmlHttp.readyState == COMPLETE) {\r
62                                 if(xmlHttp.status == OK) {\r
63                                         callback(xmlHttp.responseText, element);\r
64                                 } else {\r
65                                         console.log(xmlHttp.status);\r
66                                 }\r
67                         }\r
68                 }\r
69                 xmlHttp.open("POST", url(command), true); // true for asynchronous\r
70                 //xmlHttp.setRequestHeader("Content-Type", "application/json");\r
71                 xmlHttp.setRequestHeader("Content-Type", "plain/text");\r
72                 xmlHttp.send(parm);\r
73         }\r
74 \r
75         function dpidStr(dpid) {\r
76                 return ("000000000000000" + dpid.toString(16)).substr(-16);\r
77         }\r
78 \r
79         function setDPID(str) {\r
80                 dpid = parseInt(str);\r
81         }\r
82 \r
83         function getDPID() {\r
84                 return dpid;\r
85         }\r
86 \r
87         // these are the public objects\r
88         return {\r
89                 dpid: getDPID,\r
90                 setDPID: setDPID,\r
91                 httpGetAsync: httpGetAsync,\r
92                 httpPostAsync: httpPostAsync,\r
93         }\r
94 })();\r