2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 import {Component, EventEmitter, Output} from '@angular/core';
23 import {UIInterfaceModel} from "../interface-operations.component";
25 InputOperationParameter,
26 InterfaceOperationModel,
28 } from "../../../../../models/interfaceOperation";
29 import {TranslateService} from "../../../../shared/translator/translate.service";
30 import {IDropDownOption} from "onap-ui-angular/dist/form-elements/dropdown/dropdown-models";
31 import {DropdownValue} from "../../../../components/ui/form-components/dropdown/ui-element-dropdown.component";
32 import {ArtifactModel} from "../../../../../models/artifacts";
33 import {PropertyBEModel} from "../../../../../models/properties-inputs/property-be-model";
34 import {PropertyParamRowComponent} from "./property-param-row/property-param-row.component";
35 import {PropertyFEModel} from "../../../../../models/properties-inputs/property-fe-model";
38 selector: 'operation-handler',
39 templateUrl: './interface-operation-handler.component.html',
40 styleUrls: ['./interface-operation-handler.component.less'],
41 providers: [TranslateService]
44 export class InterfaceOperationHandlerComponent {
45 @Output('propertyChanged') emitter: EventEmitter<PropertyFEModel> = new EventEmitter<PropertyFEModel>();
48 toscaArtifactTypes: Array<DropdownValue>;
49 selectedInterface: UIInterfaceModel;
50 selectedInterfaceOperation: InterfaceOperationModel;
51 validityChangedCallback: Function;
54 interfaceType: string;
55 artifactVersion: string;
57 interfaceOperationName: string;
58 operationToUpdate: InterfaceOperationModel;
59 inputs: Array<InputOperationParameter> = [];
60 properties: Array<PropertyParamRowComponent> = [];
61 isLoading: boolean = false;
64 toscaArtifactTypeSelected: string;
65 toscaArtifactTypeProperties: Array<PropertyBEModel> = [];
67 toscaArtifactTypes: Array<DropdownValue> = [];
69 enableAddArtifactImplementation: boolean;
70 propertyValueValid: boolean = true;
73 this.interfaceType = this.input.selectedInterface.displayType();
74 this.operationToUpdate = new InterfaceOperationModel(this.input.selectedInterfaceOperation);
75 this.operationToUpdate.interfaceId = this.input.selectedInterface.uniqueId;
76 this.operationToUpdate.interfaceType = this.input.selectedInterface.type;
77 if (!this.operationToUpdate.inputs) {
78 this.operationToUpdate.inputs = new class implements IOperationParamsList {
79 listToscaDataDefinition: Array<InputOperationParameter> = [];
83 this.inputs = this.operationToUpdate.inputs.listToscaDataDefinition;
84 this.removeImplementationQuote();
85 this.validityChanged();
86 this.loadInterfaceOperationImplementation();
89 private loadInterfaceOperationImplementation() {
90 this.toscaArtifactTypes = this.input.toscaArtifactTypes;
91 this.artifactVersion = this.operationToUpdate.implementation.artifactVersion;
92 this.artifactName = this.operationToUpdate.implementation.artifactName;
93 this.toscaArtifactTypeProperties = this.operationToUpdate.implementation.properties;
94 this.getArtifactTypesSelected();
97 onDescriptionChange= (value: any): void => {
98 this.operationToUpdate.description = value;
101 onImplementationNameChange(value: any) {
104 let artifact = new ArtifactModel();
105 artifact.artifactName = value;
106 this.operationToUpdate.implementation = artifact;
107 this.enableAddArtifactImplementation = false;
108 this.readonly = false;
112 onPropertyValueChange = (propertyValue) => {
113 this.emitter.emit(propertyValue);
116 onMarkToAddArtifactToImplementation(event: any) {
118 this.toscaArtifactTypeSelected = undefined;
119 this.artifactVersion = undefined;
120 if (this.operationToUpdate.implementation.artifactType) {
121 this.artifactName = undefined;
123 this.toscaArtifactTypeProperties = undefined;
125 this.getArtifactTypesSelected();
127 this.enableAddArtifactImplementation = event;
128 this.validateRequiredField();
131 onSelectToscaArtifactType(type: IDropDownOption) {
133 let toscaArtifactType = type.value;
134 let artifact = new ArtifactModel();
135 this.artifactName = undefined;
136 this.artifactVersion = undefined;
137 artifact.artifactType = toscaArtifactType.type;
138 artifact.properties = toscaArtifactType.properties;
139 this.toscaArtifactTypeProperties = artifact.properties;
140 this.toscaArtifactTypeSelected = artifact.artifactType;
141 this.operationToUpdate.implementation = artifact;
142 this.getArtifactTypesSelected();
144 this.validateRequiredField();
147 onArtifactFileChange(value: any) {
149 this.operationToUpdate.implementation.artifactName = value;
151 this.validateRequiredField();
154 onArtifactVersionChange(value: any) {
156 this.operationToUpdate.implementation.artifactVersion = value;
160 onAddInput(inputOperationParameter?: InputOperationParameter): void {
161 let newInput = new InputOperationParameter(inputOperationParameter)
162 newInput.type = "string";
163 newInput.inputId = this.generateUniqueId();
164 this.inputs.push(newInput);
165 this.validityChanged();
168 propertyValueValidation = (propertyValue): void => {
169 this.onPropertyValueChange(propertyValue);
170 this.propertyValueValid = propertyValue.isValid;
171 this.readonly = !this.propertyValueValid;
172 this.validateRequiredField();
175 onRemoveInput = (inputParam: InputOperationParameter): void => {
176 let index = this.inputs.indexOf(inputParam);
177 this.inputs.splice(index, 1);
178 this.validityChanged();
181 private removeImplementationQuote(): void {
182 if (this.operationToUpdate.implementation) {
183 if (!this.operationToUpdate.implementation
184 || !this.operationToUpdate.implementation.artifactName) {
188 let implementation = this.operationToUpdate.implementation.artifactName.trim();
190 if (implementation.startsWith("'") && implementation.endsWith("'")) {
191 this.operationToUpdate.implementation.artifactName = implementation.slice(1, -1);
196 private generateUniqueId = (): string => {
198 const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
199 const charactersLength = characters.length;
200 for (let i = 0; i < 36; i++ ) {
201 result += characters.charAt(Math.floor(Math.random() * charactersLength));
206 validityChanged = () => {
207 let validState = this.checkFormValidForSubmit();
208 this.input.validityChangedCallback(validState);
210 this.readonly = false;
214 private getArtifactTypesSelected() {
215 if (this.operationToUpdate.implementation && this.operationToUpdate.implementation.artifactType) {
216 this.artifactName = this.operationToUpdate.implementation.artifactName;
217 this.toscaArtifactTypeSelected = this.operationToUpdate.implementation.artifactType;
218 this.artifactVersion = this.operationToUpdate.implementation.artifactVersion;
219 this.toscaArtifactTypeProperties = this.operationToUpdate.implementation.properties;
220 this.enableAddArtifactImplementation = true;
222 this.validateRequiredField();
225 validateRequiredField = () => {
226 this.readonly = true;
227 const isRequiredFieldSelected = this.isRequiredFieldsSelected();
228 this.input.validityChangedCallback(isRequiredFieldSelected);
229 if (isRequiredFieldSelected && this.propertyValueValid) {
230 this.readonly = false;
234 private isRequiredFieldsSelected() {
235 return this.toscaArtifactTypeSelected && this.artifactName;
238 private checkFormValidForSubmit = (): boolean => {
239 return this.operationToUpdate.name && this.artifactName && this.isParamsValid();
242 private isParamsValid = (): boolean => {
243 const isInputValid = (input) => input.name && input.inputId;
244 const isValid = this.inputs.every(isInputValid);
246 this.readonly = true;
251 toDropDownOption(val: string) {
252 return { value : val, label: val };