e668b641726eac4af6f9f069fb56071dc2807c14
[ccsdk/cds.git] /
1 import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2 import {ActivatedRoute, Router} from '@angular/router';
3 import {BluePrintDetailModel} from '../model/BluePrint.detail.model';
4 import {PackageCreationStore} from '../package-creation/package-creation.store';
5 import {FilesContent, FolderNodeElement, MetaDataTabModel} from '../package-creation/mapping-models/metadata/MetaDataTab.model';
6 import {MetadataTabComponent} from '../package-creation/metadata-tab/metadata-tab.component';
7 import * as JSZip from 'jszip';
8 import {ConfigurationDashboardService} from './configuration-dashboard.service';
9 import {TemplateTopology, VlbDefinition} from '../package-creation/mapping-models/definitions/VlbDefinition';
10 import {DslDefinition} from '../package-creation/mapping-models/CBAPacakge.model';
11 import {PackageCreationUtils} from '../package-creation/package-creation.utils';
12 import {PackageCreationModes} from '../package-creation/creationModes/PackageCreationModes';
13 import {PackageCreationBuilder} from '../package-creation/creationModes/PackageCreationBuilder';
14 import {saveAs} from 'file-saver';
15 import {DesignerStore} from '../designer/designer.store';
16 import {ToastrService} from 'ngx-toastr';
17 import {NgxFileDropEntry} from 'ngx-file-drop';
18 import {TopologyTemplate} from '../designer/model/designer.topologyTemplate.model';
19
20 @Component({
21     selector: 'app-configuration-dashboard',
22     templateUrl: './configuration-dashboard.component.html',
23     styleUrls: ['./configuration-dashboard.component.css'],
24 })
25 export class ConfigurationDashboardComponent implements OnInit {
26     viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
27     @ViewChild(MetadataTabComponent, {static: false})
28     metadataTabComponent: MetadataTabComponent;
29     public customActionName = '';
30
31     entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
32         'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
33     @ViewChild('nameit', {static: true})
34     elementRef: ElementRef;
35     uploadedFiles = [];
36     zipFile: JSZip = new JSZip();
37     filesData: any = [];
38     folder: FolderNodeElement = new FolderNodeElement();
39     id: any;
40
41     currentBlob = new Blob();
42     vlbDefinition: VlbDefinition = new VlbDefinition();
43
44     constructor(
45         private route: ActivatedRoute,
46         private configurationDashboardService: ConfigurationDashboardService,
47         private packageCreationStore: PackageCreationStore,
48         private packageCreationUtils: PackageCreationUtils,
49         private router: Router,
50         private designerStore: DesignerStore,
51         private toastService: ToastrService
52     ) {
53     }
54
55     ngOnInit() {
56         this.vlbDefinition.topology_template = new TemplateTopology();
57
58         this.elementRef.nativeElement.focus();
59         this.id = this.route.snapshot.paramMap.get('id');
60         this.configurationDashboardService.getPagedPackages(this.id).subscribe(
61             (bluePrintDetailModels) => {
62                 if (bluePrintDetailModels) {
63                     this.viewedPackage = bluePrintDetailModels[0];
64                     this.downloadCBAPackage(bluePrintDetailModels);
65                     this.packageCreationStore.clear();
66                 }
67             });
68
69         if (this.route.snapshot.paramMap.has('id')) {
70             console.log('The id is equal to ' + this.route.snapshot.paramMap.get('id'));
71         }
72     }
73
74     private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
75         this.configurationDashboardService.downloadResource(
76             bluePrintDetailModels[0].artifactName + '/' + bluePrintDetailModels[0].artifactVersion).subscribe(response => {
77             const blob = new Blob([response], {type: 'application/octet-stream'});
78             this.currentBlob = blob;
79             this.zipFile.loadAsync(blob).then((zip) => {
80                 Object.keys(zip.files).forEach((filename) => {
81                     zip.files[filename].async('string').then((fileData) => {
82                         if (fileData) {
83                             if (filename.includes('Scripts/')) {
84                                 this.setScripts(filename, fileData);
85                             } else if (filename.includes('Templates/')) {
86                                 if (filename.includes('-mapping.')) {
87                                     this.setMapping(filename, fileData);
88                                 } else if (filename.includes('-template.')) {
89                                     this.setTemplates(filename, fileData);
90                                 }
91
92                             } else if (filename.includes('Definitions/')) {
93                                 this.setImports(filename, fileData, bluePrintDetailModels);
94                             } else if (filename.includes('TOSCA-Metadata/')) {
95                                 const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
96                                 this.setMetaData(metaDataTabInfo, bluePrintDetailModels[0]);
97                             }
98                         }
99                     });
100                 });
101             });
102         });
103     }
104
105     setScripts(filename: string, fileData: any) {
106         this.packageCreationStore.addScripts(filename, fileData);
107     }
108
109     setImports(filename: string, fileData: any, bluePrintDetailModels: BluePrintDetailModel) {
110         if (filename.includes(bluePrintDetailModels[0].artifactName)) {
111             let definition = new VlbDefinition();
112             definition = fileData as VlbDefinition;
113             definition = JSON.parse(fileData);
114             const dslDefinition = new DslDefinition();
115             dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
116             const mapOfCustomKeys = new Map<string, string>();
117             for (const metadataKey in definition.metadata) {
118                 if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
119                     mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
120                 }
121             }
122             this.packageCreationStore.changeDslDefinition(dslDefinition);
123             this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
124             if (definition.topology_template && definition.topology_template.content) {
125                 this.designerStore.saveSourceContent(definition.topology_template.content);
126             }
127
128         }
129         this.packageCreationStore.addDefinition(filename, fileData);
130
131     }
132
133     setTemplates(filename: string, fileData: any) {
134         this.packageCreationStore.addTemplate(filename, fileData);
135     }
136
137     setMapping(fileName: string, fileData: string) {
138         this.packageCreationStore.addMapping(fileName, fileData);
139     }
140
141     editBluePrint() {
142         this.packageCreationStore.state$.subscribe(
143             cbaPackage => {
144                 FilesContent.clear();
145                 let packageCreationModes: PackageCreationModes;
146                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
147                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
148                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
149                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
150                 this.filesData.push(this.folder.TREE_DATA);
151                 this.saveBluePrintToDataBase();
152             });
153     }
154
155     setMetaData(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
156         metaDataObject.description = bluePrintDetailModel.artifactDescription;
157         this.packageCreationStore.changeMetaData(metaDataObject);
158
159     }
160
161     saveMetaData() {
162         this.metadataTabComponent.saveMetaDataToStore();
163     }
164
165     getMetaDataTabInfo(fileData: string) {
166         const metaDataTabModel = new MetaDataTabModel();
167         const arrayOfLines = fileData.split('\n');
168         metaDataTabModel.entryFileName = arrayOfLines[3].split(':')[1];
169         metaDataTabModel.name = arrayOfLines[4].split(':')[1];
170         metaDataTabModel.version = arrayOfLines[5].split(':')[1];
171         metaDataTabModel.mode = arrayOfLines[6].split(':')[1];
172         metaDataTabModel.templateTags = new Set<string>(arrayOfLines[7].split(':')[1].split(','));
173         return metaDataTabModel;
174     }
175
176     saveBluePrintToDataBase() {
177         this.create();
178         this.zipFile.generateAsync({type: 'blob'})
179             .then(blob => {
180                 this.packageCreationStore.saveBluePrint(blob).subscribe(
181                     bluePrintDetailModels => {
182                         if (bluePrintDetailModels) {
183                             const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
184                             this.toastService.info('package updated successfully ');
185                             this.router.navigate(['/packages/package/' + id]);
186                         }
187                     }, error => {
188                         this.toastService.error('error happened when editing ' + error.message);
189                         console.log('Error -' + error.message);
190                     });
191             });
192     }
193
194     deletePackage() {
195         this.configurationDashboardService.deletePackage(this.id).subscribe(res => {
196             console.log('Deleted');
197             console.log(res);
198             this.router.navigate(['/packages']);
199         }, err => {
200             console.log(err);
201         });
202     }
203
204     create() {
205         this.zipFile = new JSZip();
206         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
207             this.zipFile.folder(key.split('/')[0]);
208             this.zipFile.file(key, value);
209         });
210
211     }
212
213     goBacktoDashboard() {
214         this.router.navigate(['/packages']);
215     }
216
217     downloadPackage(artifactName: string, artifactVersion: string) {
218         this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
219             const blob = new Blob([response], {type: 'application/octet-stream'});
220             saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
221         });
222     }
223
224     deployCurrentPackage() {
225         console.log('happened');
226         this.router.navigate(['/packages']);
227     }
228
229     goToDesignerMode(id) {
230         //  this.designerService.setActionName(this.customActionName);
231         this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
232     }
233
234     public dropped(files: NgxFileDropEntry[]) {
235
236     }
237
238     public fileOver(event) {
239         console.log(event);
240     }
241
242     public fileLeave(event) {
243         console.log(event);
244     }
245
246     textChanged($event: {}) {
247     this.packageCreationStore.addTopologyTemplate(this.vlbDefinition.topology_template);
248     }
249
250     enrichBluePrint() {
251
252         this.packageCreationStore.state$.subscribe(
253             cbaPackage => {
254                 FilesContent.clear();
255                 console.log(cbaPackage);
256
257                 let packageCreationModes: PackageCreationModes;
258                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
259                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
260                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
261                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
262                 this.filesData.push(this.folder.TREE_DATA);
263                 this.enrichPackage();
264
265             });
266     }
267
268     private enrichPackage() {
269         this.create();
270         this.zipFile.generateAsync({type: 'blob'})
271             .then(blob => {
272                 this.packageCreationStore.enrichBluePrint(blob).subscribe(response => {
273                     console.log('success');
274                     const blobInfo = new Blob([response], {type: 'application/octet-stream'});
275                     saveAs(blobInfo, 'test' + '-' + '1.0.0' + '-CBA.zip');
276                     this.toastService.info('enriched successfully ');
277                 });
278             }, error => {
279                 this.toastService.error('error happened when editing ' + error.message);
280                 console.log('Error -' + error.message);
281             });
282     }
283 }