Initial commit of vBd Manager functions.
[honeycomb.git] / vbd / gui / module / src / main / resources / vpp / services / vpp.services.js
index 70b1631..4c20f84 100644 (file)
@@ -30,14 +30,15 @@ define(['app/vpp/vpp.module', 'next'], function(vpp) {
         }
     });
 
-    vpp.register.service('dataService', function() {
+    vpp.register.service('dataService', ['$timeout', function($timeout) {
+
+        var self = this;
 
         nx.graphic.Icons.registerIcon("bd", "src/app/vpp/assets/images/bd1.svg", 45, 45);
         nx.graphic.Icons.registerIcon("interf", "src/app/vpp/assets/images/interf.svg", 45, 45);
 
         this.bridgeDomainsTopo = new nx.graphic.Topology({
-            height: 350,
-            width: 500,
+            adaptive: true,
             scalable: true,
             theme:'blue',
             enableGradualScaling:true,
@@ -49,8 +50,6 @@ define(['app/vpp/vpp.module', 'next'], function(vpp) {
                     var type = vertex.get().type;
                     if (type === 'bd') {
                         return 'bd'
-                    } else if (type === 'vpp') {
-                        return 'switch'
                     } else {
                         return 'interf';
                     }
@@ -82,11 +81,11 @@ define(['app/vpp/vpp.module', 'next'], function(vpp) {
             }
         });
         this.nextApp =  new nx.ui.Application;
+        this.bridgedomainsLoaded = false;
 
         this.vpps = [];
-
         this.tableContent = [];
-        this.unassignedInterfaces = [];
+        this.originalAssignments = [];
         this.interfaces = [];
         this.injectedInterfaces = [];
         this.bridgedomains = [];
@@ -95,69 +94,188 @@ define(['app/vpp/vpp.module', 'next'], function(vpp) {
             name: ''
         };
 
-        this.setBridgeDomains = function(data) {
-            angular.copy(data['bridge-domains']['bridge-domain'], this.bridgedomains);
+        this.generateInterfaces = function() {
+            self.interfaces.length = 0;
+            _.forEach(this.vpps, function(vpp) {
+                _.forEach(vpp.interfaces, function(interf) {
+                    interf.vppName = vpp.name;
+                    interf.label = vpp.name+'/'+interf.name;
+                    interf.scale = 0.5;
+                    self.interfaces.push(interf);
+                });
+            });
+            console.log(this.interfaces);
         };
 
-        this.clearInjectedInterfacesInBridgeDomainTopo = function() {
-            this.bridgeDomainsTopo.clear();
-            this.injectedInterfaces.length = 0;
+        this.buildAssignedInterfaces = function() {
+            this.originalAssignments.length = 0;
+            _.forEach(this.bridgedomains, function(bd){
+                var bdName = bd['topology-id'];
+                var nodes = bd.node;
+                if (nodes) {
+                    _.forEach(nodes, function(vpp) {
+                        var vppName = vpp['node-id'];
+                        var tps = vpp['termination-point'];
+                        if (tps) {
+                            _.forEach(tps, function(tp) {
+                                tp.vppName = vppName;
+                                tp.vbd = bdName;
+                                self.originalAssignments.push(tp);
+                            })
+                        }
+                    })
+                }
+            });
 
+            console.log('Assigned Interfaces: ');
+            console.log(this.originalAssignments);
         };
 
-        this.generateUnassignedInterfaces = function() {
-            this.unassignedInterfaces.length = 0;
-            for (var x=0; x<this.interfaces.length; x++) {
-                if (!this.interfaces[x]['v3po:l2']['bridge-domain']) {
-                    this.unassignedInterfaces.push(this.interfaces[x]);
+        this.buildTableContent = function() {
+            this.tableContent.length = 0;
+            angular.copy(this.interfaces,this.tableContent);
+
+
+            //Makes assignements based on previously changed interfaces, or assignments retrieved from ODL
+            _.forEach(this.tableContent, function(interf) {
+                var matchedChangedInterface = _.find(self.changedInterfaces, {
+                    name: interf.name,
+                    vppName: interf.vppName
+                });
+
+                var matchedOriginalAssignment = _.find(self.originalAssignments, {
+                    'vbridge-topology:user-interface': interf.name,
+                    vppName: interf.vppName
+                });
+
+                if (matchedChangedInterface) {
+                    interf.assigned = matchedChangedInterface.assigned;
+                    interf.vbd = matchedChangedInterface.vbd;
+                } else if (matchedOriginalAssignment) {
+                    interf.assigned = true;
+                    interf.vbd = matchedOriginalAssignment.vbd;
+                } else {
+                    interf.assigned = false;
+                    interf.vbd = '';
                 }
-            }
-        };
+            });
+
+            _.remove(self.tableContent, function(interf){
+                var isAssigned = interf.assigned === true;
+                var isNotCorrectBd = !(interf.vbd === self.selectedBd.name);
+                return(isAssigned && isNotCorrectBd);
+            });
+
+            //_.forEach(this.originalAssignments, function(origAssignment) {
+            //    if (origAssignment.vbd === self.selectedBd.name) {
+            //        var matchedInterface = _.find(self.tableContent, {
+            //            name: origAssignment['vbridge-topology:user-interface'],
+            //            vppName: origAssignment.vppName
+            //        });
+            //        if (matchedInterface) {
+            //            matchedInterface.assigned = true;
+            //            matchedInterface.vbd = origAssignment.vbd;
+            //        } else {
+            //            console.error('Interface "'+origAssignment['vbridge-topology:user-interface']+'" on VPP "'+origAssignment.vppName+'" in vBD "'+origAssignment.vbd+'" was not found at mount point!');
+            //        }
+            //    } else {
+            //        _.remove(self.tableContent, {
+            //            name: origAssignment['vbridge-topology:user-interface'],
+            //            vppName: origAssignment.vppName
+            //        });
+            //    }
+            //});
+            //
+            //_.forEach(this.changedInterfaces, function(changedInterface) {
+            //
+            //    var matchedInterface = _.find(self.tableContent, {
+            //        name: changedInterface.name,
+            //        vppName: changedInterface.vppName
+            //    });
+            //
+            //    if (matchedInterface) {
+            //        if (changedInterface.assigned) {
+            //            if (changedInterface.vbd === self.selectedBd.name) {
+            //                matchedInterface.assigned = true;
+            //                matchedInterface.vbd = changedInterface.vbd;
+            //            }
+            //            else {
+            //                _.remove(self.tableContent, {
+            //                    name: changedInterface.name,
+            //                    vppName: changedInterface.vppName
+            //                });
+            //            }
+            //        } else {
+            //            matchedInterface.assigned = false;
+            //            matchedInterface.vbd = '';
+            //        }
+            //    }
+            //});
+
+            //..
+
+            //_.remove(self.tableContent, {
+            //    name: origAssignment['vbridge-topology:user-interface'],
+            //    vppName: origAssignment.vppName
+            //});
+
+            this.injectBridgeDomainsTopoElements();
 
-        this.injectBridgeDomainsTopoElements = function() {
-            this.clearInjectedInterfacesInBridgeDomainTopo();
-            this.injectBdIntoBridgeDomainsTopo();
-            this.injectInterfacesIntoBridgeDomainsTopo();
-            this.injectInterfacesLinksIntoBridgeDomainsTopo();
-            this.bridgeDomainsTopo.adaptToContainer();
         };
 
-        this.buildTableContent = function() {
-            this.tableContent.length = 0;
-            this.generateUnassignedInterfaces();
-            angular.copy(this.unassignedInterfaces.concat(this.injectedInterfaces),this.tableContent);
+
+        this.clearTopology = function() {
+            this.bridgeDomainsTopo.clear();
+            this.injectedInterfaces.length = 0;
+
         };
 
-        this.injectBdIntoBridgeDomainsTopo = function() {
-            this.bridgeDomainsTopo.addNode({
+        //this.generateUnassignedInterfaces = function() {
+        //    this.unassignedInterfaces.length = 0;
+        //    for (var x=0; x<this.interfaces.length; x++) {
+        //        if (!this.interfaces[x]['v3po:l2']['bridge-domain']) {
+        //            this.unassignedInterfaces.push(this.interfaces[x]);
+        //        }
+        //    }
+        //};
+
+        this.setData = function() {
+
+            for (var x=0; x<this.tableContent.length; x++) {
+                if (this.tableContent[x].assigned) {
+                    this.bridgeDomainsTopo.addNode(this.tableContent[x]);
+                    this.injectedInterfaces.push(this.tableContent[x]);
+                }
+            }
+
+            var nodes = [{
                 name : this.selectedBd.name,
                 label: this.selectedBd.name,
-                x: 60,
-                y: -50,
-                scale: 5
-            });
-        };
+                type:'bd',
+                x: 0,
+                y: 0,
+                scale: 1
+            }].concat(this.injectedInterfaces);
 
-        this.injectInterfacesLinksIntoBridgeDomainsTopo = function() {
-            var nodes = this.bridgeDomainsTopo.getNodes();
+            var links = [];
             for (var x=1; x<nodes.length; x++){
-                var target = nodes[x].get('data-id');
-                this.bridgeDomainsTopo.addLink({'source':0, 'target': target});
+                links.push({'source':0, 'target': x});
             }
-        };
 
-        this.injectInterfacesIntoBridgeDomainsTopo = function() {
-            for (var x=0; x<this.interfaces.length; x++) {
-                if ((this.interfaces[x]['v3po:l2']['bridge-domain'] === this.selectedBd.name) && (this.interfaces[x].type==='iana-if-type:ethernetCsmacd')) {
-                    this.interfaces[x].label = 'vpp1/'+this.interfaces[x].name;
-                    this.bridgeDomainsTopo.addNode(this.interfaces[x]);
-                    this.injectedInterfaces.push(this.interfaces[x]);
-                    this.interfaces[x].assigned = true;
-                }
-            }
+            var topoData = {
+                nodes: nodes,
+                links: links
+            };
+
+            this.bridgeDomainsTopo.data(topoData);
         };
 
-    });
+        this.injectBridgeDomainsTopoElements = function() {
+            this.clearTopology();
+            this.setData();
+            self.bridgeDomainsTopo.adaptToContainer();
+        };
+    }]);