aboutsummaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/package-creation-extraction.service.ts
blob: 585c16929d9150f51b9fdc25aa8b466d114befb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {Injectable, ViewChild} from '@angular/core';
import {MetaDataTabModel} from './mapping-models/metadata/MetaDataTab.model';
import {CBADefinition, TemplateTopology} from './mapping-models/definitions/CBADefinition';
import {DslDefinition} from './mapping-models/CBAPacakge.model';
import {PackageCreationStore} from './package-creation.store';
import * as JSZip from 'jszip';
import {PackageCreationUtils} from './package-creation.utils';
import {MetadataTabComponent} from './metadata-tab/metadata-tab.component';
import {DesignerStore} from '../designer/designer.store';
import {BluePrintDetailModel} from '../model/BluePrint.detail.model';

@Injectable({
    providedIn: 'root'
})
export class PackageCreationExtractionService {

    private zipFile: JSZip;
    private entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
        'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];

    private toscaMetaDataKeys: string[] = ['TOSCA-Meta-File-Version', 'CSAR-Version',
        'Created-By', 'Entry-Definitions', 'Template-Name', 'Template-Version', 'Template-Type', 'Template-Tags'];
    @ViewChild(MetadataTabComponent, {static: false})
    private metadataTabComponent: MetadataTabComponent;

    constructor(
        private packageCreationStore: PackageCreationStore,
        private packageCreationUtils: PackageCreationUtils,
        private designerStore: DesignerStore
    ) {

    }

    public extractBlobToStore(blob) {
        this.zipFile = new JSZip();
        let packageName = null;
        this.zipFile.loadAsync(blob).then((zip) => {
            Object.keys(zip.files).filter(fileName => fileName.includes('TOSCA-Metadata/'))
                .forEach((filename) => {
                    zip.files[filename].async('string').then((fileData) => {
                        if (fileData) {
                            if (filename.includes('TOSCA-Metadata/')) {

                                const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
                                packageName = metaDataTabInfo.name.trim();
                                this.setMetaData(metaDataTabInfo);
                                console.log('found file ' + packageName);
                            }
                        }
                    });
                });
        });

        this.zipFile.loadAsync(blob).then((zip) => {
            Object.keys(zip.files).forEach((filename) => {
                zip.files[filename].async('string').then((fileData) => {
                    console.log(filename);
                    if (fileData) {
                        if (filename.includes('Scripts/')) {
                            this.setScripts(filename, fileData);
                        } else if (filename.includes('Templates/')) {
                            if (filename.includes('-mapping.')) {
                                this.setMapping(filename, fileData);
                            } else if (filename.includes('-template.')) {
                                this.setTemplates(filename, fileData);
                            }

                        } else if (filename.includes('Definitions/')) {
                            this.setImports(filename, fileData, packageName);
                        } else if (filename.includes('Plans/')) {
                            this.setPlans(filename, fileData);
                        } else if (filename.includes('Requirements/')) {
                            this.setRequirements(filename, fileData);
                        }
                    }
                });
            });
        });
    }

    public setScripts(filename: string, fileData: any) {
        this.packageCreationStore.addScripts(filename, fileData);
    }

    public setPlans(filename: string, fileData: any) {
        this.packageCreationStore.addPlans(filename, fileData);
    }

    public setRequirements(filename: string, fileData: any) {
        this.packageCreationStore.addRequirements(filename, fileData);
    }

    public setImports(filename: string, fileData: any, packageName: string) {
        console.log(filename);
        if (filename.includes('Definitions/' + packageName.trim() + '.json')) {
            let definition = new CBADefinition();
            definition = fileData as CBADefinition;
            definition = JSON.parse(fileData);
            const dslDefinition = new DslDefinition();
            dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
            const mapOfCustomKeys = new Map<string, string>();
            for (const metadataKey in definition.metadata) {
                if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
                    mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
                }
            }

            this.packageCreationStore.changeDslDefinition(dslDefinition);
            this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
            this.setPackageDescription(definition.metadata.template_description);
            console.log(definition);
            console.log(definition.topology_template);
            const content = {};
            const workflow = 'workflows';
            content[workflow] = definition.topology_template ? definition.topology_template.workflows : {};
            const nodeTemplates = 'node_templates';
            content[nodeTemplates] = definition.topology_template ? definition.topology_template.node_templates : {};
            this.designerStore.saveSourceContent(JSON.stringify(content));
            if (definition.topology_template) {
                this.packageCreationStore.addTopologyTemplate(definition.topology_template);
            } else {
                this.packageCreationStore.addTopologyTemplate(new TemplateTopology());
            }


        }
        this.packageCreationStore.addDefinition(filename, fileData);

    }

    public setTemplates(filename: string, fileData: any) {
        this.packageCreationStore.addTemplate(filename, fileData);
    }

    public setMapping(fileName: string, fileData: string) {
        this.packageCreationStore.addMapping(fileName, fileData);
    }

    private setMetaData(metaDataObject: MetaDataTabModel) {
        this.packageCreationStore.changeMetaData(metaDataObject);
    }

    public setMetaDataWithObject(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
        metaDataObject.description = bluePrintDetailModel.artifactDescription;
        this.packageCreationStore.changeMetaData(metaDataObject);

    }

    public getMetaDataTabInfo(fileData: string) {
        const metaDataTabModel = new MetaDataTabModel();

        const arrayOfLines = fileData.split('\n');
        const map = new Map<string, string>();
        for (const currentLine of arrayOfLines) {
            const currentKey = currentLine.split(':')[0];
            const currentValue = currentLine.split(':')[1];
            map.set(currentKey, currentValue);
        }
        metaDataTabModel.entryFileName = map.get(this.toscaMetaDataKeys[3]);
        metaDataTabModel.name = map.get(this.toscaMetaDataKeys[4]).trim();
        metaDataTabModel.version = map.get(this.toscaMetaDataKeys[5]).trim();
        metaDataTabModel.mode = map.get(this.toscaMetaDataKeys[6]);
        if (map.get(this.toscaMetaDataKeys[7])) {
            metaDataTabModel.templateTags = new Set<string>(map.get(this.toscaMetaDataKeys[7]).split(','));
        }
        return metaDataTabModel;
    }

    private setPackageDescription(templateDescription: string) {
        const metaData = this.packageCreationStore.getMetaData();
        metaData.description = templateDescription;
        this.setMetaData(metaData);

    }
}