9686f3d40e524cbb1689ad6de0b26e6c8a1d900e
[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 {Constraint, ConstraintTypes} from "../ng2/pages/properties-assignment/constraints/constraints.component";
23 import {load} from 'js-yaml';
24
25 export class ServiceDataTypeReader {
26
27     private serviceDataType = new DataTypeModel();
28
29     public read(dataTypeFile: File): Promise<DataTypeModel> {
30
31         return new Promise<DataTypeModel>((resolve, reject) => {
32             const reader = new FileReader();
33             reader.onloadend = () => {
34                 try {
35                     const result = <String>reader.result;
36                     const loadedContent = load(result);
37                     console.log("Readed content: " + loadedContent);
38                     this.readName(this.getDataType(loadedContent));
39                     this.readDerivedFrom(this.getDataType(loadedContent));
40                     this.readDescription(this.getDataType(loadedContent));
41                     this.readProperties(this.getDataType(loadedContent));
42                     resolve(this.serviceDataType);
43                 } catch (error) {
44                     reject(error);
45                 }
46             }
47             reader.readAsText(dataTypeFile);
48         });
49     }
50
51     private getDataType(fileContent:any) {
52         const index = Object.keys(fileContent).indexOf("data_types",0)
53         if (index == -1){
54             return fileContent;
55         }
56         return fileContent["data_types"];
57     }
58
59     private readName(fileContent: any) {
60         this.serviceDataType.name = Object.keys(fileContent).values().next().value;
61     }
62
63     private readDerivedFrom(fileContent: any) {
64         let dataType = Object.keys(fileContent).values().next().value;
65         this.serviceDataType.derivedFromName = fileContent[dataType]["derived_from"];
66     }
67
68     private readDescription(fileContent: any) {
69         let dataType = Object.keys(fileContent).values().next().value;
70         this.serviceDataType.description = fileContent[dataType]["description"];
71     }
72
73     private readProperties(fileContent: any) {
74         this.serviceDataType.properties = new Array<PropertyBEModel>();
75         let dataType = Object.keys(fileContent).values().next().value;
76         const properties = fileContent[dataType]["properties"];
77         Object.keys(properties).forEach((key )=>
78             {
79                 let property = new PropertyBEModel();
80                 property.name = key;
81                 property.description = properties[key]["description"];
82                 property.type = properties[key]["type"];
83                 property.schemaType = properties[key]["schema"];
84                 property.required = properties[key]["required"];
85                 const constraints = properties[key]["constraints"];
86
87                 if (constraints) {
88                     property.constraints = new Array();
89                     let constraintArray = new Array();
90                     Object.keys(constraints).forEach((constrainKey) => {
91                         Object.keys(constraints[constrainKey]).forEach((kc) => {
92                             let newConstraint = this.mapValuesToConstraint(<ConstraintTypes>kc, constraints[constrainKey][kc]);
93                             let jsonObject = this.getConstraintFormat(newConstraint);
94                             constraintArray.push(jsonObject);
95
96                         });
97                     });
98                     property.constraints.push(constraintArray);
99                 }
100                 this.serviceDataType.properties.push(property);
101             }
102         );
103     }
104
105     private getConstraintFormat(constraint: Constraint): any {
106         switch (constraint.type) {
107             case ConstraintTypes.equal:
108                 return {
109                     [ConstraintTypes.equal]: constraint.value
110                 }
111             case ConstraintTypes.less_or_equal:
112                 return {
113                     [ConstraintTypes.less_or_equal]: constraint.value
114                 }
115             case ConstraintTypes.less_than:
116                 return {
117                     [ConstraintTypes.less_than]: constraint.value
118                 }
119             case ConstraintTypes.greater_or_equal:
120                 return {
121                     [ConstraintTypes.greater_or_equal]: constraint.value
122                 }
123             case ConstraintTypes.greater_than:
124                 return {
125                     [ConstraintTypes.greater_than]: constraint.value
126                 }
127             case ConstraintTypes.in_range:
128                 return {
129                     [ConstraintTypes.in_range]: constraint.value
130                 }
131             case ConstraintTypes.length:
132                 return {
133                     [ConstraintTypes.length]: constraint.value
134                 }
135             case ConstraintTypes.max_length:
136                 return {
137                     [ConstraintTypes.max_length]: constraint.value
138                 }
139             case ConstraintTypes.min_length:
140                 return {
141                     [ConstraintTypes.min_length]: constraint.value
142                 }
143             case ConstraintTypes.pattern:
144                 return {
145                     [ConstraintTypes.pattern]: constraint.value
146                 }
147             case ConstraintTypes.valid_values:
148                 return {
149                     [ConstraintTypes.valid_values]: constraint.value
150                 }
151             default:
152                 return;
153         }
154     }
155
156     private mapValuesToConstraint(type: string, value: any):Constraint {
157         let constraintType: ConstraintTypes;
158         let constraintValue: any;
159         if (!type) {
160             constraintType = ConstraintTypes.null;
161             constraintValue = "";
162         } else if(type === "valid_values"){
163             constraintType = ConstraintTypes.valid_values;
164             constraintValue = value;
165         } else if(type === "equal") {
166             constraintType = ConstraintTypes.equal;
167             constraintValue = value;
168         } else if(type === "greater_than") {
169             constraintType = ConstraintTypes.greater_than;
170             constraintValue = value;
171         } else if(type === "greater_or_equal") {
172             constraintType = ConstraintTypes.greater_or_equal;
173             constraintValue = value;
174         } else if(type === "less_than") {
175             constraintType = ConstraintTypes.less_than;
176             constraintValue = value;
177         } else if(type === "less_or_equal") {
178             constraintType = ConstraintTypes.less_or_equal;
179             constraintValue = value;
180         } else if(type === "in_range") {
181             constraintType = ConstraintTypes.in_range;
182             constraintValue = value;
183         } else if(type === "range_max_value" || type === "range_min_value") {
184             constraintType = ConstraintTypes.in_range;
185             constraintValue = value;
186         } else if(type === "length") {
187             constraintType = ConstraintTypes.length;
188             constraintValue = value;
189         } else if(type === "min_length") {
190             constraintType = ConstraintTypes.min_length;
191             constraintValue = value;
192         } else if(type === "max_length") {
193             constraintType = ConstraintTypes.max_length;
194             constraintValue = value;
195         } else if(type === "pattern") {
196             constraintType = ConstraintTypes.pattern;
197             constraintValue = value;
198         }
199         return {
200             type:constraintType,
201             value:constraintValue
202         }
203     }
204 }