029601d6744662655906af8681ae3eb2f96926d7
[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
19 @Component({
20     selector: 'app-configuration-dashboard',
21     templateUrl: './configuration-dashboard.component.html',
22     styleUrls: ['./configuration-dashboard.component.css'],
23 })
24 export class ConfigurationDashboardComponent implements OnInit {
25     viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
26     @ViewChild(MetadataTabComponent, {static: false})
27     metadataTabComponent: MetadataTabComponent;
28     public customActionName = '';
29
30     entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
31         'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
32     @ViewChild('nameit', {static: true})
33     elementRef: ElementRef;
34     uploadedFiles = [];
35     zipFile: JSZip = new JSZip();
36     filesData: any = [];
37     folder: FolderNodeElement = new FolderNodeElement();
38     id: any;
39
40     currentBlob = new Blob();
41     vlbDefinition: VlbDefinition = new VlbDefinition();
42     isSaveEnabled = false;
43     versionPattern = '^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$';
44     metadataClasses = 'nav-item nav-link active';
45
46     constructor(
47         private route: ActivatedRoute,
48         private configurationDashboardService: ConfigurationDashboardService,
49         private packageCreationStore: PackageCreationStore,
50         private packageCreationUtils: PackageCreationUtils,
51         private router: Router,
52         private designerStore: DesignerStore,
53         private toastService: ToastrService
54     ) {
55     }
56
57     ngOnInit() {
58         this.vlbDefinition.topology_template = new TemplateTopology();
59
60         this.elementRef.nativeElement.focus();
61         this.refreshCurrentPackage();
62         const regexp = RegExp(this.versionPattern);
63         this.packageCreationStore.state$.subscribe(
64             cbaPackage => {
65                 if (cbaPackage && cbaPackage.metaData && cbaPackage.metaData.description
66                     && cbaPackage.metaData.name && cbaPackage.metaData.version &&
67                     regexp.test(cbaPackage.metaData.version)) {
68                     if (!this.metadataClasses.includes('complete')) {
69                         this.metadataClasses += ' complete';
70                     }
71                 } else {
72                     this.metadataClasses = this.metadataClasses.replace('complete', '');
73                     this.isSaveEnabled = false;
74                 }
75
76             });
77     }
78
79     private refreshCurrentPackage() {
80         this.id = this.route.snapshot.paramMap.get('id');
81         this.configurationDashboardService.getPagedPackages(this.id).subscribe(
82             (bluePrintDetailModels) => {
83                 if (bluePrintDetailModels) {
84                     this.viewedPackage = bluePrintDetailModels[0];
85                     this.downloadCBAPackage(bluePrintDetailModels);
86                     this.packageCreationStore.clear();
87                 }
88             });
89     }
90
91     private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
92         this.configurationDashboardService.downloadResource(
93             bluePrintDetailModels[0].artifactName + '/' + bluePrintDetailModels[0].artifactVersion).subscribe(response => {
94             const blob = new Blob([response], {type: 'application/octet-stream'});
95             this.currentBlob = blob;
96             this.extractBlobToStore(blob, bluePrintDetailModels);
97         });
98     }
99
100     private extractBlobToStore(blob: Blob, bluePrintDetailModels: BluePrintDetailModel) {
101         this.zipFile.loadAsync(blob).then((zip) => {
102             Object.keys(zip.files).forEach((filename) => {
103                 zip.files[filename].async('string').then((fileData) => {
104                     console.log(filename);
105                     if (fileData) {
106                         if (filename.includes('Scripts/')) {
107                             this.setScripts(filename, fileData);
108                         } else if (filename.includes('Templates/')) {
109                             if (filename.includes('-mapping.')) {
110                                 this.setMapping(filename, fileData);
111                             } else if (filename.includes('-template.')) {
112                                 this.setTemplates(filename, fileData);
113                             }
114
115                         } else if (filename.includes('Definitions/')) {
116                             this.setImports(filename, fileData, bluePrintDetailModels);
117                         } else if (filename.includes('TOSCA-Metadata/')) {
118                             const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
119                             this.setMetaData(metaDataTabInfo, bluePrintDetailModels[0]);
120                         }
121                     }
122                 });
123             });
124         });
125     }
126
127     setScripts(filename: string, fileData: any) {
128         this.packageCreationStore.addScripts(filename, fileData);
129     }
130
131     setImports(filename: string, fileData: any, bluePrintDetailModels: BluePrintDetailModel) {
132         if (filename.includes(bluePrintDetailModels[0].artifactName)) {
133             let definition = new VlbDefinition();
134             definition = fileData as VlbDefinition;
135             definition = JSON.parse(fileData);
136             const dslDefinition = new DslDefinition();
137             dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
138             const mapOfCustomKeys = new Map<string, string>();
139             for (const metadataKey in definition.metadata) {
140                 if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
141                     mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
142                 }
143             }
144             this.packageCreationStore.changeDslDefinition(dslDefinition);
145             this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
146             if (definition.topology_template && definition.topology_template.content) {
147                 this.designerStore.saveSourceContent(definition.topology_template.content);
148             }
149
150         }
151         this.packageCreationStore.addDefinition(filename, fileData);
152
153     }
154
155     setTemplates(filename: string, fileData: any) {
156         this.packageCreationStore.addTemplate(filename, fileData);
157     }
158
159     setMapping(fileName: string, fileData: string) {
160         this.packageCreationStore.addMapping(fileName, fileData);
161     }
162
163     editBluePrint() {
164         this.packageCreationStore.state$.subscribe(
165             cbaPackage => {
166                 FilesContent.clear();
167                 let packageCreationModes: PackageCreationModes;
168                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
169                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
170                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
171                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
172                 this.filesData.push(this.folder.TREE_DATA);
173                 this.saveBluePrintToDataBase();
174             });
175     }
176
177     setMetaData(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
178         metaDataObject.description = bluePrintDetailModel.artifactDescription;
179         this.packageCreationStore.changeMetaData(metaDataObject);
180
181     }
182
183     saveMetaData() {
184         this.metadataTabComponent.saveMetaDataToStore();
185
186     }
187
188     getMetaDataTabInfo(fileData: string) {
189         const metaDataTabModel = new MetaDataTabModel();
190         const arrayOfLines = fileData.split('\n');
191         metaDataTabModel.entryFileName = arrayOfLines[3].split(':')[1];
192         metaDataTabModel.name = arrayOfLines[4].split(':')[1];
193         metaDataTabModel.version = arrayOfLines[5].split(':')[1];
194         metaDataTabModel.mode = arrayOfLines[6].split(':')[1];
195         metaDataTabModel.templateTags = new Set<string>(arrayOfLines[7].split(':')[1].split(','));
196         return metaDataTabModel;
197     }
198
199     saveBluePrintToDataBase() {
200         this.create();
201         this.zipFile.generateAsync({type: 'blob'})
202             .then(blob => {
203                 this.packageCreationStore.saveBluePrint(blob).subscribe(
204                     bluePrintDetailModels => {
205                         if (bluePrintDetailModels) {
206                             const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
207                             this.toastService.info('package updated successfully ');
208                             this.router.navigate(['/packages/package/' + id]);
209                         }
210                     }, error => {
211                         this.toastService.error('error happened when editing ' + error.message);
212                         console.log('Error -' + error.message);
213                     });
214             });
215     }
216
217     deletePackage() {
218         this.configurationDashboardService.deletePackage(this.id).subscribe(res => {
219             console.log('Deleted');
220             console.log(res);
221             this.router.navigate(['/packages']);
222         }, err => {
223             console.log(err);
224         });
225     }
226
227     create() {
228         this.zipFile = new JSZip();
229         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
230             this.zipFile.folder(key.split('/')[0]);
231             this.zipFile.file(key, value);
232         });
233
234     }
235
236     discardChanges() {
237         this.refreshCurrentPackage();
238     }
239
240     downloadPackage(artifactName: string, artifactVersion: string) {
241         this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
242             const blob = new Blob([response], {type: 'application/octet-stream'});
243             saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
244         });
245     }
246
247     deployCurrentPackage() {
248         this.collectZipFileFromStore();
249         this.deployPackage();
250
251     }
252
253     goToDesignerMode(id) {
254         //  this.designerService.setActionName(this.customActionName);
255         this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
256     }
257
258     public dropped(files: NgxFileDropEntry[]) {
259
260     }
261
262     public fileOver(event) {
263         console.log(event);
264     }
265
266     public fileLeave(event) {
267         console.log(event);
268     }
269
270     textChanged($event: {}) {
271         this.packageCreationStore.addTopologyTemplate(this.vlbDefinition.topology_template);
272     }
273
274     enrichBluePrint() {
275
276         this.collectZipFileFromStore();
277         this.enrichPackage();
278     }
279
280     private collectZipFileFromStore() {
281         this.packageCreationStore.state$.subscribe(
282             cbaPackage => {
283                 FilesContent.clear();
284                 console.log(cbaPackage);
285                 let packageCreationModes: PackageCreationModes;
286                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
287                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
288                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
289                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
290                 this.filesData.push(this.folder.TREE_DATA);
291             });
292     }
293
294     private enrichPackage() {
295         this.create();
296         this.zipFile.generateAsync({type: 'blob'})
297             .then(blob => {
298                 this.packageCreationStore.enrichBluePrint(blob).subscribe(response => {
299                     console.log('success');
300                     const blobInfo = new Blob([response], {type: 'application/octet-stream'});
301                     this.configurationDashboardService.getPagedPackages(this.id).subscribe(
302                         (bluePrintDetailModels) => {
303                             if (bluePrintDetailModels) {
304                                 this.packageCreationStore.clear();
305                                 this.extractBlobToStore(blob, bluePrintDetailModels);
306                                 this.isSaveEnabled = true;
307                                 this.toastService.info('enriched successfully ');
308                             }
309                         });
310
311                     // saveAs(blobInfo, 'test' + '-' + '1.0.0' + '-CBA.zip');
312
313                 });
314             }, error => {
315                 this.toastService.error('error happened when editing ' + error.message);
316                 console.log('Error -' + error.message);
317             });
318     }
319
320     private deployPackage() {
321         this.create();
322         this.zipFile.generateAsync({type: 'blob'})
323             .then(blob => {
324                 this.packageCreationStore.deployBluePrint(blob).subscribe(response => {
325                     console.log('success');
326                     console.log(response);
327
328                     // saveAs(blobInfo, 'test' + '-' + '1.0.0' + '-CBA.zip');
329
330                 });
331             }, error => {
332                 this.toastService.error('error happened when deploying ' + error.message);
333                 console.log('Error -' + error.message);
334             });
335     }
336
337     clickEvent() {
338         this.isSaveEnabled = true;
339     }
340 }