3  *  ============LICENSE_START=======================================================
 
   4  *  Copyright (C) 2023 Nordix Foundation.
 
   5  *  ================================================================================
 
   6  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   7  *  you may not use this file except in compliance with the License.
 
   8  *  You may obtain a copy of the License at
 
  10  *       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, Input, OnInit, Output} from '@angular/core';
 
  23 import {FormArray, FormControl, FormGroup, Validators} from "@angular/forms";
 
  24 import {ToscaCustomFunction} from "../../../../../models/tosca-custom-function";
 
  25 import {ToscaFunctionParameter} from "../../../../../models/tosca-function-parameter";
 
  26 import {ToscaStringParameter} from "../../../../../models/tosca-string-parameter";
 
  27 import {ToscaFunctionType} from "../../../../../models/tosca-function-type.enum";
 
  28 import {PropertyBEModel} from "../../../../../models/properties-inputs/property-be-model";
 
  29 import {PROPERTY_TYPES} from "../../../../../utils/constants";
 
  30 import {InstanceFeDetails} from "../../../../../models/instance-fe-details";
 
  31 import {ToscaFunctionValidationEvent} from "../tosca-function.component";
 
  32 import {ToscaFunction} from "../../../../../models/tosca-function";
 
  33 import {CustomToscaFunction} from "../../../../../models/default-custom-functions";
 
  36     selector: 'app-tosca-custom-function',
 
  37     templateUrl: './tosca-custom-function.component.html',
 
  38     styleUrls: ['./tosca-custom-function.component.less']
 
  40 export class ToscaCustomFunctionComponent implements OnInit {
 
  42     @Input() toscaCustomFunction: ToscaCustomFunction;
 
  43     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
 
  44     @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
 
  45     @Input() name: string;
 
  46     @Input() isDefaultCustomFunction: boolean;
 
  47     @Output() onValidFunction: EventEmitter<ToscaCustomFunction> = new EventEmitter<ToscaCustomFunction>();
 
  48     @Output() onValidityChange: EventEmitter<ToscaCustomFunctionValidationEvent> = new EventEmitter<ToscaCustomFunctionValidationEvent>();
 
  50     customFunctionFormName: FormControl = new FormControl('', [Validators.required, Validators.minLength(1)]);
 
  51     customParameterFormArray: FormArray = new FormArray([], Validators.minLength(1));
 
  52     formGroup: FormGroup = new FormGroup(
 
  54             'customParameterList': this.customParameterFormArray,
 
  55             'customName': this.customFunctionFormName
 
  59     parameters: ToscaFunctionParameter[] = [];
 
  60     propertyInputList: Array<PropertyBEModel> = [];
 
  62     STRING_FUNCTION_TYPE = ToscaFunctionType.STRING
 
  69         if (this.name && this.isDefaultCustomFunction) {
 
  70             this.customFunctionFormName.setValue(this.name);
 
  71             this.emitOnValidityChange();
 
  77     private initForm(): void {
 
  78         this.formGroup.valueChanges.subscribe(() => {
 
  79             this.emitOnValidityChange();
 
  80             if (this.formGroup.valid) {
 
  81                 this.onValidFunction.emit(this.buildCustomFunctionFromForm());
 
  84         if (!this.toscaCustomFunction) {
 
  87         if (this.toscaCustomFunction.parameters) {
 
  88             this.name = this.toscaCustomFunction.name;
 
  89             this.customFunctionFormName.setValue(this.name)
 
  90             this.parameters = Array.from(this.toscaCustomFunction.parameters);
 
  91             for (const parameter of this.parameters) {
 
  92                 if (parameter.type !== PROPERTY_TYPES.STRING) {
 
  93                     const propertyBEModel = this.createProperty(parameter.value);
 
  94                     propertyBEModel.toscaFunction = <ToscaFunction> parameter;
 
  95                     this.propertyInputList.push(propertyBEModel);
 
  96                     this.customParameterFormArray.push(
 
  97                         new FormControl(parameter, [Validators.required, Validators.minLength(1)])
 
 100                     this.propertyInputList.push(undefined);
 
 101                     this.customParameterFormArray.push(
 
 102                         new FormControl(parameter.value, [Validators.required, Validators.minLength(1)])
 
 109     private buildCustomFunctionFromForm(): ToscaCustomFunction {
 
 110         const toscaCustomFunction1 = new ToscaCustomFunction();
 
 111         toscaCustomFunction1.name = this.customFunctionFormName.value;
 
 112         this.customParameterFormArray.controls.forEach(control => {
 
 113             const value = control.value;
 
 114             if (typeof value === 'string') {
 
 115                 const stringParameter = new ToscaStringParameter();
 
 116                 stringParameter.value = value;
 
 117                 toscaCustomFunction1.parameters.push(stringParameter);
 
 119                 toscaCustomFunction1.parameters.push(control.value);
 
 123         return toscaCustomFunction1;
 
 126     private emitOnValidityChange() {
 
 127         this.onValidityChange.emit({
 
 128             isValid: this.formGroup.valid,
 
 129             toscaCustomFunction: this.formGroup.valid ? this.buildCustomFunctionFromForm() : undefined
 
 133     addFunction(): void {
 
 134         this.propertyInputList.push(this.createProperty());
 
 135         this.parameters.push({} as ToscaFunctionParameter);
 
 136         this.customParameterFormArray.push(
 
 137             new FormControl(undefined, [Validators.required, Validators.minLength(1)])
 
 141     addStringParameter(): void {
 
 142         const toscaStringParameter = new ToscaStringParameter();
 
 143         toscaStringParameter.value = ''
 
 144         this.parameters.push(toscaStringParameter);
 
 145         this.propertyInputList.push(undefined);
 
 146         this.customParameterFormArray.push(
 
 147             new FormControl('', [Validators.required, Validators.minLength(1)])
 
 151     removeParameter(position): void {
 
 152         this.propertyInputList.splice(position, 1);
 
 153         this.parameters.splice(position, 1);
 
 154         this.customParameterFormArray.removeAt(position);
 
 157     createProperty(value?: any): PropertyBEModel {
 
 158         const property = new PropertyBEModel();
 
 159         property.type = PROPERTY_TYPES.ANY;
 
 160         property.value = value ? value : undefined;
 
 164     onFunctionValidityChange(event: ToscaFunctionValidationEvent, index: number): void {
 
 165         if (event.isValid && event.toscaFunction) {
 
 166             this.customParameterFormArray.controls[index].setValue(event.toscaFunction)
 
 168             this.customParameterFormArray.controls[index].setValue(undefined);
 
 173 export interface ToscaCustomFunctionValidationEvent {
 
 175     toscaCustomFunction: ToscaCustomFunction,