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, ComponentRef, Inject, Input} from '@angular/core';
23 import {TopologyTemplateService} from '../../../services/component-services/topology-template.service';
24 import {TranslateService} from "../../../shared/translator/translate.service";
25 import {ModalService } from 'app/ng2/services/modal.service';
26 import { ModalComponent } from 'app/ng2/components/ui/modal/modal.component';
28 Component as TopologyTemplate
29 } from "../../../../models/components/component";
30 import {PluginsService} from "app/ng2/services/plugins.service";
31 import {SelectedComponentType} from "../common/store/graph.actions";
33 import {WorkspaceService} from "../../workspace/workspace.service";
35 ComponentInstanceInterfaceModel,
36 InterfaceOperationModel
37 } from "../../../../models/interfaceOperation";
39 InterfaceOperationHandlerComponent
40 } from "./operation-creator/interface-operation-handler.component";
48 ComponentInstance, ArtifactModel
50 import {ArtifactGroupType} from "../../../../utils/constants";
51 import {DropdownValue} from "../../../components/ui/form-components/dropdown/ui-element-dropdown.component";
52 import {ToscaArtifactService} from "../../../services/tosca-artifact.service";
53 import {ToscaArtifactModel} from "../../../../models/toscaArtifact";
55 export class UIInterfaceOperationModel extends InterfaceOperationModel {
56 isCollapsed: boolean = true;
59 constructor(operation: InterfaceOperationModel) {
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 {
88 CANCEL_BUTTON: string;
91 constructor(private TranslateService: TranslateService) {
92 this.TranslateService.languageChangedObservable.subscribe(lang => {
93 this.EDIT_TITLE = this.TranslateService.translate('INTERFACE_EDIT_TITLE');
94 this.CANCEL_BUTTON = this.TranslateService.translate("INTERFACE_CANCEL_BUTTON");
95 this.SAVE_BUTTON = this.TranslateService.translate("INTERFACE_SAVE_BUTTON");
100 export class UIInterfaceModel extends ComponentInstanceInterfaceModel {
101 isCollapsed: boolean = false;
103 constructor(interf?: any) {
105 this.operations = _.map(
107 (operation) => new UIInterfaceOperationModel(operation)
112 this.isCollapsed = !this.isCollapsed;
117 selector: 'app-interface-operations',
118 templateUrl: './interface-operations.component.html',
119 styleUrls: ['./interface-operations.component.less'],
120 providers: [ModalService, TranslateService]
122 export class InterfaceOperationsComponent {
123 interfaces: UIInterfaceModel[];
124 inputs: Array<InputBEModel>;
126 interfaceTypes: { [interfaceType: string]: string[] };
127 topologyTemplate: TopologyTemplate;
128 componentMetaData: ComponentMetadata;
129 componentInstanceSelected: ComponentInstance;
130 modalInstance: ComponentRef<ModalComponent>;
131 modalTranslation: ModalTranslation;
132 componentInstancesInterfaces: Map<string, InterfaceModel[]>;
134 deploymentArtifactsFilePath: Array<DropdownValue> = [];
135 toscaArtifactTypes: Array<DropdownValue> = [];
137 @Input() component: ComponentInstance;
138 @Input() readonly: boolean;
139 @Input() enableMenuItems: Function;
140 @Input() disableMenuItems: Function;
141 @Input() componentType: SelectedComponentType;
145 private TranslateService: TranslateService,
146 private PluginsService: PluginsService,
147 private topologyTemplateService: TopologyTemplateService,
148 private toscaArtifactService: ToscaArtifactService,
149 private modalServiceNg2: ModalService,
150 private workspaceService: WorkspaceService,
151 @Inject("Notification") private Notification: any,
153 this.modalTranslation = new ModalTranslation(TranslateService);
157 this.componentMetaData = this.workspaceService.metadata;
158 this.loadComponentInstances();
159 this.loadDeployedArtifacts();
160 this.loadToscaArtifacts()
163 private loadComponentInstances() {
164 this.isLoading = true;
165 this.topologyTemplateService.getComponentInstances(this.componentMetaData.componentType, this.componentMetaData.uniqueId)
166 .subscribe((response) => {
167 this.componentInstanceSelected = response.componentInstances.find(ci => ci.uniqueId === this.component.uniqueId);
168 this.initComponentInstanceInterfaceOperations();
169 this.isLoading = false;
173 private initComponentInstanceInterfaceOperations() {
174 this.initInterfaces(this.componentInstanceSelected.interfaces);
175 this.sortInterfaces();
178 private initInterfaces(interfaces: ComponentInstanceInterfaceModel[]): void {
179 this.interfaces = _.map(interfaces, (interfaceModel) => new UIInterfaceModel(interfaceModel));
182 private sortInterfaces(): void {
183 this.interfaces = _.filter(this.interfaces, (interf) => interf.operations && interf.operations.length > 0); // remove empty interfaces
184 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
185 _.forEach(this.interfaces, (interf) => {
186 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically
190 collapseAll(value: boolean = true): void {
191 _.forEach(this.interfaces, (interf) => {
192 interf.isCollapsed = value;
196 isAllCollapsed(): boolean {
197 return _.every(this.interfaces, (interf) => interf.isCollapsed);
200 isAllExpanded(): boolean {
201 return _.every(this.interfaces, (interf) => !interf.isCollapsed);
204 isListEmpty(): boolean {
207 (interf) => interf.operations && interf.operations.length > 0
211 private enableOrDisableSaveButton = (): boolean => {
212 return this.modalInstance.instance.dynamicContent.instance.readonly;
215 onSelectInterfaceOperation(interfaceModel: UIInterfaceModel, operation: InterfaceOperationModel) {
216 const cancelButton: ButtonModel = new ButtonModel(this.modalTranslation.CANCEL_BUTTON, 'outline white', this.cancelAndCloseModal);
217 const saveButton: ButtonModel = new ButtonModel(this.modalTranslation.SAVE_BUTTON, 'blue', () =>
218 this.updateInterfaceOperation(), this.enableOrDisableSaveButton);
219 const modalModel: ModalModel = new ModalModel('l', this.modalTranslation.EDIT_TITLE, '', [saveButton, cancelButton], 'custom');
220 this.modalInstance = this.modalServiceNg2.createCustomModal(modalModel);
222 this.modalServiceNg2.addDynamicContentToModal(
224 InterfaceOperationHandlerComponent,
226 deploymentArtifactsFilePath: this.deploymentArtifactsFilePath,
227 toscaArtifactTypes: this.toscaArtifactTypes,
228 selectedInterface: interfaceModel,
229 selectedInterfaceOperation: operation,
230 validityChangedCallback: this.enableOrDisableSaveButton
233 this.modalInstance.instance.open();
236 private cancelAndCloseModal = () => {
237 this.loadComponentInstances();
238 return this.modalServiceNg2.closeCurrentModal();
241 private updateInterfaceOperation() {
242 this.isLoading = true;
243 let operationUpdated = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
244 this.topologyTemplateService.updateComponentInstanceInterfaceOperation(
245 this.componentMetaData.uniqueId,
246 this.componentMetaData.componentType,
247 this.componentInstanceSelected.uniqueId,
248 operationUpdated).subscribe((updatedComponentInstance: ComponentInstance) => {
249 this.componentInstanceSelected = new ComponentInstance(updatedComponentInstance);
250 this.initComponentInstanceInterfaceOperations();
252 this.modalServiceNg2.closeCurrentModal();
253 this.isLoading = false;
256 loadDeployedArtifacts() {
257 this.topologyTemplateService.getArtifactsByType(this.componentMetaData.componentType, this.componentMetaData.uniqueId, ArtifactGroupType.DEPLOYMENT)
258 .subscribe(response => {
259 let artifactsDeployment = response.deploymentArtifacts;
260 if (artifactsDeployment) {
261 let deploymentArtifactsFound = <ArtifactModel[]>_.values(artifactsDeployment)
262 deploymentArtifactsFound.forEach(value => {
263 this.deploymentArtifactsFilePath.push(new DropdownValue(value, value.artifactType.concat('->').concat(value.artifactName)));
266 this.Notification.error({
267 message: 'Failed to Load the Deployed Artifacts:' + error,
273 loadToscaArtifacts() {
274 this.toscaArtifactService.getToscaArtifacts(this.componentMetaData.model).subscribe(response => {
276 let toscaArtifactsFound = <ToscaArtifactModel[]>_.values(response);
277 toscaArtifactsFound.forEach(value => this.toscaArtifactTypes.push(new DropdownValue(value, value.type)));
280 this.Notification.error({
281 message: 'Failed to Load Tosca Artifacts:' + error,