2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 import {DataTypeModel, PropertyBEModel} from "../models";
22 import {Constraint, ConstraintTypes} from "../ng2/pages/properties-assignment/constraints/constraints.component";
23 import {load} from 'js-yaml';
25 export class ServiceDataTypeReader {
27 private serviceDataType = new DataTypeModel();
29 public read(dataTypeFile: File): Promise<DataTypeModel> {
31 return new Promise<DataTypeModel>((resolve, reject) => {
32 const reader = new FileReader();
33 reader.onloadend = () => {
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);
47 reader.readAsText(dataTypeFile);
51 private getDataType(fileContent:any) {
52 const index = Object.keys(fileContent).indexOf("data_types",0)
56 return fileContent["data_types"];
59 private readName(fileContent: any) {
60 this.serviceDataType.name = Object.keys(fileContent).values().next().value;
63 private readDerivedFrom(fileContent: any) {
64 let dataType = Object.keys(fileContent).values().next().value;
65 this.serviceDataType.derivedFromName = fileContent[dataType]["derived_from"];
68 private readDescription(fileContent: any) {
69 let dataType = Object.keys(fileContent).values().next().value;
70 this.serviceDataType.description = fileContent[dataType]["description"];
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 )=>
79 let property = new PropertyBEModel();
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"];
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);
98 property.constraints.push(constraintArray);
100 this.serviceDataType.properties.push(property);
105 private getConstraintFormat(constraint: Constraint): any {
106 switch (constraint.type) {
107 case ConstraintTypes.equal:
109 [ConstraintTypes.equal]: constraint.value
111 case ConstraintTypes.less_or_equal:
113 [ConstraintTypes.less_or_equal]: constraint.value
115 case ConstraintTypes.less_than:
117 [ConstraintTypes.less_than]: constraint.value
119 case ConstraintTypes.greater_or_equal:
121 [ConstraintTypes.greater_or_equal]: constraint.value
123 case ConstraintTypes.greater_than:
125 [ConstraintTypes.greater_than]: constraint.value
127 case ConstraintTypes.in_range:
129 [ConstraintTypes.in_range]: constraint.value
131 case ConstraintTypes.length:
133 [ConstraintTypes.length]: constraint.value
135 case ConstraintTypes.max_length:
137 [ConstraintTypes.max_length]: constraint.value
139 case ConstraintTypes.min_length:
141 [ConstraintTypes.min_length]: constraint.value
143 case ConstraintTypes.pattern:
145 [ConstraintTypes.pattern]: constraint.value
147 case ConstraintTypes.valid_values:
149 [ConstraintTypes.valid_values]: constraint.value
156 private mapValuesToConstraint(type: string, value: any):Constraint {
157 let constraintType: ConstraintTypes;
158 let constraintValue: any;
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;
201 value:constraintValue