Enable UI component to display property constraints
[sdc.git] / catalog-ui / src / app / utils / service-data-type-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 {DataTypeModel, PropertyBEModel} from "../models";
22 import {load} from 'js-yaml';
23
24 export class ServiceDataTypeReader {
25
26     private serviceDataType = new DataTypeModel();
27
28     public read(dataTypeFile: File): Promise<DataTypeModel> {
29
30         return new Promise<DataTypeModel>((resolve, reject) => {
31             const reader = new FileReader();
32             reader.onloadend = () => {
33                 try {
34                     const result = <String>reader.result;
35                     const loadedContent = load(result);
36                     console.log("Readed content: " + loadedContent);
37                     this.readName(this.getDataType(loadedContent));
38                     this.readDerivedFrom(this.getDataType(loadedContent));
39                     this.readDescription(this.getDataType(loadedContent));
40                     this.readProperties(this.getDataType(loadedContent));
41                     resolve(this.serviceDataType);
42                 } catch (error) {
43                     reject(error);
44                 }
45             }
46             reader.readAsText(dataTypeFile);
47         });
48     }
49
50     private getDataType(fileContent:any) {
51         const index = Object.keys(fileContent).indexOf("data_types",0)
52         if (index == -1){
53             return fileContent;
54         }
55         return fileContent["data_types"];
56     }
57
58     private readName(fileContent: any) {
59         this.serviceDataType.name = Object.keys(fileContent).values().next().value;
60     }
61
62     private readDerivedFrom(fileContent: any) {
63         let dataType = Object.keys(fileContent).values().next().value;
64         this.serviceDataType.derivedFromName = fileContent[dataType]["derived_from"];
65     }
66
67     private readDescription(fileContent: any) {
68         let dataType = Object.keys(fileContent).values().next().value;
69         this.serviceDataType.description = fileContent[dataType]["description"];
70     }
71
72     private readProperties(fileContent: any) {
73         this.serviceDataType.properties = new Array<PropertyBEModel>();
74         let dataType = Object.keys(fileContent).values().next().value;
75         const properties = fileContent[dataType]["properties"];
76         Object.keys(properties).forEach((key )=>
77             {
78                 let property = new PropertyBEModel();
79                 property.name = key;
80                 property.description = properties[key]["description"];
81                 property.type = properties[key]["type"];
82                 property.schemaType = properties[key]["schema"];
83                 property.required = properties[key]["required"];
84                 this.serviceDataType.properties.push(property);
85             }
86         );
87     }
88 }