2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 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=========================================================
21 import {Component, ComponentRef, Inject, Input} from '@angular/core';
22 import {Component as IComponent} from 'app/models/components/component';
23 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
24 import {HierarchyDisplayOptions} from "../../components/logic/hierarchy-navigtion/hierarchy-display-options";
25 import {ISdcConfig, SdcConfigToken} from "app/ng2/config/sdc-config.config";
26 import {TranslateService} from "app/ng2/shared/translator/translate.service";
27 import {IModalButtonComponent, SdcUiServices} from 'onap-ui-angular';
28 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
30 import {ModalService} from 'app/ng2/services/modal.service';
40 WORKFLOW_ASSOCIATION_OPTIONS
43 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
44 import {TopologyTemplateService} from "../../services/component-services/topology-template.service";
45 import {InterfaceOperationModel} from "../../../models/interfaceOperation";
46 import {InterfaceOperationHandlerComponent} from "../composition/interface-operatons/operation-creator/interface-operation-handler.component";
47 import {DropdownValue} from "../../components/ui/form-components/dropdown/ui-element-dropdown.component";
48 import {ToscaArtifactModel} from "../../../models/toscaArtifact";
49 import {ToscaArtifactService} from "../../services/tosca-artifact.service";
50 import {InterfaceOperationComponent} from "../interface-operation/interface-operation.page.component";
51 import {Observable} from "rxjs/Observable";
52 import {PluginsService} from 'app/ng2/services/plugins.service';
53 import { InstanceFeDetails } from 'app/models/instance-fe-details';
55 export class UIOperationModel extends OperationModel {
56 isCollapsed: boolean = true;
60 constructor(operation: OperationModel) {
62 if (!operation.description) {
63 this.description = '';
66 if (this.description.length > this.MAX_LENGTH) {
67 this.isEllipsis = true;
69 this.isEllipsis = false;
73 getDescriptionEllipsis(): string {
74 if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
75 return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
77 return this.description;
82 this.isCollapsed = !this.isCollapsed;
86 class ModalTranslation {
90 CANCEL_BUTTON: string;
92 CREATE_BUTTON: string;
93 DELETE_BUTTON: string;
96 constructor(private TranslateService: TranslateService) {
97 this.TranslateService.languageChangedObservable.subscribe(lang => {
98 this.CREATE_TITLE = this.TranslateService.translate("INTERFACE_CREATE_TITLE");
99 this.EDIT_TITLE = this.TranslateService.translate('INTERFACE_EDIT_TITLE');
100 this.DELETE_TITLE = this.TranslateService.translate("INTERFACE_DELETE_TITLE");
101 this.CANCEL_BUTTON = this.TranslateService.translate("INTERFACE_CANCEL_BUTTON");
102 this.SAVE_BUTTON = this.TranslateService.translate("INTERFACE_SAVE_BUTTON");
103 this.CREATE_BUTTON = this.TranslateService.translate("INTERFACE_CREATE_BUTTON");
104 this.DELETE_BUTTON = this.TranslateService.translate("INTERFACE_DELETE_BUTTON");
105 this.deleteText = (operationName) => this.TranslateService.translate("INTERFACE_DELETE_TEXT", {operationName});
110 export class UIInterfaceModel extends InterfaceModel {
111 isCollapsed: boolean = false;
113 constructor(interf?: any) {
115 if (this.operations) {
116 this.operations = this.operations.map((operation) => new UIOperationModel(operation));
121 this.isCollapsed = !this.isCollapsed;
126 selector: 'interface-definition',
127 templateUrl: './interface-definition.page.component.html',
128 styleUrls: ['interface-definition.page.component.less'],
129 providers: [ModalService, TranslateService, InterfaceOperationComponent]
131 export class InterfaceDefinitionComponent {
133 modalInstance: ComponentRef<ModalComponent>;
134 interfaces: UIInterfaceModel[];
135 inputs: InputBEModel[];
137 instancesNavigationData = [];
139 loadingInstances: boolean = false;
140 selectedInstanceData: any = null;
141 hierarchyInstancesDisplayOptions: HierarchyDisplayOptions = new HierarchyDisplayOptions('uniqueId', 'name', 'archived', null, 'iconClass');
142 disableFlag : boolean = true;
144 deploymentArtifactsFilePath: Array<DropdownValue> = [];
146 toscaArtifactTypes: Array<DropdownValue> = [];
147 interfaceTypesTest: Array<DropdownValue> = [];
148 interfaceTypesMap: Map<string, string[]>;
151 interfaceTypes: { [interfaceType: string]: string[] };
152 modalTranslation: ModalTranslation;
154 capabilities: CapabilitiesGroup;
156 openOperation: OperationModel;
157 enableWorkflowAssociation: boolean;
158 workflowIsOnline: boolean;
159 validImplementationProps: boolean = true;
160 validMilestoneActivities: boolean = true;
161 validMilestoneFilters: boolean = true;
162 serviceInterfaces: InterfaceModel[];
164 @Input() component: IComponent;
165 @Input() readonly: boolean;
166 @Input() enableMenuItems: Function;
167 @Input() disableMenuItems: Function;
170 @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
171 @Inject("$state") private $state: ng.ui.IStateService,
172 @Inject("Notification") private notification: any,
173 private translateService: TranslateService,
174 private componentServiceNg2: ComponentServiceNg2,
175 private modalServiceNg2: ModalService,
176 private modalServiceSdcUI: SdcUiServices.ModalService,
177 private topologyTemplateService: TopologyTemplateService,
178 private toscaArtifactService: ToscaArtifactService,
179 private ComponentServiceNg2: ComponentServiceNg2,
180 private WorkflowServiceNg2: WorkflowServiceNg2,
181 private ModalServiceSdcUI: SdcUiServices.ModalService,
182 private PluginsService: PluginsService
184 this.modalTranslation = new ModalTranslation(translateService);
185 this.interfaceTypesMap = new Map<string, string[]>();
189 this.isLoading = true;
190 this.interfaces = [];
191 this.workflowIsOnline = !_.isUndefined(this.PluginsService.getPluginByStateUrl('workflowDesigner'));
193 this.ComponentServiceNg2.getInterfaceOperations(this.component),
194 this.ComponentServiceNg2.getComponentInputs(this.component),
195 this.ComponentServiceNg2.getInterfaceTypes(this.component),
196 this.ComponentServiceNg2.getCapabilitiesAndRequirements(this.component.componentType, this.component.uniqueId),
197 this.componentServiceNg2.getComponentResourcePropertiesData(this.component)
198 ).subscribe((response: any[]) => {
199 const callback = (workflows) => {
200 this.isLoading = false;
201 this.serviceInterfaces = response[0].interfaces;
202 this.initInterfaces(response[0].interfaces);
203 this.sortInterfaces();
204 this.inputs = response[1].inputs;
205 this.interfaceTypes = response[2];
206 this.workflows = (workflows.items) ? workflows.items : workflows;
207 this.capabilities = response[3].capabilities;
208 this.instances = response[4].componentInstances;
209 const serviceInstance = new ComponentInstance();
210 serviceInstance.name = "SELF";
211 serviceInstance.uniqueId = this.component.uniqueId;
212 if (this.instances != null) {
213 this.instances.unshift(serviceInstance);
215 this.instances = [serviceInstance];
217 _.forEach(this.instances, (instance) => {
218 this.instancesNavigationData.push(instance);
220 this.onInstanceSelectedUpdate(this.instancesNavigationData[0]);
221 this.loadingInstances = false;
224 if (this.enableWorkflowAssociation && this.workflowIsOnline) {
225 this.WorkflowServiceNg2.getWorkflows().subscribe(
228 this.workflowIsOnline = false;
237 this.loadToscaArtifacts();
240 onInstanceSelectedUpdate = (instance: any) => {
241 this.selectedInstanceData = instance;
242 if (instance.name != "SELF") {
243 this.disableFlag = true;
244 let newInterfaces : InterfaceModel[] = [];
245 if (instance.interfaces instanceof Array) {
246 instance.interfaces.forEach(result => {
247 let interfaceObj = new InterfaceModel();
248 interfaceObj.type = result.type;
249 interfaceObj.uniqueId = result.uniqueId;
250 if (result.operations instanceof Array) {
251 interfaceObj.operations = result.operations;
252 } else if (!_.isEmpty(result.operations)) {
253 interfaceObj.operations = [];
254 Object.keys(result.operations).forEach(name => {
255 interfaceObj.operations.push(result.operations[name]);
258 newInterfaces.push(interfaceObj);
261 Object.keys(instance.interfaces).forEach(key => {
262 let obj = instance.interfaces[key];
263 let interfaceObj = new InterfaceModel();
264 interfaceObj.type = obj.type;
265 interfaceObj.uniqueId = obj.uniqueId;
266 if (obj.operations instanceof Array) {
267 interfaceObj.operations = obj.operations;
268 } else if (!_.isEmpty(obj.operations)) {
269 interfaceObj.operations = [];
270 Object.keys(obj.operations).forEach(name => {
271 interfaceObj.operations.push(obj.operations[name]);
274 newInterfaces.push(interfaceObj);
277 this.interfaces = newInterfaces.map((interf) => new UIInterfaceModel(interf));
279 this.interfaces = this.serviceInterfaces.map((interf) => new UIInterfaceModel(interf));
281 this.sortInterfaces();
284 initInterfaces(interfaces: InterfaceModel[]): void {
286 this.interfaces = interfaces.map((interf) => new UIInterfaceModel(interf));
290 private cancelAndCloseModal = () => {
291 return this.modalServiceNg2.closeCurrentModal();
294 private disableSaveButton = (): boolean => {
295 let disable:boolean = true;
299 if (this.component.isService()) {
302 const validMilestoneActivities = this.modalInstance.instance.dynamicContent.instance.validMilestoneActivities;
303 const validMilestoneFilters = this.modalInstance.instance.dynamicContent.instance.validMilestoneFilters;
304 if (!validMilestoneActivities || !validMilestoneFilters) {
308 let selectedInterfaceOperation = this.modalInstance.instance.dynamicContent.instance.selectedInterfaceOperation;
309 let isInterfaceOperation:boolean = !(typeof selectedInterfaceOperation == 'undefined' || _.isEmpty(selectedInterfaceOperation));
310 let selectedInterfaceType = this.modalInstance.instance.dynamicContent.instance.selectedInterfaceType;
311 let isInterfaceType:boolean = !(typeof selectedInterfaceType == 'undefined' || _.isEmpty(selectedInterfaceType));
312 let bothSet: boolean = isInterfaceOperation && isInterfaceType;
314 let enableAddArtifactImplementation = this.modalInstance.instance.dynamicContent.instance.enableAddArtifactImplementation;
315 if(enableAddArtifactImplementation) {
316 let validImplementationProps = this.modalInstance.instance.dynamicContent.instance.validImplementationProps;
317 let toscaArtifactTypeSelected = this.modalInstance.instance.dynamicContent.instance.toscaArtifactTypeSelected;
318 let isToscaArtifactType:boolean = !(typeof toscaArtifactTypeSelected == 'undefined' || _.isEmpty(toscaArtifactTypeSelected));
319 disable = !bothSet || !isToscaArtifactType || !validImplementationProps;
326 onSelectInterfaceOperation(interfaceModel: UIInterfaceModel, operation: InterfaceOperationModel) {
327 const isEdit = operation !== undefined;
328 const modalButtons = [];
329 if (!this.readonly) {
330 const saveButton: ButtonModel = new ButtonModel(this.modalTranslation.SAVE_BUTTON, 'blue',
331 () => isEdit ? this.updateOperation() : this.createOperationCallback(),
332 this.disableSaveButton
334 modalButtons.push(saveButton);
336 modalButtons.push(new ButtonModel(this.modalTranslation.CANCEL_BUTTON, 'outline white', this.cancelAndCloseModal));
337 const interfaceDataModal: ModalModel =
338 new ModalModel('l', this.modalTranslation.EDIT_TITLE, '', modalButtons, 'custom');
339 this.modalInstance = this.modalServiceNg2.createCustomModal(interfaceDataModal);
341 this.modalServiceNg2.addDynamicContentToModal(
343 InterfaceOperationHandlerComponent,
345 deploymentArtifactsFilePath: this.deploymentArtifactsFilePath,
346 toscaArtifactTypes: this.toscaArtifactTypes,
347 selectedInterface: interfaceModel ? interfaceModel : new UIInterfaceModel(),
348 selectedInterfaceOperation: operation ? operation : new InterfaceOperationModel(),
349 validityChangedCallback: this.disableSaveButton,
350 isViewOnly: this.readonly,
351 validImplementationProps: this.validImplementationProps,
352 validMilestoneActivities: this.validMilestoneActivities,
353 validMilestoneFilters: this.validMilestoneFilters,
355 interfaceTypesMap: this.interfaceTypesMap,
356 modelName: this.component.model
359 this.modalInstance.instance.open();
362 private updateOperation = (): void => {
363 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = true;
364 const interfaceOperationHandlerComponentInstance: InterfaceOperationHandlerComponent = this.modalInstance.instance.dynamicContent.instance;
365 const operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
367 if (operationToUpdate.implementation && operationToUpdate.implementation.timeout != null) {
368 timeout = operationToUpdate.implementation.timeout;
370 const isArtifactChecked = interfaceOperationHandlerComponentInstance.enableAddArtifactImplementation;
371 if (!isArtifactChecked) {
372 const artifactName = interfaceOperationHandlerComponentInstance.artifactName ?
373 interfaceOperationHandlerComponentInstance.artifactName : '';
374 operationToUpdate.implementation = new ArtifactModel({'artifactName': artifactName, 'artifactVersion': ''} as ArtifactModel);
376 if (timeout != null) {
377 operationToUpdate.implementation.timeout = timeout;
379 this.componentServiceNg2.updateComponentInterfaceOperation(this.component.uniqueId, operationToUpdate)
380 .subscribe((newOperation: InterfaceOperationModel) => {
383 this.interfaces.forEach(interf => {
384 interf.operations.forEach(op => {
385 if (op.uniqueId === newOperation.uniqueId) {
387 oldOpIndex = interf.operations.findIndex((el) => el.uniqueId === op.uniqueId);
391 oldInterf.operations.splice(oldOpIndex, 1);
392 oldInterf.operations.push(new InterfaceOperationModel(newOperation));
394 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
396 this.sortInterfaces();
397 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
398 this.modalServiceNg2.closeCurrentModal();
402 private createOperationCallback(): void {
403 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = true;
404 const operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
405 console.log('createOperationCallback', operationToUpdate);
406 console.log('this.component', this.component);
407 this.componentServiceNg2.createComponentInterfaceOperation(this.component.uniqueId, this.component.getTypeUrl(), operationToUpdate)
408 .subscribe((newOperation: InterfaceOperationModel) => {
409 const foundInterface = this.interfaces.find(value => value.type === newOperation.interfaceType);
410 if (foundInterface) {
411 foundInterface.operations.push(new UIOperationModel(new OperationModel(newOperation)));
413 const uiInterfaceModel = new UIInterfaceModel();
414 uiInterfaceModel.type = newOperation.interfaceType;
415 uiInterfaceModel.uniqueId = newOperation.interfaceType;
416 uiInterfaceModel.operations = [];
417 uiInterfaceModel.operations.push(new UIOperationModel(new OperationModel(newOperation)));
418 this.interfaces.push(uiInterfaceModel);
421 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
423 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
424 this.modalServiceNg2.closeCurrentModal();
428 private handleEnableAddArtifactImplementation = (newOperation: InterfaceOperationModel): InterfaceOperationModel => {
429 if (!this.isEnableAddArtifactImplementation()) {
430 newOperation.implementation.artifactType = null;
431 newOperation.implementation.artifactVersion = null;
436 private isEnableAddArtifactImplementation = (): boolean => {
437 return this.modalInstance.instance.dynamicContent.enableAddArtifactImplementation;
440 private initInterfaceDefinition() {
441 this.isLoading = true;
442 this.interfaces = [];
443 this.topologyTemplateService.getComponentInterfaceOperations(this.component.componentType, this.component.uniqueId)
444 .subscribe((response) => {
445 if (response.interfaces) {
446 this.interfaces = response.interfaces.map((interfaceModel) => new UIInterfaceModel(interfaceModel));
448 this.isLoading = false;
452 private loadToscaArtifacts() {
453 this.toscaArtifactService.getToscaArtifacts(this.component.model).subscribe(response => {
455 let toscaArtifactsFound = <ToscaArtifactModel[]>_.values(response);
456 toscaArtifactsFound.forEach(value => this.toscaArtifactTypes.push(new DropdownValue(value, value.type)));
459 this.notification.error({
460 message: 'Failed to Load Tosca Artifacts:' + error,
466 private loadInterfaceTypes() {
467 this.componentServiceNg2.getInterfaceTypes(this.component).subscribe(response => {
469 console.info("loadInterfaceTypes ", response);
470 for (const interfaceType in response) {
471 this.interfaceTypesMap.set(interfaceType, response[interfaceType]);
472 this.interfaceTypesTest.push(new DropdownValue(interfaceType, interfaceType));
476 this.notification.error({
477 message: 'Failed to Load Interface Types:' + error,
483 collapseAll(value: boolean = true): void {
484 this.interfaces.forEach(interfaceData => {
485 interfaceData.isCollapsed = value;
489 isAllCollapsed(): boolean {
490 return this.interfaces.every((interfaceData) => interfaceData.isCollapsed);
493 isAllExpanded(): boolean {
494 return this.interfaces.every((interfaceData) => !interfaceData.isCollapsed);
497 isInterfaceListEmpty(): boolean {
498 return this.interfaces.length === 0;
501 isOperationListEmpty(): boolean {
502 return this.interfaces.filter((interfaceData) => interfaceData.operations && interfaceData.operations.length > 0).length > 0;
505 onRemoveOperation(operation: OperationModel): void {
510 const deleteButton: IModalButtonComponent = {
512 text: this.modalTranslation.DELETE_BUTTON,
517 this.ComponentServiceNg2
518 .deleteInterfaceOperation(this.component, operation)
520 const curInterf = this.interfaces.find((interf) => interf.type === operation.interfaceType);
521 const index = curInterf.operations.findIndex((el) => el.uniqueId === operation.uniqueId);
522 curInterf.operations.splice(index, 1);
523 if (!curInterf.operations.length) {
524 const interfIndex = this.interfaces.findIndex((interf) => interf.type === operation.interfaceType);
525 this.interfaces.splice(interfIndex, 1);
531 const cancelButton: IModalButtonComponent = {
533 text: this.modalTranslation.CANCEL_BUTTON,
538 this.openOperation = null;
542 this.ModalServiceSdcUI.openWarningModal(
543 this.modalTranslation.DELETE_TITLE,
544 this.modalTranslation.deleteText(operation.name),
545 'deleteOperationModal',
546 [deleteButton, cancelButton],
550 private createOperation = (operation: OperationModel): void => {
551 this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: OperationModel) => {
552 this.openOperation = null;
554 let curInterf = this.interfaces.find((interf) => interf.type === operation.interfaceType);
557 curInterf = new UIInterfaceModel({
558 type: response.interfaceType,
559 uniqueId: response.uniqueId,
562 this.interfaces.push(curInterf);
565 const newOpModel = new UIOperationModel(response);
566 curInterf.operations.push(newOpModel);
567 this.sortInterfaces();
569 if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL && operation.artifactData) {
570 this.ComponentServiceNg2.uploadInterfaceOperationArtifact(this.component, newOpModel, operation).subscribe();
571 } else if (response.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
572 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, response).subscribe();
573 } else if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW) {
574 this.$state.go('workspace.plugins', {path: 'workflowDesigner'});
579 private enableOrDisableSaveButton = (shouldEnable: boolean): void => {
580 const saveButton = this.modalInstance.instance.dynamicContent.getButtonById('saveButton');
581 saveButton.disabled = !shouldEnable;
584 private sortInterfaces(): void {
585 this.interfaces = this.interfaces.filter((interf) => interf.operations && interf.operations.length > 0); // remove empty interfaces
586 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
587 this.interfaces.forEach((interf) => {
588 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically