Merge "cleanup NetvirtSfcIT"
[netvirt.git] / ovsdb-ui / module / src / main / resources / ovsdb / OvsCore.js
1 /*
2  * Copyright (c) 2015 Inocybe Technologies and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define(['underscore'], function (_) {
10
11   var Topology = (function () {
12
13     function Topology(topoId) {
14       this.topoId = topoId || '';
15       this._bridgeNodes = {};
16       this._ovsdbNodes = {};
17       this._links = {};
18     }
19
20     Object.defineProperties(Topology.prototype, {
21       bridgeNodes: {
22         get: function () {
23           return this._bridgeNodes;
24         }
25       },
26       nodes: {
27         get: function() {
28           return _.extend({}, this._bridgeNodes, this._ovsdbNodes);
29         }
30       },
31       ovsdbNodes: {
32         get: function () {
33           return this._ovsdbNodes;
34         }
35       },
36       links: {
37         get: function () {
38           return this._links;
39         }
40       }
41     });
42
43     Topology.prototype.registerBridgeNode = function (bridgeNode) {
44       this._bridgeNodes[bridgeNode.nodeId] = bridgeNode;
45     };
46
47     Topology.prototype.registerOvsdbNode = function (ovsdbNode) {
48       this._ovsdbNodes[ovsdbNode.nodeId] = ovsdbNode;
49     };
50
51     Topology.prototype.registerLink = function (link) {
52       if (this._links[link.linkId]) {
53         console.warn('Two links have the same id (' + link.linkId + '), the first one will be overrided');
54       }
55       this._links[link.linkId] = link;
56     };
57
58     Topology.prototype.updateLink = function () {
59       _.each(this._links, (function (link, key) {
60         if (link instanceof Link) {
61           srcNode = _.filter(this._bridgeNodes, function(node) {
62             return node.getFLowName() === link.srcNodeId;
63           });
64           destNode = _.filter(this._bridgeNodes, function(node) {
65             return node.getFLowName() === link.destNodeId;
66           });
67           link.srcNodeId = srcNode[0].nodeId;
68           link.destNodeId = destNode[0].nodeId;
69         }
70         link.source = Object.keys(this.nodes).indexOf(link.srcNodeId);
71         link.target = Object.keys(this.nodes).indexOf(link.destNodeId);
72
73       }).bind(this));
74     };
75
76     return Topology;
77   })();
78
79   var OvsNode = (function () {
80     function OvsNode(nodeId, inetMgr, inetNode, otherLocalIp, ovsVersion) {
81       this.nodeId = nodeId;
82       this.inetMgr = inetMgr;
83       this.inetNode = inetNode;
84       this.otherLocalIp = otherLocalIp;
85       this.ovsVersion = ovsVersion;
86     }
87
88     OvsNode.prototype.showIpAdress = function() {
89       return this.otherLocalIp;
90     };
91
92     OvsNode.prototype.pretty = function() {
93       return {
94         'tabs' : ['Info'],
95         'containts' : [ {
96           'hasHeader' : false,
97           'headers' : [],
98           'datas' : [
99             { key: 'ID', value: this.nodeId},
100             { key: 'InetMgr', value: this.inetMgr},
101             { key: 'InetNode', value: this.inetNode},
102             { key: 'Local IP', value: this.otherLocalIp},
103             { key: 'OVS Version', value: this.ovsVersion}
104           ]
105         }]
106       };
107     };
108
109     return OvsNode;
110   })();
111
112   var BridgeNode = (function () {
113
114     function BridgeNode(nodeId, dpIp, name, controllerTarget, controllerConnected) {
115       this.nodeId = nodeId;
116       this.dpIp = dpIp;
117       this.name = name;
118       this.controllerTarget = controllerTarget;
119       this.controllerConnected = controllerConnected;
120       this._tpList = [];
121       this.flowInfo = {};
122       this.flowTable = [];
123     }
124
125     Object.defineProperties(BridgeNode.prototype, {
126       tPs: {
127         get: function () {
128           return this._tpList;
129         }
130       },
131     });
132
133     var dpToFlow = function (dpId) {
134       return 'openflow:' + parseInt(dpId.replace(/:/g, ''), 16);
135     };
136
137     BridgeNode.prototype.getFLowName = function () {
138       return (!this.dpIp) ? this.nodeId : dpToFlow(this.dpIp);
139     };
140
141     BridgeNode.prototype.addTerminationPoint = function (tp) {
142       this._tpList.push(tp);
143       this._tpList.sort(function(tp1, tp2) {
144         return tp1.ofPort - tp2.ofPort;
145       });
146     };
147
148     BridgeNode.prototype.addFlowTableInfo = function(flowTable) {
149       this.flowTable.push(flowTable);
150       this.flowTable.sort(function(ft1, ft2) {
151         return ft1.key - ft2.key;
152       });
153     };
154
155     BridgeNode.prototype.pretty = function() {
156       return {
157         'tabs' : [
158           'Basic Info',
159           'Ports',
160           'Flow Info',
161           'Flow Tables'
162         ],
163         'containts' : [
164           {
165             'hasHeader' : false,
166             'headers' : [],
167             'datas' : [
168               { key: 'ID', value: this.nodeId},
169               { key: 'Name', value: this.name},
170               { key: 'OpenFlow Name', value: this.getFLowName()},
171               { key: 'Controller Target', value: this.controllerTarget},
172               { key: 'Controller Connected', value: this.controllerConnected}
173             ]
174           },
175           {
176             'hasHeader' : true,
177             'header' : ['Of Port', 'Name', 'Mac', 'IFace Id',],
178             'datas' : this._tpList.map(function(s) {
179               return [s.ofPort, s.name, s.mac, s.ifaceId];
180             })
181           },
182           {
183               'hasHeader' : false,
184               'headers' : [],
185               'datas': [
186                 {key : 'Manufacturer', value: this.flowInfo.manufacturer},
187                 {key : 'Hardware', value: this.flowInfo.hardware},
188                 {key : 'Software', value: this.flowInfo.software},
189                 {key : 'Feature', value: this.flowInfo.features},
190                 {key : 'Ip', value: this.flowInfo.ip}
191               ]
192           },
193           {
194             'hasHeader' : true,
195             'headers' : ['Table Id', 'Value'],
196             'datas' :this.flowTable.map(function(t) {
197               return [t.key, t.value];
198             })
199           }
200         ]
201       };
202     };
203
204     return BridgeNode;
205   })();
206
207   var TerminationPoint = (function () {
208     function TerminationPoint(name, ofPort, tpType, mac, ifaceId) {
209       this.name = name;
210       this.ofPort = ofPort;
211       this.tpType = tpType;
212       this.mac = mac || '';
213       this.ifaceId = ifaceId || '';
214     }
215     return TerminationPoint;
216   })();
217
218   var BaseLink = (function() {
219     function BaseLink(linkId, srcNodeId, destNodeId, linkType, styles) {
220       this.linkId = linkId;
221       this.srcNodeId = srcNodeId;
222       this.destNodeId = destNodeId;
223       this.linkType  = linkType;
224
225       // styling
226       styles = _.extend({}, styles);
227       this.color = styles.color;
228       this.width = styles.width || 1;
229       this.dashArray = styles.dashArray || 'None';
230
231       // d3js needed values
232       this.source = -1;
233       this.target = -1;
234     }
235     return BaseLink;
236   })();
237
238   var Link = (function () {
239     function Link(linkId, srcNodeId, destNodeId) {
240       var opt = {
241         color: 'black'
242       };
243
244       BaseLink.call(this, linkId, srcNodeId, destNodeId, 'link', opt);
245     }
246
247     Link.prototype = Object.create(BaseLink.prototype);
248     Link.prototype.constructor = Link;
249
250     return Link;
251   })();
252
253   var TunnelLink = (function() {
254     function TunnelLink(linkId, srcNodeId, destNodeId, linkType, color) {
255       var opt = {
256         color: 'green',
257         width: 2,
258         dashArray: '5,5'
259       };
260       BaseLink.call(this, linkId, srcNodeId, destNodeId, 'tunnel', opt);
261     }
262
263     TunnelLink.prototype = Object.create(BaseLink.prototype);
264     TunnelLink.prototype.constructor = TunnelLink;
265
266     return TunnelLink;
267   })();
268
269   var BridgeOvsLink = (function() {
270     function BridgeOvsLink(linkId, srcNodeId, destNodeId, linkType, color) {
271       var opt = {
272         color: 'gray',
273         dashArray: '10,10'
274       };
275       BaseLink.call(this, linkId, srcNodeId, destNodeId, 'bridgeOvsLink', opt);
276     }
277
278     BridgeOvsLink.prototype = Object.create(BaseLink.prototype);
279     BridgeOvsLink.prototype.constructor = BridgeOvsLink;
280
281     return BridgeOvsLink;
282   })();
283
284   var Util = (function() {
285     var Maths = (function() {
286       function Maths() {
287
288       }
289       // random function in javascript use timespan only
290       Maths.Random = function(nseed) {
291         var constant = Math.pow(2, 13)+1,
292           prime = 1987,
293           maximum = 1000;
294
295           if (nseed) {
296             seed = nseed;
297           }
298
299           return {
300             next : function(min, max) {
301               seed *= constant;
302               seed += prime;
303
304               return min && max ? min+seed%maximum/maximum*(max-min) : seed%maximum/maximum;
305             }
306           };
307       };
308       return Maths;
309     })();
310
311     var String = (function() {
312
313       function String() {
314
315       }
316       String.Format = function() {
317         var s = arguments[0];
318         for (var i = 0; i < arguments.length - 1; i++) {
319             var reg = new RegExp("\\{" + i + "\\}", "gm");
320             s = s.replace(reg, arguments[i + 1]);
321         }
322         return s;
323       };
324
325       return String;
326
327     })();
328     return {
329       Math: Maths,
330       String: String
331     };
332   })();
333
334   var Neutron = (function() {
335
336     var SubNet = (function() {
337       function SubNet(id, networkId, name, ipVersion, cidr, gatewayIp, tenantId) {
338         this.id = id;
339         this.networkId = networkId;
340         this.name = name;
341         this.ipVersion = ipVersion;
342         this.cidr = cidr;
343         this.gatewayIp = gatewayIp;
344         this.tenantId = tenantId;
345       }
346       return SubNet;
347     })();
348
349     var Network = (function() {
350       function Network(id, name, shared, status, external, tenantId) {
351         this.id = id;
352         this.ip = '';
353         this.name = name;
354         this.shared = shared;
355         this.status = status;
356         this.external = external;
357         this.tenantId = tenantId;
358         this.subnets = [];
359         this.instances = [];
360         this.routers = [];
361       }
362
363       Network.prototype.addSubNets = function(subnets) {
364         if(subnets) {
365           if (_.isArray(subnets)) {
366             var i = 0;
367             for (; i < subnets.length; ++i) {
368               this.subnets.push(subnets[i]);
369             }
370           }
371           else {
372             this.subnets.push(subnet);
373           }
374         }
375       };
376
377       Network.prototype.asSubnet = function(subnet) {
378         return _.every(subnet, function(sub) {
379           return _.some(this.subnets, function(s) {
380             return s.id === sub;
381           });
382         }.bind(this));
383       };
384
385       Network.prototype.pretty = function() {
386         return {
387           'tabs' : [
388             'Info',
389             'Subnets'
390           ],
391           'containts' : [
392             {
393               'hasHeader' : false,
394               'headers': [],
395               'datas' : [
396                 {key : 'ID', value: this.id},
397                 {key : 'Ip', value: this.ip},
398                 {key : 'Name', value: this.name},
399                 {key : 'Shared', value: this.shared},
400                 {key : 'Status', value: this.status},
401                 {key : 'External', value: this.external},
402                 {key : 'Tenant Id', value: this.tenantId}
403               ]
404             },
405             {
406               'hasHeader' : true,
407               'header' : ['ID', 'Name', 'Ip Version', 'Ip', 'Gateway Ip'],
408               'datas' : this.subnets.map(function(s) {
409                 return [s.id, s.name, s.ipVersion, s.cidr, s.gatewayIp];
410               })
411             }
412           ]
413         };
414       };
415
416       return Network;
417     })();
418
419     var Port = (function() {
420       function Port(id, networkId, name, tenantId, deviceId, deviceOwner, fixed_ips, mac) {
421         this.id = id;
422         this.networkId = networkId;
423         this.name = name;
424         this.tenantId = '' + tenantId || '';
425         this.deviceId = deviceId;
426         this.deviceOwner = deviceOwner;
427         this.fixed_ips = fixed_ips;
428         this.mac = mac;
429       }
430
431       Port.prototype.pretty = function() {
432         return [
433           { key: 'ID', value : this.id },
434           { key: 'Name', value: name },
435           { key: 'Tenant Id', value : this.tenantId },
436           { key: 'Device Id', value : this.deviceId },
437           { key: 'Device Owner', value : this.deviceOwner },
438           { key: 'MAC', value : this.mac }
439         ];
440       };
441
442       return Port;
443     })();
444
445     var Router = (function() {
446       function Router(id, name, status, tenantId, externalGateway) {
447         this.id = id;
448         this.name = name;
449         this.status = status;
450         this.tenantId = tenantId;
451         this.interfaces = [];
452         this.externalGateway = externalGateway;
453       }
454
455       Router.prototype.pretty = function() {
456         return {
457           'tabs' : [
458             'Info',
459             'Interfaces'
460           ],
461           'containts' : [
462             {
463               'hasHeader' : false,
464               'headers': [],
465               'datas' : [
466                 { key: 'ID', value: this.id},
467                 { key: 'Name', value: this.name},
468                 { key: 'status', value: this.status},
469                 { key: 'Tenant ID', value: this.tenantId}
470               ]
471             },
472             {
473               'hasHeader' : true,
474               'header' : ['ID', 'Type', 'Mac Address', 'Ip', 'Tenant Id'],
475               'datas' : this.interfaces.map(function(s) {
476                 return [s.id, s.type, s.mac, s.ip.ip_address, s.tenantId];
477               })
478             }
479           ]
480         };
481       };
482
483       return Router;
484     })();
485
486     var Instance = (function() {
487       function Instance(id, networkId, name, ip, mac, deviceOwner, tenantId, topoInfo) {
488         this.id = id;
489         this.networkId = networkId;
490         this.name = name;
491         this.ip = ip;
492         this.mac = '' + mac;
493         this.type = deviceOwner;
494         this.tenantId = tenantId;
495         this.topoInfo = topoInfo || [];
496         this.floatingIp = {};
497       }
498
499       Instance.prototype.extractFloatingIps = function(floatingIps) {
500         var ctx = this;
501         this.floatingIp = _.find(floatingIps, function(fIp) {
502           return fIp.tenantId === ctx.tenantId &&
503             fIp.fixedIp === ctx.ip;
504         });
505       };
506
507       Instance.prototype.pretty = function() {
508         return {
509           'tabs' : [
510             'Info',
511             'Ports'
512           ],
513           'containts' : [
514             {
515               'hasHeader' : false,
516               'headers': [],
517               'datas' : [
518                 { key: 'ID', value: this.id},
519                 { key: "Network Id", value : this.networkId},
520                 { key: 'Name', value: this.name},
521                 { key: 'Ip', value: this.ip},
522                 { key: 'Floating Ip', value: (this.floatingIp) ? this.floatingIp.ip : 'Not found' },
523                 { key: 'MAC', value: this.mac},
524                 { key: 'Type', value: this.type},
525                 { key: 'Tenant ID', value: this.tenantId}
526               ]
527             },
528             {
529               'hasHeader' : true,
530               'header' : ['Name', 'Of Port', 'Mac', 'Flow', 'Ovsdb Node', 'Ovsdb Node IP'],
531               'datas' : this.topoInfo.map(function(s) {
532                 return [s.name, s.ofPort, s.mac, s.bridge.getFLowName(), s.ovsNode.nodeId, s.ovsNode.showIpAdress()];
533               })
534             }
535           ]
536         };
537       };
538
539       return Instance;
540     })();
541
542     var FloatingIp =(function() {
543       function FloatingIp(id, networkId, portId, fixedIp, floatingIp, tentantId, status) {
544         this.id = id;
545         this.networkId = networkId;
546         this.portId = portId;
547         this.fixedIp = fixedIp;
548         this.ip = floatingIp;
549         this.tenantId = tentantId;
550         this.status = status;
551       }
552
553       return FloatingIp;
554     })();
555
556     return {
557       Network: Network,
558       Port: Port,
559       Instance: Instance,
560       FloatingIp: FloatingIp,
561       Router: Router,
562       SubNet: SubNet
563     };
564   })();
565
566   return {
567     OvsNode: OvsNode,
568     BridgeNode: BridgeNode,
569     TerminationPoint: TerminationPoint,
570     Topology: Topology,
571     BaseLink: BaseLink,
572     Link: Link,
573     TunnelLink: TunnelLink,
574     BridgeOvsLink:BridgeOvsLink,
575     Util:Util,
576     Neutron: Neutron
577   };
578 });