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;
 
  72         this.interfaceType = this.input.selectedInterface.displayType();
 
  73         this.operationToUpdate = new InterfaceOperationModel(this.input.selectedInterfaceOperation);
 
  74         this.operationToUpdate.interfaceId = this.input.selectedInterface.uniqueId;
 
  75         this.operationToUpdate.interfaceType = this.input.selectedInterface.type;
 
  76         if (!this.operationToUpdate.inputs) {
 
  77             this.operationToUpdate.inputs = new class implements IOperationParamsList {
 
  78                 listToscaDataDefinition: Array<InputOperationParameter> = [];
 
  82         this.inputs = this.operationToUpdate.inputs.listToscaDataDefinition;
 
  83         this.removeImplementationQuote();
 
  84         this.validityChanged();
 
  85         this.loadInterfaceOperationImplementation();
 
  88     private loadInterfaceOperationImplementation() {
 
  89         this.toscaArtifactTypes = this.input.toscaArtifactTypes;
 
  90         this.artifactVersion = this.operationToUpdate.implementation.artifactVersion;
 
  91         this.artifactName = this.operationToUpdate.implementation.artifactName;
 
  92         this.toscaArtifactTypeProperties = this.operationToUpdate.implementation.properties;
 
  93         this.getArtifactTypesSelected();
 
  96     onDescriptionChange= (value: any): void => {
 
  97         this.operationToUpdate.description = value;
 
 100     onImplementationNameChange(value: any) {
 
 103             let artifact = new ArtifactModel();
 
 104             artifact.artifactName = value;
 
 105             this.operationToUpdate.implementation = artifact;
 
 106             this.enableAddArtifactImplementation = false;
 
 107             this.readonly = false;
 
 111     onPropertyValueChange = (propertyValue) => {
 
 112         this.emitter.emit(propertyValue);
 
 115     onMarkToAddArtifactToImplementation(event: any) {
 
 117             this.toscaArtifactTypeSelected = undefined;
 
 118             this.artifactVersion = undefined;
 
 119             if (this.operationToUpdate.implementation.artifactType) {
 
 120                 this.artifactName = undefined;
 
 122             this.toscaArtifactTypeProperties = undefined;
 
 124             this.getArtifactTypesSelected();
 
 126         this.enableAddArtifactImplementation = event;
 
 127         this.validateRequiredField();
 
 130     onSelectToscaArtifactType(type: IDropDownOption) {
 
 132             let toscaArtifactType = type.value;
 
 133             let artifact = new ArtifactModel();
 
 134             this.artifactName = undefined;
 
 135             this.artifactVersion = undefined;
 
 136             artifact.artifactType = toscaArtifactType.type;
 
 137             artifact.properties = toscaArtifactType.properties;
 
 138             this.toscaArtifactTypeProperties = artifact.properties;
 
 139             this.toscaArtifactTypeSelected = artifact.artifactType;
 
 140             this.operationToUpdate.implementation = artifact;
 
 141             this.getArtifactTypesSelected();
 
 143         this.validateRequiredField();
 
 146     onArtifactFileChange(value: any) {
 
 148             this.operationToUpdate.implementation.artifactName = value;
 
 150         this.validateRequiredField();
 
 153     onArtifactVersionChange(value: any) {
 
 155             this.operationToUpdate.implementation.artifactVersion = value;
 
 159     onAddInput(inputOperationParameter?: InputOperationParameter): void {
 
 160         let newInput = new InputOperationParameter(inputOperationParameter)
 
 161         newInput.type = "string";
 
 162         newInput.inputId = this.generateUniqueId();
 
 163         this.inputs.push(newInput);
 
 164         this.validityChanged();
 
 167     propertyValueValidation = (propertyValue): void => {
 
 168         this.onPropertyValueChange(propertyValue);
 
 169         this.readonly = !propertyValue.isValid;
 
 172     onRemoveInput = (inputParam: InputOperationParameter): void => {
 
 173         let index = this.inputs.indexOf(inputParam);
 
 174         this.inputs.splice(index, 1);
 
 175         this.validityChanged();
 
 178     private removeImplementationQuote(): void {
 
 179         if (this.operationToUpdate.implementation) {
 
 180             if (!this.operationToUpdate.implementation
 
 181                 || !this.operationToUpdate.implementation.artifactName) {
 
 185             let implementation = this.operationToUpdate.implementation.artifactName.trim();
 
 187             if (implementation.startsWith("'") && implementation.endsWith("'")) {
 
 188                 this.operationToUpdate.implementation.artifactName = implementation.slice(1, -1);
 
 193     private generateUniqueId = (): string => {
 
 195         const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
 196         const charactersLength = characters.length;
 
 197         for (let i = 0; i < 36; i++ ) {
 
 198             result += characters.charAt(Math.floor(Math.random() * charactersLength));
 
 203     validityChanged = () => {
 
 204         let validState = this.checkFormValidForSubmit();
 
 205         this.input.validityChangedCallback(validState);
 
 207             this.readonly = false;
 
 211     private getArtifactTypesSelected() {
 
 212         if (this.operationToUpdate.implementation && this.operationToUpdate.implementation.artifactType) {
 
 213             this.artifactName = this.operationToUpdate.implementation.artifactName;
 
 214             this.toscaArtifactTypeSelected = this.operationToUpdate.implementation.artifactType;
 
 215             this.artifactVersion = this.operationToUpdate.implementation.artifactVersion;
 
 216             this.toscaArtifactTypeProperties = this.operationToUpdate.implementation.properties;
 
 217             this.enableAddArtifactImplementation = true;
 
 219         this.validateRequiredField();
 
 222     validateRequiredField = () => {
 
 223         this.readonly = true;
 
 224         let requiredFieldSelected = this.toscaArtifactTypeSelected && this.artifactName ? true : false;
 
 225         this.input.validityChangedCallback(requiredFieldSelected);
 
 226         if (requiredFieldSelected) {
 
 227             this.readonly = false;
 
 231     private checkFormValidForSubmit = (): boolean => {
 
 232         return this.operationToUpdate.name && this.isParamsValid();
 
 235     private isParamsValid = (): boolean => {
 
 236         const isInputValid = (input) => input.name && input.inputId;
 
 237         const isValid = this.inputs.every(isInputValid);
 
 239             this.readonly = true;
 
 244     toDropDownOption(val: string) {
 
 245         return { value : val, label: val };