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';
24 TopologyTemplateService
25 } from '../../../services/component-services/topology-template.service';
26 import {TranslateService} from "../../../shared/translator/translate.service";
27 import {ModalService} from 'app/ng2/services/modal.service';
28 import {CompositionService} from "app/ng2/pages/composition/composition.service";
29 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
30 import {Component as TopologyTemplate} from "../../../../models/components/component";
31 import {PluginsService} from "app/ng2/services/plugins.service";
32 import {SelectedComponentType} from "../common/store/graph.actions";
33 import {InstanceFeDetails} from "../../../../models/instance-fe-details";
34 import {WorkspaceService} from "../../workspace/workspace.service";
36 ComponentInterfaceDefinitionModel,
37 InterfaceOperationModel
38 } from "../../../../models/interfaceOperation";
40 InterfaceOperationHandlerComponent
41 } from "./operation-creator/interface-operation-handler.component";
52 import {ArtifactGroupType} from "../../../../utils/constants";
55 } from "../../../components/ui/form-components/dropdown/ui-element-dropdown.component";
56 import {ToscaArtifactService} from "../../../services/tosca-artifact.service";
57 import {ToscaArtifactModel} from "../../../../models/toscaArtifact";
59 export class UIInterfaceOperationModel extends InterfaceOperationModel {
60 isCollapsed: boolean = true;
64 constructor(operation: InterfaceOperationModel) {
67 if (!operation.description) {
68 this.description = '';
71 if (this.description.length > this.MAX_LENGTH) {
72 this.isEllipsis = true;
74 this.isEllipsis = false;
78 getDescriptionEllipsis(): string {
79 if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
80 return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
82 return this.description;
87 this.isCollapsed = !this.isCollapsed;
91 class ModalTranslation {
93 CANCEL_BUTTON: string;
97 constructor(private translateService: TranslateService) {
98 this.translateService.languageChangedObservable.subscribe(lang => {
99 this.EDIT_TITLE = this.translateService.translate('INTERFACE_EDIT_TITLE');
100 this.CANCEL_BUTTON = this.translateService.translate("INTERFACE_CANCEL_BUTTON");
101 this.CLOSE_BUTTON = this.translateService.translate("INTERFACE_CLOSE_BUTTON");
102 this.SAVE_BUTTON = this.translateService.translate("INTERFACE_SAVE_BUTTON");
107 export class UIInterfaceModel extends ComponentInterfaceDefinitionModel {
108 isCollapsed: boolean = false;
110 constructor(interf?: any) {
112 this.operations = _.map(
114 (operation) => new UIInterfaceOperationModel(operation)
119 this.isCollapsed = !this.isCollapsed;
124 selector: 'app-interface-operations',
125 templateUrl: './interface-operations.component.html',
126 styleUrls: ['./interface-operations.component.less'],
127 providers: [ModalService, TranslateService]
129 export class InterfaceOperationsComponent {
130 interfaces: UIInterfaceModel[];
131 inputs: Array<InputBEModel>;
133 interfaceTypes: { [interfaceType: string]: string[] };
134 topologyTemplate: TopologyTemplate;
135 componentMetaData: ComponentMetadata;
136 componentInstanceSelected: ComponentInstance;
137 modalInstance: ComponentRef<ModalComponent>;
138 modalTranslation: ModalTranslation;
139 componentInstancesInterfaces: Map<string, InterfaceModel[]>;
141 deploymentArtifactsFilePath: Array<DropdownValue> = [];
142 toscaArtifactTypes: Array<DropdownValue> = [];
143 componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
144 validImplementationProps: boolean = true;
146 @Input() component: ComponentInstance;
147 @Input() isViewOnly: boolean;
148 @Input() enableMenuItems: Function;
149 @Input() disableMenuItems: Function;
150 @Input() componentType: SelectedComponentType;
154 private translateService: TranslateService,
155 private pluginsService: PluginsService,
156 private topologyTemplateService: TopologyTemplateService,
157 private toscaArtifactService: ToscaArtifactService,
158 private modalServiceNg2: ModalService,
159 private compositionService: CompositionService,
160 private workspaceService: WorkspaceService,
161 @Inject("Notification") private Notification: any,
163 this.modalTranslation = new ModalTranslation(translateService);
167 this.componentMetaData = this.workspaceService.metadata;
168 this.loadComponentInstances();
169 this.loadDeployedArtifacts();
170 this.loadToscaArtifacts()
173 private loadComponentInstances() {
174 this.isLoading = true;
175 this.topologyTemplateService.getComponentInstances(this.componentMetaData.componentType, this.componentMetaData.uniqueId)
176 .subscribe((response) => {
177 this.componentInstanceSelected = response.componentInstances.find(ci => ci.uniqueId === this.component.uniqueId);
178 this.initComponentInstanceInterfaceOperations();
179 this.isLoading = false;
183 private initComponentInstanceInterfaceOperations() {
184 this.initInterfaces(this.componentInstanceSelected.interfaces);
185 this.sortInterfaces();
188 private initInterfaces(interfaces: ComponentInterfaceDefinitionModel[]): void {
189 this.interfaces = _.map(interfaces, (interfaceModel) => new UIInterfaceModel(interfaceModel));
192 private sortInterfaces(): void {
193 this.interfaces = _.filter(this.interfaces, (interf) => interf.operations && interf.operations.length > 0); // remove empty interfaces
194 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
195 _.forEach(this.interfaces, (interf) => {
196 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically
200 collapseAll(value: boolean = true): void {
201 _.forEach(this.interfaces, (interf) => {
202 interf.isCollapsed = value;
206 isAllCollapsed(): boolean {
207 return _.every(this.interfaces, (interf) => interf.isCollapsed);
210 isAllExpanded(): boolean {
211 return _.every(this.interfaces, (interf) => !interf.isCollapsed);
214 isListEmpty(): boolean {
217 (interf) => interf.operations && interf.operations.length > 0
221 private disableSaveButton = (): boolean => {
222 let disable:boolean = true;
223 if(this.isViewOnly) {
227 let enableAddArtifactImplementation = this.modalInstance.instance.dynamicContent.instance.enableAddArtifactImplementation;
228 if(enableAddArtifactImplementation) {
229 const validImplementationProps = this.modalInstance.instance.dynamicContent.instance.validImplementationProps;
230 const toscaArtifactTypeSelected = this.modalInstance.instance.dynamicContent.instance.toscaArtifactTypeSelected;
231 const isToscaArtifactType:boolean = !(typeof toscaArtifactTypeSelected == 'undefined' || _.isEmpty(toscaArtifactTypeSelected));
232 disable = !isToscaArtifactType || !validImplementationProps;
239 onSelectInterfaceOperation(interfaceModel: UIInterfaceModel, operation: InterfaceOperationModel) {
241 const buttonList = [];
242 if (this.isViewOnly) {
243 const closeButton: ButtonModel = new ButtonModel(this.modalTranslation.CLOSE_BUTTON, 'outline white', this.cancelAndCloseModal);
244 buttonList.push(closeButton);
246 const saveButton: ButtonModel = new ButtonModel(this.modalTranslation.SAVE_BUTTON, 'blue', () =>
247 this.updateInterfaceOperation(), this.disableSaveButton);
248 const cancelButton: ButtonModel = new ButtonModel(this.modalTranslation.CANCEL_BUTTON, 'outline white', this.cancelAndCloseModal);
249 buttonList.push(saveButton);
250 buttonList.push(cancelButton);
252 const modalModel: ModalModel = new ModalModel('l', this.modalTranslation.EDIT_TITLE, '', buttonList, 'custom');
253 this.modalInstance = this.modalServiceNg2.createCustomModal(modalModel);
255 const componentInstances = this.compositionService.getComponentInstances()
256 if (componentInstances) {
257 componentInstances.forEach(value => {
258 this.componentInstanceMap.set(value.uniqueId, <InstanceFeDetails>{
264 this.modalServiceNg2.addDynamicContentToModal(
266 InterfaceOperationHandlerComponent,
268 deploymentArtifactsFilePath: this.deploymentArtifactsFilePath,
269 componentInstanceMap: this.componentInstanceMap,
270 toscaArtifactTypes: this.toscaArtifactTypes,
271 selectedInterface: interfaceModel ? interfaceModel : new UIInterfaceModel(),
272 selectedInterfaceOperation: operation ? operation : new InterfaceOperationModel(),
273 validityChangedCallback: this.disableSaveButton,
274 isViewOnly: this.isViewOnly,
275 validImplementationProps: this.validImplementationProps,
277 modelName: this.componentMetaData.model
280 this.modalInstance.instance.open();
283 private cancelAndCloseModal = () => {
284 this.loadComponentInstances();
285 return this.modalServiceNg2.closeCurrentModal();
288 private updateInterfaceOperation() {
289 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = true;
290 const interfaceOperationHandlerComponentInstance: InterfaceOperationHandlerComponent = this.modalInstance.instance.dynamicContent.instance;
291 const operationUpdated: InterfaceOperationModel = interfaceOperationHandlerComponentInstance.operationToUpdate;
292 const isArtifactChecked = interfaceOperationHandlerComponentInstance.enableAddArtifactImplementation;
293 if (!isArtifactChecked) {
294 let artifactName = interfaceOperationHandlerComponentInstance.artifactName;
295 artifactName = artifactName === undefined ? '' : artifactName;
296 operationUpdated.implementation = new ArtifactModel({'artifactName': artifactName, 'artifactVersion': ''} as ArtifactModel);
298 this.topologyTemplateService.updateComponentInstanceInterfaceOperation(
299 this.componentMetaData.uniqueId,
300 this.componentMetaData.componentType,
301 this.componentInstanceSelected.uniqueId,
303 .subscribe((updatedComponentInstance: ComponentInstance) => {
304 this.componentInstanceSelected = new ComponentInstance(updatedComponentInstance);
305 this.initComponentInstanceInterfaceOperations();
306 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
307 this.modalServiceNg2.closeCurrentModal();
309 this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
310 this.modalServiceNg2.closeCurrentModal();
314 loadDeployedArtifacts() {
315 this.topologyTemplateService.getArtifactsByType(this.componentMetaData.componentType, this.componentMetaData.uniqueId, ArtifactGroupType.DEPLOYMENT)
316 .subscribe(response => {
317 let artifactsDeployment = response.deploymentArtifacts;
318 if (artifactsDeployment) {
319 let deploymentArtifactsFound = <ArtifactModel[]>_.values(artifactsDeployment)
320 deploymentArtifactsFound.forEach(value => {
321 this.deploymentArtifactsFilePath.push(new DropdownValue(value, value.artifactType.concat('->').concat(value.artifactName)));
325 this.Notification.error({
326 message: 'Failed to Load the Deployed Artifacts:' + error,
332 loadToscaArtifacts() {
333 this.toscaArtifactService.getToscaArtifacts(this.componentMetaData.model).subscribe(response => {
335 let toscaArtifactsFound = <ToscaArtifactModel[]>_.values(response);
336 toscaArtifactsFound.forEach(value => this.toscaArtifactTypes.push(new DropdownValue(value, value.type)));
339 this.Notification.error({
340 message: 'Failed to Load Tosca Artifacts:' + error,