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