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';
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';
38 WORKFLOW_ASSOCIATION_OPTIONS
41 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
42 import {TopologyTemplateService} from "../../services/component-services/topology-template.service";
43 import {InterfaceOperationModel} from "../../../models/interfaceOperation";
45 InterfaceOperationHandlerComponent
46 } from "../composition/interface-operatons/operation-creator/interface-operation-handler.component";
49 } from "../../components/ui/form-components/dropdown/ui-element-dropdown.component";
50 import {ToscaArtifactModel} from "../../../models/toscaArtifact";
51 import {ToscaArtifactService} from "../../services/tosca-artifact.service";
53 InterfaceOperationComponent
54 } from "../interface-operation/interface-operation.page.component";
55 import {Observable} from "rxjs/Observable";
56 import {PluginsService} from 'app/ng2/services/plugins.service';
58 export class UIOperationModel extends OperationModel {
59 isCollapsed: boolean = true;
63 constructor(operation: OperationModel) {
66 if (!operation.description) {
67 this.description = '';
70 if (this.description.length > this.MAX_LENGTH) {
71 this.isEllipsis = true;
73 this.isEllipsis = false;
77 getDescriptionEllipsis(): string {
78 if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
79 return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
81 return this.description;
86 this.isCollapsed = !this.isCollapsed;
90 // tslint:disable-next-line:max-classes-per-file
91 class ModalTranslation {
95 CANCEL_BUTTON: string;
97 CREATE_BUTTON: string;
98 DELETE_BUTTON: string;
101 constructor(private TranslateService: TranslateService) {
102 this.TranslateService.languageChangedObservable.subscribe(lang => {
103 this.CREATE_TITLE = this.TranslateService.translate("INTERFACE_CREATE_TITLE");
104 this.EDIT_TITLE = this.TranslateService.translate('INTERFACE_EDIT_TITLE');
105 this.DELETE_TITLE = this.TranslateService.translate("INTERFACE_DELETE_TITLE");
106 this.CANCEL_BUTTON = this.TranslateService.translate("INTERFACE_CANCEL_BUTTON");
107 this.SAVE_BUTTON = this.TranslateService.translate("INTERFACE_SAVE_BUTTON");
108 this.CREATE_BUTTON = this.TranslateService.translate("INTERFACE_CREATE_BUTTON");
109 this.DELETE_BUTTON = this.TranslateService.translate("INTERFACE_DELETE_BUTTON");
110 this.deleteText = (operationName) => this.TranslateService.translate("INTERFACE_DELETE_TEXT", {operationName});
115 export class UIInterfaceModel extends InterfaceModel {
116 isCollapsed: boolean = false;
118 constructor(interf?: any) {
120 if (this.operations) {
121 this.operations = this.operations.map((operation) => new UIOperationModel(operation));
126 this.isCollapsed = !this.isCollapsed;
130 // tslint:disable-next-line:max-classes-per-file
132 selector: 'interface-definition',
133 templateUrl: './interface-definition.page.component.html',
134 styleUrls: ['interface-definition.page.component.less'],
135 providers: [ModalService, TranslateService, InterfaceOperationComponent]
137 export class InterfaceDefinitionComponent {
139 modalInstance: ComponentRef<ModalComponent>;
140 interfaces: UIInterfaceModel[];
141 inputs: InputBEModel[];
143 deploymentArtifactsFilePath: Array<DropdownValue> = [];
145 toscaArtifactTypes: Array<DropdownValue> = [];
146 interfaceTypesTest: Array<DropdownValue> = [];
147 interfaceTypesMap: Map<string, string[]>;
150 interfaceTypes: { [interfaceType: string]: string[] };
151 modalTranslation: ModalTranslation;
153 capabilities: CapabilitiesGroup;
156 openOperation: OperationModel;
157 enableWorkflowAssociation: boolean;
158 workflowIsOnline: boolean;
160 @Input() component: IComponent;
161 @Input() readonly: boolean;
162 @Input() enableMenuItems: Function;
163 @Input() disableMenuItems: Function;
166 @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
167 @Inject("$state") private $state: ng.ui.IStateService,
168 @Inject("Notification") private notification: any,
169 private translateService: TranslateService,
170 private componentServiceNg2: ComponentServiceNg2,
171 private modalServiceNg2: ModalService,
172 private modalServiceSdcUI: SdcUiServices.ModalService,
173 private topologyTemplateService: TopologyTemplateService,
174 private toscaArtifactService: ToscaArtifactService,
175 private ComponentServiceNg2: ComponentServiceNg2,
176 private WorkflowServiceNg2: WorkflowServiceNg2,
177 private ModalServiceSdcUI: SdcUiServices.ModalService,
178 private PluginsService: PluginsService
180 this.modalTranslation = new ModalTranslation(translateService);
181 this.interfaceTypesMap = new Map<string, string[]>();
185 this.isLoading = true;
186 this.interfaces = [];
187 this.workflowIsOnline = !_.isUndefined(this.PluginsService.getPluginByStateUrl('workflowDesigner'));
189 this.ComponentServiceNg2.getInterfaceOperations(this.component),
190 this.ComponentServiceNg2.getComponentInputs(this.component),
191 this.ComponentServiceNg2.getInterfaceTypes(this.component),
192 this.ComponentServiceNg2.getCapabilitiesAndRequirements(this.component.componentType, this.component.uniqueId)
193 ).subscribe((response: any[]) => {
194 const callback = (workflows) => {
195 this.isLoading = false;
196 this.initInterfaces(response[0].interfaces);
197 this.sortInterfaces();
198 this.inputs = response[1].inputs;
199 this.interfaceTypes = response[2];
200 this.workflows = (workflows.items) ? workflows.items : workflows;
201 this.capabilities = response[3].capabilities;
203 if (this.enableWorkflowAssociation && this.workflowIsOnline) {
204 this.WorkflowServiceNg2.getWorkflows().subscribe(
207 this.workflowIsOnline = false;
215 this.loadToscaArtifacts();
218 initInterfaces(interfaces: InterfaceModel[]): void {
220 this.interfaces = interfaces.map((interf) => new UIInterfaceModel(interf));
224 private cancelAndCloseModal = () => {
225 return this.modalServiceNg2.closeCurrentModal();
228 private disableSaveButton = (): boolean => {
229 return this.isViewOnly ||
230 (this.isEnableAddArtifactImplementation()
231 && (!this.modalInstance.instance.dynamicContent.toscaArtifactTypeSelected ||
232 !this.modalInstance.instance.dynamicContent.artifactName)
236 onSelectInterfaceOperation(interfaceModel: UIInterfaceModel, operation: InterfaceOperationModel) {
237 const isEdit = operation !== undefined;
238 const cancelButton: ButtonModel = new ButtonModel(this.modalTranslation.CANCEL_BUTTON, 'outline white', this.cancelAndCloseModal);
239 const saveButton: ButtonModel = new ButtonModel(this.modalTranslation.SAVE_BUTTON, 'blue',
240 () => isEdit ? this.updateOperation() : this.createOperationCallback(),
241 this.disableSaveButton
243 const interfaceDataModal: ModalModel =
244 new ModalModel('l', this.modalTranslation.EDIT_TITLE, '', [saveButton, cancelButton], 'custom');
245 this.modalInstance = this.modalServiceNg2.createCustomModal(interfaceDataModal);
247 this.modalServiceNg2.addDynamicContentToModal(
249 InterfaceOperationHandlerComponent,
251 deploymentArtifactsFilePath: this.deploymentArtifactsFilePath,
252 toscaArtifactTypes: this.toscaArtifactTypes,
253 selectedInterface: interfaceModel ? interfaceModel : new UIInterfaceModel(),
254 selectedInterfaceOperation: operation ? operation : new InterfaceOperationModel(),
255 validityChangedCallback: this.disableSaveButton,
256 isViewOnly: this.isViewOnly,
258 interfaceTypesMap: this.interfaceTypesMap,
259 modelName: this.component.model
262 this.modalInstance.instance.open();
265 private updateOperation = (): void => {
266 let operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
267 this.componentServiceNg2.updateComponentInterfaceOperation(this.component.uniqueId, operationToUpdate)
268 .subscribe((newOperation: InterfaceOperationModel) => {
271 this.interfaces.forEach(interf => {
272 interf.operations.forEach(op => {
273 if (op.uniqueId === newOperation.uniqueId) {
275 oldOpIndex = interf.operations.findIndex((el) => el.uniqueId === op.uniqueId);
279 newOperation = this.handleEnableAddArtifactImplementation(newOperation);
280 oldInterf.operations.splice(oldOpIndex, 1);
281 oldInterf.operations.push(new InterfaceOperationModel(newOperation));
283 this.modalServiceNg2.closeCurrentModal();
286 private createOperationCallback(): void {
287 const operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
288 console.log('createOperationCallback', operationToUpdate);
289 console.log('this.component', this.component);
290 this.componentServiceNg2.createComponentInterfaceOperation(this.component.uniqueId, this.component.getTypeUrl(), operationToUpdate)
291 .subscribe((newOperation: InterfaceOperationModel) => {
292 const foundInterface = this.interfaces.find(value => value.type === newOperation.interfaceType);
293 if (foundInterface) {
294 foundInterface.operations.push(new UIOperationModel(new OperationModel(newOperation)));
296 const uiInterfaceModel = new UIInterfaceModel();
297 uiInterfaceModel.type = newOperation.interfaceType;
298 uiInterfaceModel.uniqueId = newOperation.interfaceType;
299 uiInterfaceModel.operations = [];
300 uiInterfaceModel.operations.push(new UIOperationModel(new OperationModel(newOperation)));
301 this.interfaces.push(uiInterfaceModel);
304 this.modalServiceNg2.closeCurrentModal();
307 private handleEnableAddArtifactImplementation = (newOperation: InterfaceOperationModel): InterfaceOperationModel => {
308 if (!this.isEnableAddArtifactImplementation()) {
309 newOperation.implementation.artifactType = null;
310 newOperation.implementation.artifactVersion = null;
315 private isEnableAddArtifactImplementation = (): boolean => {
316 return this.modalInstance.instance.dynamicContent.enableAddArtifactImplementation;
319 private initInterfaceDefinition() {
320 this.isLoading = true;
321 this.interfaces = [];
322 this.topologyTemplateService.getComponentInterfaceOperations(this.component.componentType, this.component.uniqueId)
323 .subscribe((response) => {
324 if (response.interfaces) {
325 this.interfaces = response.interfaces.map((interfaceModel) => new UIInterfaceModel(interfaceModel));
327 this.isLoading = false;
331 private loadToscaArtifacts() {
332 this.toscaArtifactService.getToscaArtifacts(this.component.model).subscribe(response => {
334 let toscaArtifactsFound = <ToscaArtifactModel[]>_.values(response);
335 toscaArtifactsFound.forEach(value => this.toscaArtifactTypes.push(new DropdownValue(value, value.type)));
338 this.notification.error({
339 message: 'Failed to Load Tosca Artifacts:' + error,
345 private loadInterfaceTypes() {
346 this.componentServiceNg2.getInterfaceTypes(this.component).subscribe(response => {
348 console.info("loadInterfaceTypes ", response);
349 for (const interfaceType in response) {
350 this.interfaceTypesMap.set(interfaceType, response[interfaceType]);
351 this.interfaceTypesTest.push(new DropdownValue(interfaceType, interfaceType));
355 this.notification.error({
356 message: 'Failed to Load Interface Types:' + error,
362 collapseAll(value: boolean = true): void {
363 this.interfaces.forEach(interfaceData => {
364 interfaceData.isCollapsed = value;
368 isAllCollapsed(): boolean {
369 return this.interfaces.every((interfaceData) => interfaceData.isCollapsed);
372 isAllExpanded(): boolean {
373 return this.interfaces.every((interfaceData) => !interfaceData.isCollapsed);
376 isInterfaceListEmpty(): boolean {
377 return this.interfaces.length === 0;
380 isOperationListEmpty(): boolean {
381 return this.interfaces.filter((interfaceData) => interfaceData.operations && interfaceData.operations.length > 0).length > 0;
384 onRemoveOperation = (event: Event, operation: OperationModel): void => {
385 event.stopPropagation();
387 const deleteButton: IModalButtonComponent = {
389 text: this.modalTranslation.DELETE_BUTTON,
394 this.ComponentServiceNg2
395 .deleteInterfaceOperation(this.component, operation)
397 const curInterf = this.interfaces.find((interf) => interf.type === operation.interfaceType);
398 const index = curInterf.operations.findIndex((el) => el.uniqueId === operation.uniqueId);
399 curInterf.operations.splice(index, 1);
400 if (!curInterf.operations.length) {
401 const interfIndex = this.interfaces.findIndex((interf) => interf.type === operation.interfaceType);
402 this.interfaces.splice(interfIndex, 1);
408 const cancelButton: IModalButtonComponent = {
410 text: this.modalTranslation.CANCEL_BUTTON,
415 this.openOperation = null;
419 this.ModalServiceSdcUI.openWarningModal(
420 this.modalTranslation.DELETE_TITLE,
421 this.modalTranslation.deleteText(operation.name),
422 'deleteOperationModal',
423 [deleteButton, cancelButton],
427 private createOperation = (operation: OperationModel): void => {
428 this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: OperationModel) => {
429 this.openOperation = null;
431 let curInterf = this.interfaces.find((interf) => interf.type === operation.interfaceType);
434 curInterf = new UIInterfaceModel({
435 type: response.interfaceType,
436 uniqueId: response.uniqueId,
439 this.interfaces.push(curInterf);
442 const newOpModel = new UIOperationModel(response);
443 curInterf.operations.push(newOpModel);
444 this.sortInterfaces();
446 if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL && operation.artifactData) {
447 this.ComponentServiceNg2.uploadInterfaceOperationArtifact(this.component, newOpModel, operation).subscribe();
448 } else if (response.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
449 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, response).subscribe();
450 } else if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW) {
451 this.$state.go('workspace.plugins', {path: 'workflowDesigner'});
456 private enableOrDisableSaveButton = (shouldEnable: boolean): void => {
457 const saveButton = this.modalInstance.instance.dynamicContent.getButtonById('saveButton');
458 saveButton.disabled = !shouldEnable;
461 private sortInterfaces(): void {
462 this.interfaces = this.interfaces.filter((interf) => interf.operations && interf.operations.length > 0); // remove empty interfaces
463 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
464 this.interfaces.forEach((interf) => {
465 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically