a6279a589f129a1617c6f29f1890a05322cfaabc
[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 import {SubPropertyToscaFunction} from "./sub-property-tosca-function";
27
28 export class InputOperationParameter {
29     name: string;
30     type: string;
31     schema: SchemaPropertyGroupModel;
32     inputId: string;
33     toscaDefaultValue?: string;
34     value?: any;
35     toscaFunction?: ToscaFunction;
36     subPropertyToscaFunctions: SubPropertyToscaFunction[];
37     valid:boolean= true;
38
39     constructor(param?: any) {
40         if (param) {
41             this.name = param.name;
42             this.type = param.type;
43             this.schema = param.schema;
44             this.inputId = param.inputId;
45             this.toscaDefaultValue = param.toscaDefaultValue;
46             this.value = param.value;
47             this.toscaFunction = param.toscaFunction;
48             this.valid = param.valid;
49             this.subPropertyToscaFunctions = param.subPropertyToscaFunctions;
50         }
51     }
52
53     public getDefaultValue(): any {
54         if (this.isTypeNotSimple()) {
55             if (this.toscaDefaultValue) {
56                 this.toscaDefaultValue = JSON.parse(this.toscaDefaultValue);
57                 return JSON.parse(this.toscaDefaultValue);
58             }
59             switch (this.type) {
60                 case PROPERTY_TYPES.LIST:
61                     return [];
62                 default:
63                     return {};
64             }
65         }
66
67         return this.toscaDefaultValue;
68     }
69
70     private isTypeNotSimple() {
71         return PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) == -1;
72     }
73
74     public isToscaFunction(): boolean {
75         return this.toscaFunction != null;
76     }
77 }
78
79 export class PropertyOperationParameter {
80     name: string;
81     type: string;
82     value?: string;
83     propertyId: string;
84
85     constructor(param?: any) {
86         if (param) {
87             this.name = param.name;
88             this.type = param.type;
89             this.value = param.value;
90             this.propertyId = param.propertyId;
91         }
92     }
93 }
94
95 export interface IOperationParamsList {
96     listToscaDataDefinition: Array<InputOperationParameter>;
97 }
98
99 export class BEInterfaceOperationModel {
100     name: string;
101     description: string;
102     uniqueId: string;
103     inputs: IOperationParamsList;
104     implementation: ArtifactModel;
105
106     constructor(operation?: any) {
107         if (operation) {
108             this.name = operation.name;
109             this.description = operation.description;
110             this.uniqueId = operation.uniqueId;
111             this.inputs = operation.inputs;
112             this.implementation = operation.implementation;
113         }
114     }
115 }
116
117 export class InterfaceOperationModel extends BEInterfaceOperationModel {
118     isCollapsed: boolean = true;
119     isEllipsis: boolean;
120     MAX_LENGTH = 75;
121
122     interfaceType: string;
123     interfaceId: string;
124     operationType: string;
125     description: string;
126     uniqueId: string;
127     inputParams: IOperationParamsList;
128     implementation: ArtifactModel;
129
130     constructor(operation?: any) {
131         super(operation);
132         if (operation) {
133             this.interfaceId = operation.interfaceId;
134             this.interfaceType = operation.interfaceType;
135             this.description = operation.description;
136             this.operationType = operation.operationType;
137             this.uniqueId = operation.uniqueId;
138             if (operation.inputParams && operation.inputParams.listToscaDataDefinition) {
139                 const listToscaDataDefinition: InputOperationParameter[] = [];
140                 operation.inputParams.listToscaDataDefinition.forEach(inputOperation => {
141                     listToscaDataDefinition.push(new InputOperationParameter(inputOperation));
142                 });
143                 this.inputParams = <IOperationParamsList> {
144                     'listToscaDataDefinition': listToscaDataDefinition
145                 };
146             }
147             if (operation.implementation) {
148                 this.implementation = new ArtifactModel(operation.implementation);
149             }
150         }
151     }
152
153     public displayType(): string {
154         return displayType(this.interfaceType);
155     }
156
157     getDescriptionEllipsis(): string {
158         if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
159             return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
160         }
161         return this.description;
162     }
163
164     toggleCollapsed(e) {
165         e.stopPropagation();
166         this.isCollapsed = !this.isCollapsed;
167     }
168
169 }
170
171 export class ComponentInterfaceDefinitionModel {
172     type: string;
173     uniqueId: string;
174     operations: Array<InterfaceOperationModel>;
175
176     constructor(interfaceOperation?: any) {
177         if (interfaceOperation) {
178             this.type = interfaceOperation.type;
179             this.uniqueId = interfaceOperation.uniqueId;
180             this.operations = interfaceOperation.operations;
181         }
182     }
183
184     public displayType(): string {
185         return displayType(this.type);
186     }
187 }
188
189 const displayType = (type:string) => type && type.substr(type.lastIndexOf('.') + 1);