Validation problems when trying to set an operation input of complex type
[sdc.git] / catalog-ui / src / app / models / interfaceOperation.ts
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 'use strict';
21
22 import {ArtifactModel} from "./artifacts";
23 import {SchemaPropertyGroupModel} from "./schema-property";
24 import {PROPERTY_DATA, PROPERTY_TYPES} from "../utils/constants";
25 import {ToscaFunction} from "./tosca-function";
26
27 export class InputOperationParameter {
28     name: string;
29     type: string;
30     schema: SchemaPropertyGroupModel;
31     inputId: string;
32     toscaDefaultValue?: string;
33     value?: any;
34     toscaFunction?: ToscaFunction;
35     valid:boolean= true;
36     subPropertyToscaFunctions: any;
37
38     constructor(param?: any) {
39         if (param) {
40             this.name = param.name;
41             this.type = param.type;
42             this.schema = param.schema;
43             this.inputId = param.inputId;
44             this.toscaDefaultValue = param.toscaDefaultValue;
45             this.value = param.value;
46             this.toscaFunction = param.toscaFunction;
47             this.valid = param.valid;
48             this.subPropertyToscaFunctions = param.subPropertyToscaFunctions;
49         }
50     }
51
52     public getDefaultValue(): any {
53         if (this.isTypeNotSimple()) {
54             if (this.toscaDefaultValue) {
55                 this.toscaDefaultValue = JSON.parse(this.toscaDefaultValue);
56                 return JSON.parse(this.toscaDefaultValue);
57             }
58             switch (this.type) {
59                 case PROPERTY_TYPES.LIST:
60                     return [];
61                 default:
62                     return {};
63             }
64         }
65
66         return this.toscaDefaultValue;
67     }
68
69     private isTypeNotSimple() {
70         return PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) == -1;
71     }
72
73     public isToscaFunction(): boolean {
74         return this.toscaFunction != null;
75     }
76 }
77
78 export class PropertyOperationParameter {
79     name: string;
80     type: string;
81     value?: string;
82     propertyId: string;
83
84     constructor(param?: any) {
85         if (param) {
86             this.name = param.name;
87             this.type = param.type;
88             this.value = param.value;
89             this.propertyId = param.propertyId;
90         }
91     }
92 }
93
94 export interface IOperationParamsList {
95     listToscaDataDefinition: Array<InputOperationParameter>;
96 }
97
98 export class BEInterfaceOperationModel {
99     name: string;
100     description: string;
101     uniqueId: string;
102     inputs: IOperationParamsList;
103     implementation: ArtifactModel;
104
105     constructor(operation?: any) {
106         if (operation) {
107             this.name = operation.name;
108             this.description = operation.description;
109             this.uniqueId = operation.uniqueId;
110             this.inputs = operation.inputs;
111             this.implementation = operation.implementation;
112         }
113     }
114 }
115
116 export class InterfaceOperationModel extends BEInterfaceOperationModel {
117     isCollapsed: boolean = true;
118     isEllipsis: boolean;
119     MAX_LENGTH = 75;
120
121     interfaceType: string;
122     interfaceId: string;
123     operationType: string;
124     description: string;
125     uniqueId: string;
126     inputParams: IOperationParamsList;
127     implementation: ArtifactModel;
128
129     constructor(operation?: any) {
130         super(operation);
131         if (operation) {
132             this.interfaceId = operation.interfaceId;
133             this.interfaceType = operation.interfaceType;
134             this.description = operation.description;
135             this.operationType = operation.operationType;
136             this.uniqueId = operation.uniqueId;
137             if (operation.inputParams && operation.inputParams.listToscaDataDefinition) {
138                 const listToscaDataDefinition: InputOperationParameter[] = [];
139                 operation.inputParams.listToscaDataDefinition.forEach(inputOperation => {
140                     listToscaDataDefinition.push(new InputOperationParameter(inputOperation));
141                 });
142                 this.inputParams = <IOperationParamsList> {
143                     'listToscaDataDefinition': listToscaDataDefinition
144                 };
145             }
146             if (operation.implementation) {
147                 this.implementation = new ArtifactModel(operation.implementation);
148             }
149         }
150     }
151
152     public displayType(): string {
153         return displayType(this.interfaceType);
154     }
155
156     getDescriptionEllipsis(): string {
157         if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
158             return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
159         }
160         return this.description;
161     }
162
163     toggleCollapsed(e) {
164         e.stopPropagation();
165         this.isCollapsed = !this.isCollapsed;
166     }
167
168 }
169
170 export class ComponentInterfaceDefinitionModel {
171     type: string;
172     uniqueId: string;
173     operations: Array<InterfaceOperationModel>;
174
175     constructor(interfaceOperation?: any) {
176         if (interfaceOperation) {
177             this.type = interfaceOperation.type;
178             this.uniqueId = interfaceOperation.uniqueId;
179             this.operations = interfaceOperation.operations;
180         }
181     }
182
183     public displayType(): string {
184         return displayType(this.type);
185     }
186 }
187
188 const displayType = (type:string) => type && type.substr(type.lastIndexOf('.') + 1);