4c20f844e8da18e19e35b8f718e4845173a0909b
[honeycomb.git] / vbd / gui / module / src / main / resources / vpp / services / vpp.services.js
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 define(['app/vpp/vpp.module', 'next'], function(vpp) {
9
10     vpp.register.factory('VPPRestangular', function(Restangular, ENV) {
11         return Restangular.withConfig(function(RestangularConfig) {
12             RestangularConfig.setBaseUrl(ENV.getBaseURL("MD_SAL"));
13         });
14     });
15
16     vpp.register.factory('VPPRestangularXml', function(Restangular, ENV) {
17         return Restangular.withConfig(function(RestangularConfig) {
18             RestangularConfig.setBaseUrl(ENV.getBaseURL("MD_SAL"));
19             RestangularConfig.setDefaultHeaders({ "Content-Type": "application/xml" }, { "Accept": "application/xml" });
20         });
21     });
22
23     vpp.register.service('toastService', function($mdToast) {
24         this.showToast = function(content) {
25             var toast = $mdToast.simple()
26                 .content(content)
27                 .action('OK')
28                 .position('bottom right');
29             $mdToast.show(toast);
30         }
31     });
32
33     vpp.register.service('dataService', ['$timeout', function($timeout) {
34
35         var self = this;
36
37         nx.graphic.Icons.registerIcon("bd", "src/app/vpp/assets/images/bd1.svg", 45, 45);
38         nx.graphic.Icons.registerIcon("interf", "src/app/vpp/assets/images/interf.svg", 45, 45);
39
40         this.bridgeDomainsTopo = new nx.graphic.Topology({
41             adaptive: true,
42             scalable: true,
43             theme:'blue',
44             enableGradualScaling:true,
45             nodeConfig: {
46                 color: '#414040',
47                 label: 'model.label',
48                 scale: 'model.scale',
49                 iconType: function(vertex) {
50                     var type = vertex.get().type;
51                     if (type === 'bd') {
52                         return 'bd'
53                     } else {
54                         return 'interf';
55                     }
56                 }
57             },
58             linkConfig: {
59                 label: 'model.label',
60                 linkType: 'parallel',
61                 color: function(link) {
62                     if (link.getData().type === 'tunnel') {
63                         return '#00FF00';
64                     } else {
65                         return '#414040';
66                     }
67                 },
68                 width: function(link) {
69                     if (link.getData().type === 'tunnel') {
70                         return 5;
71                     }
72                 }
73             },
74             showIcon: true,
75             dataProcessor: 'force',
76             autoLayout: true,
77             enableSmartNode: false,
78             tooltipManagerConfig: {
79                 nodeTooltipContentClass: 'TooltipNode',
80                 linkTooltipContentClass: 'TooltipLink'
81             }
82         });
83         this.nextApp =  new nx.ui.Application;
84         this.bridgedomainsLoaded = false;
85
86         this.vpps = [];
87         this.tableContent = [];
88         this.originalAssignments = [];
89         this.interfaces = [];
90         this.injectedInterfaces = [];
91         this.bridgedomains = [];
92         this.changedInterfaces = [];
93         this.selectedBd = {
94             name: ''
95         };
96
97         this.generateInterfaces = function() {
98             self.interfaces.length = 0;
99             _.forEach(this.vpps, function(vpp) {
100                 _.forEach(vpp.interfaces, function(interf) {
101                     interf.vppName = vpp.name;
102                     interf.label = vpp.name+'/'+interf.name;
103                     interf.scale = 0.5;
104                     self.interfaces.push(interf);
105                 });
106             });
107             console.log(this.interfaces);
108         };
109
110         this.buildAssignedInterfaces = function() {
111             this.originalAssignments.length = 0;
112             _.forEach(this.bridgedomains, function(bd){
113                 var bdName = bd['topology-id'];
114                 var nodes = bd.node;
115                 if (nodes) {
116                     _.forEach(nodes, function(vpp) {
117                         var vppName = vpp['node-id'];
118                         var tps = vpp['termination-point'];
119                         if (tps) {
120                             _.forEach(tps, function(tp) {
121                                 tp.vppName = vppName;
122                                 tp.vbd = bdName;
123                                 self.originalAssignments.push(tp);
124                             })
125                         }
126                     })
127                 }
128             });
129
130             console.log('Assigned Interfaces: ');
131             console.log(this.originalAssignments);
132         };
133
134         this.buildTableContent = function() {
135             this.tableContent.length = 0;
136             angular.copy(this.interfaces,this.tableContent);
137
138
139             //Makes assignements based on previously changed interfaces, or assignments retrieved from ODL
140             _.forEach(this.tableContent, function(interf) {
141                 var matchedChangedInterface = _.find(self.changedInterfaces, {
142                     name: interf.name,
143                     vppName: interf.vppName
144                 });
145
146                 var matchedOriginalAssignment = _.find(self.originalAssignments, {
147                     'vbridge-topology:user-interface': interf.name,
148                     vppName: interf.vppName
149                 });
150
151                 if (matchedChangedInterface) {
152                     interf.assigned = matchedChangedInterface.assigned;
153                     interf.vbd = matchedChangedInterface.vbd;
154                 } else if (matchedOriginalAssignment) {
155                     interf.assigned = true;
156                     interf.vbd = matchedOriginalAssignment.vbd;
157                 } else {
158                     interf.assigned = false;
159                     interf.vbd = '';
160                 }
161             });
162
163             _.remove(self.tableContent, function(interf){
164                 var isAssigned = interf.assigned === true;
165                 var isNotCorrectBd = !(interf.vbd === self.selectedBd.name);
166                 return(isAssigned && isNotCorrectBd);
167             });
168
169             //_.forEach(this.originalAssignments, function(origAssignment) {
170             //    if (origAssignment.vbd === self.selectedBd.name) {
171             //        var matchedInterface = _.find(self.tableContent, {
172             //            name: origAssignment['vbridge-topology:user-interface'],
173             //            vppName: origAssignment.vppName
174             //        });
175             //        if (matchedInterface) {
176             //            matchedInterface.assigned = true;
177             //            matchedInterface.vbd = origAssignment.vbd;
178             //        } else {
179             //            console.error('Interface "'+origAssignment['vbridge-topology:user-interface']+'" on VPP "'+origAssignment.vppName+'" in vBD "'+origAssignment.vbd+'" was not found at mount point!');
180             //        }
181             //    } else {
182             //        _.remove(self.tableContent, {
183             //            name: origAssignment['vbridge-topology:user-interface'],
184             //            vppName: origAssignment.vppName
185             //        });
186             //    }
187             //});
188             //
189             //_.forEach(this.changedInterfaces, function(changedInterface) {
190             //
191             //    var matchedInterface = _.find(self.tableContent, {
192             //        name: changedInterface.name,
193             //        vppName: changedInterface.vppName
194             //    });
195             //
196             //    if (matchedInterface) {
197             //        if (changedInterface.assigned) {
198             //            if (changedInterface.vbd === self.selectedBd.name) {
199             //                matchedInterface.assigned = true;
200             //                matchedInterface.vbd = changedInterface.vbd;
201             //            }
202             //            else {
203             //                _.remove(self.tableContent, {
204             //                    name: changedInterface.name,
205             //                    vppName: changedInterface.vppName
206             //                });
207             //            }
208             //        } else {
209             //            matchedInterface.assigned = false;
210             //            matchedInterface.vbd = '';
211             //        }
212             //    }
213             //});
214
215             //..
216
217             //_.remove(self.tableContent, {
218             //    name: origAssignment['vbridge-topology:user-interface'],
219             //    vppName: origAssignment.vppName
220             //});
221
222             this.injectBridgeDomainsTopoElements();
223
224         };
225
226
227         this.clearTopology = function() {
228             this.bridgeDomainsTopo.clear();
229             this.injectedInterfaces.length = 0;
230
231         };
232
233         //this.generateUnassignedInterfaces = function() {
234         //    this.unassignedInterfaces.length = 0;
235         //    for (var x=0; x<this.interfaces.length; x++) {
236         //        if (!this.interfaces[x]['v3po:l2']['bridge-domain']) {
237         //            this.unassignedInterfaces.push(this.interfaces[x]);
238         //        }
239         //    }
240         //};
241
242         this.setData = function() {
243
244             for (var x=0; x<this.tableContent.length; x++) {
245                 if (this.tableContent[x].assigned) {
246                     this.bridgeDomainsTopo.addNode(this.tableContent[x]);
247                     this.injectedInterfaces.push(this.tableContent[x]);
248                 }
249             }
250
251             var nodes = [{
252                 name : this.selectedBd.name,
253                 label: this.selectedBd.name,
254                 type:'bd',
255                 x: 0,
256                 y: 0,
257                 scale: 1
258             }].concat(this.injectedInterfaces);
259
260             var links = [];
261             for (var x=1; x<nodes.length; x++){
262                 links.push({'source':0, 'target': x});
263             }
264
265             var topoData = {
266                 nodes: nodes,
267                 links: links
268             };
269
270             this.bridgeDomainsTopo.data(topoData);
271         };
272
273         this.injectBridgeDomainsTopoElements = function() {
274             this.clearTopology();
275             this.setData();
276             self.bridgeDomainsTopo.adaptToContainer();
277         };
278     }]);
279
280
281
282 });