Handle CSAR reading errors in Service Import
[sdc.git] / catalog-ui / src / app / utils / service-csar-reader.ts
1 /*
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {ServiceCsar, ToscaMetaEntry} from "../models";
22 import {load} from 'js-yaml';
23 import { ComponentType } from "./constants";
24
25 export class ServiceCsarReader {
26
27     private serviceCsar = new ServiceCsar();
28
29     public read(serviceCsarBlob: Blob): Promise<ServiceCsar> {
30         const jsZip = require("jszip");
31         return new Promise<ServiceCsar>((resolve, reject) => {
32             jsZip.loadAsync(serviceCsarBlob).then(async zip => {
33                 try {
34                     const toscaMetaFileContent = await zip.file("TOSCA-Metadata/TOSCA.meta").async("string");
35                     this.readToscaMeta(toscaMetaFileContent);
36                     const entryDefinitionFileContent = await zip.file(this.serviceCsar.entryDefinitionFileName).async("string");
37                     this.readServiceMetadata(entryDefinitionFileContent);
38                     const interfaceDefinitionFileContent = await zip.file(this.serviceCsar.interfaceDefinitionFileName).async("string");
39                     this.readServiceSubstitutionNode(interfaceDefinitionFileContent);
40                     resolve(this.serviceCsar);
41                 } catch (error) {
42                     reject(error);
43                 }
44             });
45         });
46     }
47
48     private readToscaMeta(toscaMetaFileContent:string) {
49         let fileEntities:Array<string> = toscaMetaFileContent.replace("\r", "").split("\n");
50         for(let entity of fileEntities.filter(e => e)) {
51             let mapEntry:Array<string> = entity.split(":");
52             let key:string = mapEntry[0].trim();
53             let value:string = mapEntry[1].trim();
54             this.serviceCsar.toscaMeta.dataMap.set(key, value);
55         }
56         this.readEntryDefinitionFileName();
57         this.readInterfaceDefinitionFileName();
58     }
59
60     private readEntryDefinitionFileName() {
61         this.serviceCsar.entryDefinitionFileName = this.serviceCsar.toscaMeta.getEntry(ToscaMetaEntry.ENTRY_DEFINITIONS);
62     }
63
64     private readInterfaceDefinitionFileName() {
65         let fileNameArray:Array<string> = this.serviceCsar.entryDefinitionFileName.split(".");
66         fileNameArray.splice(fileNameArray.length - 1, 0, "-interface.");
67         this.serviceCsar.interfaceDefinitionFileName = fileNameArray.join("");
68     }
69
70     private readServiceMetadata(entryDefinitionFileContent) {
71         const metadata = load(entryDefinitionFileContent).metadata;
72         this.setMetadata(metadata);
73     }
74
75     private readServiceSubstitutionNode(interfaceDefinitionFileContent) {
76         const nodeTypes = load(interfaceDefinitionFileContent).node_types;
77         let nodeType = Object.keys(nodeTypes).values().next().value;
78         this.serviceCsar.substitutionNodeType = nodeTypes[nodeType]["derived_from"];
79     }
80
81     private setMetadata = (metadata:object) : void => {
82         let extraServiceMetadata: Map<string, string> = new Map<string, string>();
83         this.serviceCsar.serviceMetadata.componentType = ComponentType.SERVICE;
84         this.serviceCsar.serviceMetadata.serviceType = "Service";
85         Object.keys(metadata).forEach(variable => {
86             switch(variable) {
87                 case "description": {
88                     this.serviceCsar.serviceMetadata.description = metadata[variable];
89                     break;
90                 }
91                 case "name": {
92                     this.serviceCsar.serviceMetadata.name = metadata[variable];
93                     break;
94                 }
95                 case "model": {
96                     this.serviceCsar.serviceMetadata.model = metadata[variable];
97                     break;
98                 }
99                 case "category": {
100                     this.serviceCsar.serviceMetadata.selectedCategory = metadata[variable];
101                     break;
102                 }
103                 case "serviceRole": {
104                     this.serviceCsar.serviceMetadata.serviceRole = metadata[variable];
105                     break;
106                 }
107                 case "serviceFunction": {
108                     this.serviceCsar.serviceMetadata.serviceFunction = metadata[variable];
109                     break;
110                 }
111                 case "environmentContext": {
112                     if (metadata[variable] != null) {
113                         this.serviceCsar.serviceMetadata.environmentContext = metadata[variable];
114                     }
115                     break;
116                 }
117                 case "instantiationType": {
118                     if (metadata[variable] != null) {
119                         this.serviceCsar.serviceMetadata.instantiationType = metadata[variable];
120                     }
121                     break;
122                 }
123                 case "ecompGeneratedNaming": {
124                     if (metadata[variable] != null) {
125                         this.serviceCsar.serviceMetadata.ecompGeneratedNaming = metadata[variable] == "false" ? false : true;
126                     }
127                     break;
128                 }
129                 case "namingPolicy": {
130                     if (metadata["ecompGeneratedNaming"] != "false") {
131                         this.serviceCsar.serviceMetadata.namingPolicy = metadata[variable];
132                     }
133                     break;
134                 }
135                 default: {
136                     extraServiceMetadata.set(variable, metadata[variable])
137                     break;
138                 }
139             }
140         });
141         this.serviceCsar.extraServiceMetadata = extraServiceMetadata;
142     }
143 }