b87a465b76e9f252ef29439375d1457383a68071
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnMediator / mwtnMediator-module / src / main / resources / mwtnMediator / class.mediator.js
1 function MediatorConfig(obj) {\r
2         if (obj !== undefined) {\r
3                 this.Name = obj.Name;\r
4                 this.DeviceType = obj.DeviceType;\r
5                 this.DeviceIp = obj.DeviceIp;\r
6                 this.DevicePort = 'DevicePort' in obj?obj.DevicePort:161;\r
7                 this.TrapsPort = obj.TrapPort;\r
8                 this.IsNetConfConnected = obj.IsNCConnected;\r
9                 this.IsNetworkElementConnected = obj.IsNeConnected;\r
10                 this.NeModel = obj.NeXMLFile;\r
11                 this.NetconfPort = obj.NcPort;\r
12                 this.PID = obj.pid;\r
13                 this.IsLocked = obj.islocked;\r
14                 this.Autorun = obj.autorun;\r
15                 this.FirewallRuleActive = obj.fwactive;\r
16                 this.OpenDaylightConfigs = obj.ODLConfig;\r
17         } else {\r
18                 this.Name ="";\r
19                 this.DeviceType = -1;\r
20                 this.DeviceIp = "";\r
21                 this.DevicePort = 161;\r
22                 this.TrapsPort = 0;\r
23                 this.IsNetConfConnected = false;\r
24                 this.IsNetworkElementConnected = false;\r
25                 this.NeModel = "";\r
26                 this.NetconfPort = 0;\r
27                 this.PID =0;\r
28                 this.IsLocked = false;\r
29                 this.Autorun = false;\r
30                 this.FirewallRuleActive = false;\r
31                 this.OpenDaylightConfigs=[];\r
32         }\r
33         this.DeviceTypeString = this.getDeviceTypeString();\r
34         this.ConnectionStatus = {Netconf:this.IsNetConfConnected,NetworkElement:this.IsNetworkElementConnected}\r
35 \r
36 }\r
37 MediatorConfig.prototype.refreshData = function(obj)\r
38 {\r
39         if(obj!==undefined)\r
40         {\r
41                 //this.Name = obj.Name;\r
42                 this.DeviceType = obj.DeviceType;\r
43                 this.DeviceIp = obj.DeviceIp;\r
44                 this.DevicePort = 'DevicePort' in obj?obj.DevicePort:161;\r
45                 this.TrapsPort = obj.TrapPort;\r
46                 this.IsNetConfConnected = obj.IsNCConnected;\r
47                 this.IsNetworkElementConnected = obj.IsNeConnected;\r
48                 this.NeModel = obj.NeXMLFile;\r
49                 this.NetconfPort = obj.NcPort;\r
50                 this.PID = obj.pid;\r
51                 this.IsLocked = obj.islocked;\r
52                 this.Autorun = false;\r
53                 this.FirewallRuleActive = obj.fwactive;\r
54                 this.OpenDaylightConfigs = obj.ODLConfig;\r
55         }\r
56         this.DeviceTypeString = this.getDeviceTypeString();\r
57         this.ConnectionStatus = {Netconf:this.IsNetConfConnected,NetworkElement:this.IsNetworkElementConnected}\r
58 \r
59 }\r
60 MediatorConfig.prototype.getDeviceTypeString = function()\r
61 {\r
62         var i;\r
63         for(i=0;i<MediatorConfig.DeviceTypes.length;i++)\r
64         {\r
65                 if(MediatorConfig.DeviceTypes[i].Value==this.DeviceType)\r
66                         return MediatorConfig.DeviceTypes[i].Name;\r
67         }\r
68         return "unknown";\r
69 }\r
70 /* enum for devicetypes */\r
71 MediatorConfig.DEVICETYPE_SIMULATOR =0;\r
72 \r
73 \r
74 function MediatorConfigStatus(obj){\r
75         this.Status=obj.Status;\r
76         this.Name=obj.Name;\r
77 };\r
78 MediatorConfigStatus.STATUS_OKAY = 1;\r
79 MediatorConfigStatus.STATUS_CORRUPTED = 2;\r
80 MediatorConfigStatus.STATUS_LOCKED = 3;\r
81 MediatorConfigStatus.STATUS_REPAIRED = 4;\r
82 MediatorConfigStatus.StatusTypes=[\r
83         {Value:MediatorConfigStatus.STATUS_OKAY,Name:"Okay"},\r
84         {Value:MediatorConfigStatus.STATUS_CORRUPTED,Name:"Corrupted"},\r
85         {Value:MediatorConfigStatus.STATUS_LOCKED,Name:"Locked"},\r
86         {Value:MediatorConfigStatus.STATUS_REPAIRED,Name:"Repaired"}\r
87 ];\r
88 \r
89 /* Names for enum for devicetypes */\r
90 MediatorConfig.DeviceTypes=[\r
91 {Value:MediatorConfig.DEVICETYPE_SIMULATOR,Name:"Simulator"}];\r
92 \r
93 MediatorConfig.prototype.IsRunning = function() {\r
94         return this.PID > 0;\r
95 }\r
96 MediatorConfig.prototype.TestParams = function() {\r
97         if(this.Name===undefined || this.Name.length<=0)\r
98                 throw "Name is not given";\r
99         // Name without spaces\r
100         var inValidName = /\s/;\r
101         if(inValidName.test(this.Name))\r
102                 throw "Name cannot have whitespaces";\r
103         // DeviceType: int from 0 to ...\r
104         if(this.DeviceType<0 || this.DeviceType>MediatorConfig.DeviceTypes[MediatorConfig.DeviceTypes.length-1].Value)\r
105                 throw "DeviceType is not set";\r
106         // DeviceIp: valid IP-Address\r
107         var validIpTest =/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;\r
108         if(!validIpTest.test(this.DeviceIp))\r
109                 throw "IP-Address is not valid";\r
110         // TrapsPort: valid Port\r
111         if(this.TrapsPort<=0 || this.TrapsPort>65535)\r
112                 throw "TrapsPort is not valid";\r
113         // NeModel: valid XMLFilename\r
114         if(this.NeModel===undefined || this.NeModel.length<=0)\r
115                 throw "NE XML Filename is not valid";\r
116         // NetconfPort: valid Port\r
117         if(this.NetconfPort<=0 || this.NetconfPort>65535)\r
118                 throw "Netconf Port is not valid";\r
119 \r
120         return true;\r
121 }\r
122 function PortRange(a)\r
123 {\r
124         if(a!==undefined)\r
125         {\r
126                 this.Min=a[0];\r
127                 this.Max=a[1];\r
128         }\r
129         else\r
130         {\r
131                 this.Min=0;\r
132                 this.Max=0;\r
133         }\r
134 }\r
135 function JavaMemParam(str)\r
136 {\r
137         if(str!==undefined)\r
138         {\r
139 \r
140 \r
141         }\r
142 }\r
143 function ServerConfig(obj){\r
144         if (obj !== undefined) {\r
145                 this.HomeDir = obj.home;\r
146                 this.Host = obj.host;\r
147                 this.Port = obj.port;\r
148                 this.NetconfRange=new PortRange(obj.ncrange);\r
149                 this.SnmpRange = new PortRange(obj.snmprange);\r
150                 this.JmxRange = new PortRange(obj.jmxrange);\r
151                 this.LogLevel = obj.loglevel;\r
152                 this.LogFile = obj.logfile;\r
153                 this.MediatorLogLevel = obj.mediator-loglevel;\r
154                 this.MediatorDevicePingTimeout=obj.mediator-devicepingtimeout;\r
155                 this.MediatorSnmpLatency = obj.mediator-snmplatency;\r
156                 this.MediatorMemory = new JavaMemParam(obj.mediator-memory);\r
157         }\r
158         else\r
159         {\r
160                 this.HomeDir = "";\r
161                 this.Host = "";\r
162                 this.Port = 0;\r
163                 this.NetconfRange=new PortRange();\r
164                 this.SnmpRange = new PortRange();\r
165                 this.JmxRange = new PortRange();\r
166                 this.LogLevel = "";\r
167                 this.LogFile = "";\r
168                 this.MediatorLogLevel = "";\r
169                 this.MediatorDevicePingTimeout=0;\r
170                 this.MediatorSnmpLatency = 0;\r
171                 this.MediatorMemory = new JavaMemParam();\r
172         }\r
173 }\r
174 function MediatorServer(url) {\r
175         this._root = url;\r
176         // if(this._root.endsWith("/"))\r
177         this._root = this._root + "/";\r
178         this._mediatorConfigs = [];\r
179         this._neXMLFilenames = undefined;\r
180         this.defaultODLConfig = {Server:"sendateodl5.fritz.box",Port:8181,User:"admin",Password:"admin"};\r
181 \r
182 }\r
183 MediatorServer.prototype.getConfigs = function(){return this._mediatorConfigs;}\r
184 \r
185 MediatorServer.prototype.SetDefaultODLConfig = function(cfg)\r
186 {\r
187         this.defaultODLConfig = cfg;\r
188 }\r
189 MediatorServer.prototype.GetDefaultODLConfig = function()\r
190 {\r
191         return this.defaultODLConfig;\r
192 }\r
193 MediatorServer.prototype.refreshConfig = function(configs,cb)\r
194 {\r
195         var changed = [];\r
196         if(this._mediatorConfigs===undefined)\r
197         {\r
198                 if(cb!==undefined)\r
199                         cb(changed);\r
200                 return;\r
201         }\r
202         if(configs!==undefined && configs.length>0)\r
203         {\r
204                 var i,j;\r
205                 for(i=0;i<configs.length;i++)\r
206                 {\r
207                         //find config in array by name\r
208                         for(j=0;j<this._mediatorConfigs.length;j++)\r
209                         {\r
210                                 if(this._mediatorConfigs[j].Name==configs[i].Name)\r
211                                 {\r
212                                         //refresh data\r
213                                         this._mediatorConfigs[j].refreshData(configs[i]);\r
214                                         changed.push(configs[i]);\r
215                                         break;\r
216                                 }\r
217                         }\r
218                 }\r
219         }\r
220         if(cb!==undefined)\r
221                 cb(changed);\r
222 }\r
223 MediatorServer.prototype.onConfigsReceived = function(configJSONArray) {\r
224         this._mediatorConfigs = [];\r
225         for (var i = 0; i < configJSONArray.length; i++) {\r
226                 var c = new MediatorConfig(configJSONArray[i]);\r
227                 this._mediatorConfigs.push(c);\r
228         }\r
229 }\r
230 MediatorServer.prototype.onNeXMLReceived = function(neXMLFilenamesArray) {\r
231         this._neXMLFilenames = neXMLFilenamesArray;\r
232 }\r
233 \r
234 MediatorServer.prototype.LoadNetworkElementXMLFiles = function(cb,cbError) {\r
235         var _self = this;\r
236         this.post("getnemodels", function(response) {\r
237                 if (response.code == 1) {\r
238                         _self.onNeXMLReceived(response.data);\r
239                         if (cb !== undefined)\r
240                                 cb(_self._neXMLFilenames);\r
241                 } else\r
242                 {\r
243                         _self.log(response.data);\r
244                         if(cbError!==undefined)\r
245                                 cbError(response.data);\r
246                 }\r
247         },function(err){\r
248                 if(cbError!==undefined)\r
249                         cbError(err);\r
250         });\r
251 }\r
252 MediatorServer.prototype.LoadLogs = function(name,cb,cbError)\r
253 {\r
254         var _self = this;\r
255         this.post("getlog&name="+name,function(response){\r
256                 if (response.code == 1) {\r
257                         if (cb !== undefined)\r
258                                 cb(response.data);\r
259                 } else\r
260                 {       _self.log(response.data);\r
261                         if(cbError!==undefined)\r
262                                 cbError(response.data);\r
263                 }\r
264         },function(err){\r
265                 if(cbError!==undefined)\r
266                         cbError(err);\r
267         });\r
268 }\r
269 MediatorServer.prototype.LoadAvailableNCPorts = function(cb,cbError)\r
270 {\r
271         var _self = this;\r
272         this.post("getncports",function(response){\r
273                 if (response.code == 1) {\r
274                         if (cb !== undefined)\r
275                                 cb(response.data);\r
276                 } else\r
277                 {       _self.log(response.data);\r
278                         if(cbError!==undefined)\r
279                                 cbError(response.data);\r
280                 }\r
281         },function(err){\r
282                 if(cbError!==undefined)\r
283                         cbError(err);\r
284         });\r
285 }\r
286 MediatorServer.prototype.LoadAvailableSnmpPorts = function(cb,cbError)\r
287 {\r
288         var _self = this;\r
289         this.post("getsnmpports",function(response){\r
290                 if (response.code == 1) {\r
291                         if (cb !== undefined)\r
292                                 cb(response.data);\r
293                 }\r
294                 else\r
295                 {\r
296                         _self.log(response.data);\r
297                         if(cbError!==undefined)\r
298                                 cbError(response.data);\r
299                 }\r
300         },function(err){\r
301                 if(cbError!==undefined)\r
302                         cbError(err);\r
303         });\r
304 }\r
305 MediatorServer.prototype.StartMediator = function(name, cb, cbError) {\r
306         var _self = this;\r
307         this.post("start",{name:name}, function(response) {\r
308                 if (response.code == 1) {\r
309                         if (cb !== undefined)\r
310                                 cb(response.data);\r
311                 } else {\r
312                         _self.log(response.data);\r
313                         if(cbError!==undefined)\r
314                                 cbError(response.data);\r
315                 }\r
316         },function(err){\r
317                 if(cbError!==undefined)\r
318                         cbError(err);\r
319         });\r
320 }\r
321 MediatorServer.prototype.StopMediator = function(name, cb, cbError) {\r
322         var _self = this;\r
323         this.post("stop",{name:name}, function(response) {\r
324                 if (response.code == 1) {\r
325                         if (cb !== undefined)\r
326                                 cb(response.data);\r
327                 } else {\r
328                         _self.log(response.data);\r
329                         if(cbError!==undefined)\r
330                                 cbError(response.data);\r
331                 }\r
332         },function(err){\r
333                 if(cbError!==undefined)\r
334                         cbError(err);\r
335         });\r
336 }\r
337 MediatorServer.prototype.ReloadConfig = function(name,cb,cbError)\r
338 {\r
339         var _self = this;\r
340         this.post("getconfig",{name:name},function(response){\r
341                 if (response.code == 1) {\r
342                         _self.refreshConfig(response.data,function(changes){\r
343                                 if (cb !== undefined)\r
344                                         cb(changes);\r
345                         });\r
346                 } else {\r
347                         _self.log(response.data);\r
348                         if(cbError!==undefined)\r
349                                 cbError(response.data);\r
350                 }\r
351 \r
352         },function(err){\r
353                 if(cbError!==undefined)\r
354                         cbError(err);\r
355         });\r
356 \r
357 }\r
358 MediatorServer.prototype.LoadConfigs = function(cb,cbError) {\r
359         var _self = this;\r
360         this.post("getconfig", function(response) {\r
361                 if (response.code == 1) {\r
362                         _self.onConfigsReceived(response.data);\r
363                         if (cb !== undefined)\r
364                                 cb(_self._mediatorConfigs);\r
365                 } else {\r
366                         _self.log(response.data);\r
367                 }\r
368 \r
369         },function(err){\r
370                 if(cbError!==undefined)\r
371                         cbError(err);\r
372         });\r
373 }\r
374 MediatorServer.prototype.Repair = function(cb,cbError)\r
375 {\r
376         var _self = this;\r
377         this.post("repair", function(response) {\r
378                 if (response.code == 1) {\r
379                         if (cb !== undefined)\r
380                                 cb(response.data);\r
381                 } else {\r
382                         _self.log(response.data);\r
383                         if(cbError!==undefined)\r
384                                 cbError(response.data);\r
385                 }\r
386         },function(err){\r
387                 if(cbError!==undefined)\r
388                         cbError(err);\r
389         });\r
390 }\r
391 MediatorServer.prototype.LoadVersion = function(cb,cbError)\r
392 {\r
393         var _self = this;\r
394         this.post("version", function(response) {\r
395                 if (response.code == 1) {\r
396                         if (cb !== undefined)\r
397                                 cb(response.data);\r
398                 } else {\r
399                         _self.log(response.data);\r
400                 }\r
401 \r
402         },function(err){\r
403                 if(cbError!==undefined)\r
404                         cbError(err);\r
405         });\r
406 }\r
407 /*\r
408  * name: <String> deviceType: <int> deviceip: <Ipv4-String> trapsPort:<int>\r
409  * nexml:<String> ncport:<int> cb: callback-function\r
410  */\r
411 MediatorServer.prototype.CreateMediator = function(name,devicetype,deviceip,deviceport,trapsPort,nexml,ncport, cb, cbError) {\r
412 \r
413         var obj;\r
414         try\r
415         {\r
416                 obj=new MediatorConfig({\r
417                         Name:name,\r
418                         DeviceType:devicetype,\r
419                         DeviceIp:deviceip,\r
420                         DevicePort:deviceport,\r
421                         TrapPort:trapsPort,\r
422                         NeXMLFile:nexml,\r
423                         NcPort:ncport\r
424                 });\r
425                 obj.TestParams();\r
426         }\r
427         catch(e)\r
428         {\r
429                 if(cbError!==undefined)\r
430                         cbError(e);\r
431         }\r
432         this.post("create", {\r
433                 config : JSON.stringify({\r
434                         Name : obj.Name,\r
435                         DeviceType : obj.DeviceType,\r
436                         DeviceIp : obj.DeviceIp,\r
437                         DevicePort : obj.DevicePort,\r
438                         TrapPort : obj.TrapsPort,\r
439                         NeXMLFile : obj.NeModel,\r
440                         NcPort : obj.NetconfPort,\r
441                         ODLConfig:[this.defaultODLConfig]\r
442                 })\r
443         }, function(response) {\r
444                 if(response.code==1)\r
445                 {\r
446                         if(cb!==undefined)\r
447                                 cb(true)\r
448                 }\r
449                 else\r
450                 {\r
451                         if(cbError!==undefined)\r
452                                 cbError(response.data);\r
453                 }\r
454         },function(err){\r
455                 if(cbError!==undefined)\r
456                         cbError(err);\r
457         });\r
458 }\r
459 MediatorServer.prototype.DeleteMediator = function(name, cb, cbError)\r
460 {\r
461         var _self = this;\r
462         this.post('delete', {name:name}, function(response){\r
463                 if(response.code==1)\r
464                 {\r
465                         if(cb!==undefined)\r
466                                 cb(true);\r
467                 }\r
468                 else\r
469                 {\r
470                         if(cb!==undefined)\r
471                                 cb(response.data);\r
472                 }\r
473 \r
474         },function(err){\r
475                 if(cbError!==undefined)\r
476                         cbError(err);\r
477         });\r
478 }\r
479 MediatorServer.prototype.ClearLock = function(name, cb, cbError) {\r
480         var _self = this;\r
481         this.post("clearlock", {\r
482                 name : name\r
483         }, function(response) {\r
484                 if (response.code == 1) {\r
485                         if (cb !== undefined)\r
486                                 cb(true);\r
487                 } else {\r
488                         _self.log(response.data);\r
489                         if (cbError !== undefined)\r
490                                 cbError(response.data);\r
491                 }\r
492         },function(err){\r
493                 if(cbError!==undefined)\r
494                         cbError(err);\r
495         });\r
496 }\r
497 /*\r
498  * do post request\r
499  *\r
500  * @params task\r
501  */\r
502 MediatorServer.prototype.post = function(task, data, callback,callbackError) {\r
503         var _self = this;\r
504         if (typeof (data) === "function")\r
505         {\r
506                 callbackError = callback;\r
507                 callback = data;\r
508         }\r
509         var cb=function(r) {\r
510                                 if (callback !== undefined) {\r
511                                         try {\r
512                                                 if (typeof (r) === "string")\r
513                                                         r = JSON.parse(r);\r
514                                                 callback(r);\r
515                                         } catch (e) {\r
516                                                 _self.log(e);\r
517                                                 if(callbackError!==undefined)\r
518                                                         callbackError(e);\r
519                                         }\r
520                                 }\r
521                         };\r
522 \r
523         if(this.usejQueryv3)\r
524         {\r
525                 $.post({\r
526                         url : this._root + "api/?task=" + task,\r
527                         data : data,\r
528                         success : cb\r
529                 });\r
530         }\r
531         else //jquery 1.x\r
532         {\r
533                 if(typeof(data)=== "function")\r
534                 {\r
535                         $.post(this._root + "api/?task=" + task,cb).done(function() {\r
536                                 _self.log( "second success" );\r
537                           })\r
538                           .fail(function(e) {\r
539                                   _self.log( "error" +e);\r
540                                   if(callbackError!==undefined)\r
541                                           callbackError(e);\r
542                           })\r
543                           .always(function() {\r
544                                   _self.log( "finished" );\r
545                           });\r
546                 }\r
547                 else\r
548                 {\r
549                         $.post(this._root + "api/?task=" + task,data,cb).done(function() {\r
550                                 _self.log( "second success" );\r
551                           })\r
552                           .fail(function(e) {\r
553                                   _self.log( "error" +e);\r
554                                   if(callbackError!==undefined)\r
555                                           callbackError(e);\r
556                           })\r
557                           .always(function() {\r
558                                   _self.log( "finished" );\r
559                           });\r
560                 }\r
561         }\r
562 }\r
563 \r
564 MediatorServer.prototype._error = function(message) {\r
565         console.log(message);\r
566 }\r
567 MediatorServer.prototype.log = function(message) {\r
568 //      console.log(message);\r
569 }