aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/utils/service-csar-reader.ts
blob: 73b77559e2ece67fe07e1301f3e01ac111c34bd6 (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
/*
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

import {ServiceCsar, ToscaMetaEntry} from "../models";
import {load} from 'js-yaml';
import { ComponentType } from "./constants";

export class ServiceCsarReader {

    private serviceCsar = new ServiceCsar();

    public read(serviceCsarBlob:Blob): Promise<ServiceCsar> {
        const jsZip = require("jszip");
        return new Promise<ServiceCsar>((resolve) => {
            jsZip.loadAsync(serviceCsarBlob).then(async zip => {
                const toscaMetaFileContent = await zip.file("TOSCA-Metadata/TOSCA.meta").async("string");
                this.readToscaMeta(toscaMetaFileContent);
                const entryDefinitionFileContent = await zip.file(this.serviceCsar.entryDefinitionFileName).async("string");
                this.readServiceMetadata(entryDefinitionFileContent);
                const interfaceDefinitionFileContent = await zip.file(this.serviceCsar.interfaceDefinitionFileName).async("string");
                this.readServiceSubstitutionNode(interfaceDefinitionFileContent);
                resolve(this.serviceCsar);
            });
        });
    }

    private readToscaMeta(toscaMetaFileContent:string) {
        let fileEntities:Array<string> = toscaMetaFileContent.replace("\r", "").split("\n");
        for(let entity of fileEntities.filter(e => e)) {
            let mapEntry:Array<string> = entity.split(":");
            let key:string = mapEntry[0].trim();
            let value:string = mapEntry[1].trim();
            this.serviceCsar.toscaMeta.dataMap.set(key, value);
        }
        this.readEntryDefinitionFileName();
        this.readInterfaceDefinitionFileName();
    }

    private readEntryDefinitionFileName() {
        this.serviceCsar.entryDefinitionFileName = this.serviceCsar.toscaMeta.getEntry(ToscaMetaEntry.ENTRY_DEFINITIONS);
    }

    private readInterfaceDefinitionFileName() {
        let fileNameArray:Array<string> = this.serviceCsar.entryDefinitionFileName.split(".");
        fileNameArray.splice(fileNameArray.length - 1, 0, "-interface.");
        this.serviceCsar.interfaceDefinitionFileName = fileNameArray.join("");
    }

    private readServiceMetadata(entryDefinitionFileContent) {
        const metadata = load(entryDefinitionFileContent).metadata;
        this.setMetadata(metadata);
    }

    private readServiceSubstitutionNode(interfaceDefinitionFileContent) {
        const nodeTypes = load(interfaceDefinitionFileContent).node_types;
        let nodeType = Object.keys(nodeTypes).values().next().value;
        this.serviceCsar.substitutionNodeType = nodeTypes[nodeType]["derived_from"];
    }

    private setMetadata = (metadata:object) : void => {
        let extraServiceMetadata: Map<string, string> = new Map<string, string>();
        this.serviceCsar.serviceMetadata.componentType = ComponentType.SERVICE;
        this.serviceCsar.serviceMetadata.serviceType = "Service";
        Object.keys(metadata).forEach(variable => {
            switch(variable) {
                case "description": {
                    this.serviceCsar.serviceMetadata.description = metadata[variable];
                    break;
                }
                case "name": {
                    this.serviceCsar.serviceMetadata.name = metadata[variable];
                    break;
                }
                case "model": {
                    this.serviceCsar.serviceMetadata.model = metadata[variable];
                    break;
                }
                case "category": {
                    this.serviceCsar.serviceMetadata.selectedCategory = metadata[variable];
                    break;
                }
                case "serviceRole": {
                    this.serviceCsar.serviceMetadata.serviceRole = metadata[variable];
                    break;
                }
                case "serviceFunction": {
                    this.serviceCsar.serviceMetadata.serviceFunction = metadata[variable];
                    break;
                }
                case "environmentContext": {
                    if (metadata[variable] != null) {
                        this.serviceCsar.serviceMetadata.environmentContext = metadata[variable];
                    }
                    break;
                }
                case "instantiationType": {
                    if (metadata[variable] != null) {
                        this.serviceCsar.serviceMetadata.instantiationType = metadata[variable];
                    }
                    break;
                }
                case "ecompGeneratedNaming": {
                    if (metadata[variable] != null) {
                        this.serviceCsar.serviceMetadata.ecompGeneratedNaming = metadata[variable] == "false" ? false : true;
                    }
                    break;
                }
                case "namingPolicy": {
                    if (metadata["ecompGeneratedNaming"] != "false") {
                        this.serviceCsar.serviceMetadata.namingPolicy = metadata[variable];
                    }
                    break;
                }
                default: {
                    extraServiceMetadata.set(variable, metadata[variable])
                    break;
                }
            }
        });
        this.serviceCsar.extraServiceMetadata = extraServiceMetadata;
    }
}