UI Support for operation milestones
[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 enum MilestoneEnum {
29     on_entry = 'on_entry',
30     on_success = 'on_success',
31     on_failure = 'on_failure',
32     on_timeout = 'on_timeout'
33 }
34
35 export enum ActivityTypesEnum {
36     Delegate = 'delegate'
37 }
38
39 export class InputOperationParameter {
40     name: string;
41     type: string;
42     schema: SchemaPropertyGroupModel;
43     inputId: string;
44     toscaDefaultValue?: string;
45     value?: any;
46     toscaFunction?: ToscaFunction;
47     subPropertyToscaFunctions: SubPropertyToscaFunction[];
48     valid:boolean= true;
49
50     constructor(param?: any) {
51         if (param) {
52             this.name = param.name;
53             this.type = param.type;
54             this.schema = param.schema;
55             this.inputId = param.inputId;
56             this.toscaDefaultValue = param.toscaDefaultValue;
57             this.value = param.value;
58             this.toscaFunction = param.toscaFunction;
59             this.valid = param.valid;
60             this.subPropertyToscaFunctions = param.subPropertyToscaFunctions;
61         }
62     }
63
64     public getDefaultValue(): any {
65         if (this.isTypeNotSimple()) {
66             if (this.toscaDefaultValue) {
67                 this.toscaDefaultValue = JSON.parse(this.toscaDefaultValue);
68                 return JSON.parse(this.toscaDefaultValue);
69             }
70             switch (this.type) {
71                 case PROPERTY_TYPES.LIST:
72                     return [];
73                 default:
74                     return {};
75             }
76         }
77
78         return this.toscaDefaultValue;
79     }
80
81     private isTypeNotSimple() {
82         return PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) == -1;
83     }
84
85     public isToscaFunction(): boolean {
86         return this.toscaFunction != null;
87     }
88 }
89
90 export class PropertyOperationParameter {
91     name: string;
92     type: string;
93     value?: string;
94     propertyId: string;
95
96     constructor(param?: any) {
97         if (param) {
98             this.name = param.name;
99             this.type = param.type;
100             this.value = param.value;
101             this.propertyId = param.propertyId;
102         }
103     }
104 }
105
106 export interface IOperationParamsList {
107     listToscaDataDefinition: Array<InputOperationParameter>;
108 }
109
110 export class Milestone {
111     activities: IActivityParameterList;
112     filterParams: IActivityParameterList;
113
114     constructor(param?: any) {
115         if (param) {
116             this.activities = param.activityParams;
117             this.filterParams = param.filterParams;
118         }
119     }
120 }
121
122 export class ActivityParameter {
123     type: string;
124     workflow: string;
125     inputs: IOperationParamsList;
126
127     constructor(param?: any) {
128         if (param) {
129             this.type = param.type;
130             this.workflow = param.workflow;
131             this.inputs = param.inputs;
132         }
133     }
134 }
135
136 export interface IActivityParameterList {
137     listToscaDataDefinition: Array<ActivityParameter>;
138 }
139
140 export class FilterParameter {
141     name: string;
142     constraint: string;
143     filterValue: any;
144     toscaFunction?: ToscaFunction;
145
146     constructor(param?: any) {
147         if (param) {
148             this.name = param.name;
149             this.constraint = param.constraint;
150             this.filterValue = param.filterValue;
151             this.toscaFunction = param.toscaFunction;
152         }
153     }
154 }
155
156 export interface IFilterParameterList {
157     listToscaDataDefinition: Array<FilterParameter>;
158 }
159
160 export class BEInterfaceOperationModel {
161     name: string;
162     description: string;
163     uniqueId: string;
164     inputs: IOperationParamsList;
165     implementation: ArtifactModel;
166     milestones: Object;
167
168     constructor(operation?: any) {
169         if (operation) {
170             this.name = operation.name;
171             this.description = operation.description;
172             this.uniqueId = operation.uniqueId;
173             this.inputs = operation.inputs;
174             this.implementation = operation.implementation;
175             this.milestones = operation.milestones;
176         }
177     }
178 }
179
180 export class InterfaceOperationModel extends BEInterfaceOperationModel {
181     isCollapsed: boolean = true;
182     isEllipsis: boolean;
183     MAX_LENGTH = 75;
184
185     interfaceType: string;
186     interfaceId: string;
187     operationType: string;
188     description: string;
189     uniqueId: string;
190     inputParams: IOperationParamsList;
191     implementation: ArtifactModel;
192     milestones: Object
193
194     constructor(operation?: any) {
195         super(operation);
196         if (operation) {
197             this.interfaceId = operation.interfaceId;
198             this.interfaceType = operation.interfaceType;
199             this.description = operation.description;
200             this.operationType = operation.operationType;
201             this.uniqueId = operation.uniqueId;
202             if (operation.inputParams && operation.inputParams.listToscaDataDefinition) {
203                 const listToscaDataDefinition: InputOperationParameter[] = [];
204                 operation.inputParams.listToscaDataDefinition.forEach(inputOperation => {
205                     listToscaDataDefinition.push(new InputOperationParameter(inputOperation));
206                 });
207                 this.inputParams = <IOperationParamsList> {
208                     'listToscaDataDefinition': listToscaDataDefinition
209                 };
210             }
211             if (operation.implementation) {
212                 this.implementation = new ArtifactModel(operation.implementation);
213             }
214             this.milestones = operation.milestones;
215         }
216     }
217
218     public displayType(): string {
219         return displayType(this.interfaceType);
220     }
221
222     getDescriptionEllipsis(): string {
223         if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
224             return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
225         }
226         return this.description;
227     }
228
229     toggleCollapsed(e) {
230         e.stopPropagation();
231         this.isCollapsed = !this.isCollapsed;
232     }
233
234 }
235
236 export class ComponentInterfaceDefinitionModel {
237     type: string;
238     uniqueId: string;
239     operations: Array<InterfaceOperationModel>;
240
241     constructor(interfaceOperation?: any) {
242         if (interfaceOperation) {
243             this.type = interfaceOperation.type;
244             this.uniqueId = interfaceOperation.uniqueId;
245             this.operations = interfaceOperation.operations;
246         }
247     }
248
249     public displayType(): string {
250         return displayType(this.type);
251     }
252 }
253
254 const displayType = (type:string) => type && type.substr(type.lastIndexOf('.') + 1);