summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/utils
diff options
context:
space:
mode:
authorfranciscovila <javier.paradela.vila@est.tech>2022-11-24 10:29:04 +0000
committerMichael Morris <michael.morris@est.tech>2023-01-26 23:32:10 +0000
commit701e441228724c5b701d94cc3f1e520ce656398a (patch)
tree5900482086d86f8b8e465e6d4b57db4bd7a94184 /catalog-ui/src/app/utils
parent1bbecd7edbdd907a53812d303d378236d23d071e (diff)
Import data type in UI
Develop all necessary changes in the UI to allow importing a data type from a yaml file Issue-ID: SDC-4279 Signed-off-by: franciscovila <javier.paradela.vila@est.tech> Change-Id: Id413386fad8b362e8c4a1d25c859a22178189074
Diffstat (limited to 'catalog-ui/src/app/utils')
-rw-r--r--catalog-ui/src/app/utils/service-data-type-reader.ts88
1 files changed, 88 insertions, 0 deletions
diff --git a/catalog-ui/src/app/utils/service-data-type-reader.ts b/catalog-ui/src/app/utils/service-data-type-reader.ts
new file mode 100644
index 0000000000..fd5ee2fc19
--- /dev/null
+++ b/catalog-ui/src/app/utils/service-data-type-reader.ts
@@ -0,0 +1,88 @@
+/*
+ * ============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 {DataTypeModel, PropertyBEModel} from "../models";
+import {load} from 'js-yaml';
+
+export class ServiceDataTypeReader {
+
+ private serviceDataType = new DataTypeModel();
+
+ public read(dataTypeFile: File): Promise<DataTypeModel> {
+
+ return new Promise<DataTypeModel>((resolve, reject) => {
+ const reader = new FileReader();
+ reader.onloadend = () => {
+ try {
+ const result = <String>reader.result;
+ const loadedContent = load(result);
+ console.log("Readed content: " + loadedContent);
+ this.readName(loadedContent);
+ this.readDerivedFrom(this.getDataType(loadedContent));
+ this.readDescription(this.getDataType(loadedContent));
+ this.readProperties(this.getDataType(loadedContent));
+ resolve(this.serviceDataType);
+ } catch (error) {
+ reject(error);
+ }
+ }
+ reader.readAsText(dataTypeFile);
+ });
+ }
+
+ private getDataType(fileContent:any) {
+ const index = Object.keys(fileContent).indexOf("data_types",0)
+ if (index == -1){
+ return fileContent;
+ }
+ return fileContent["data_types"];
+ }
+
+ private readName(fileContent: any) {
+ this.serviceDataType.name = Object.keys(fileContent).values().next().value;
+ }
+
+ private readDerivedFrom(fileContent: any) {
+ let dataType = Object.keys(fileContent).values().next().value;
+ this.serviceDataType.derivedFromName = fileContent[dataType]["derived_from"];
+ }
+
+ private readDescription(fileContent: any) {
+ let dataType = Object.keys(fileContent).values().next().value;
+ this.serviceDataType.description = fileContent[dataType]["description"];
+ }
+
+ private readProperties(fileContent: any) {
+ this.serviceDataType.properties = new Array<PropertyBEModel>();
+ let dataType = Object.keys(fileContent).values().next().value;
+ const properties = fileContent[dataType]["properties"];
+ Object.keys(properties).forEach((key )=>
+ {
+ let property = new PropertyBEModel();
+ property.name = key;
+ property.description = properties[key]["description"];
+ property.type = properties[key]["type"];
+ property.schemaType = properties[key]["schema"];
+ property.required = properties[key]["required"];
+ this.serviceDataType.properties.push(property);
+ }
+ );
+ }
+} \ No newline at end of file