aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/panel/panel-tabs/properties-tab/properties-tab.component.ts
blob: b4b8248ed02aef8e11625d36609601190c1a6966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Component, Input, OnInit } from '@angular/core';
import { Store } from '@ngxs/store';
import {
    AttributeModel,
    AttributesGroup,
    Component as TopologyTemplate,
    ComponentMetadata,
    FullComponentInstance,
    PropertiesGroup,
    PropertyModel
} from 'app/models';
import { CompositionService } from 'app/ng2/pages/composition/composition.service';
import { WorkspaceService } from 'app/ng2/pages/workspace/workspace.service';
import { GroupByPipe } from 'app/ng2/pipes/groupBy.pipe';
import { ResourceNamePipe } from 'app/ng2/pipes/resource-name.pipe';
import { TopologyTemplateService } from 'app/ng2/services/component-services/topology-template.service';
import { ComponentGenericResponse } from 'app/ng2/services/responses/component-generic-response';
import { TranslateService } from 'app/ng2/shared/translator/translate.service';
import { ModalsHandler } from 'app/utils';
import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
import {SelectedComponentType, TogglePanelLoadingAction} from "../../../common/store/graph.actions";

@Component({
    selector: 'properties-tab',
    templateUrl: './properties-tab.component.html',
    styleUrls: ['./properties-tab.component.less']
})
export class PropertiesTabComponent implements OnInit {
    attributes: AttributesGroup;
    isComponentInstanceSelected: boolean;
    properties: PropertiesGroup;
    groupPropertiesByInstance: boolean;
    propertiesMessage: string;
    metadata: ComponentMetadata;
    objectKeys = Object.keys;

    @Input() isViewOnly: boolean;
    @Input() componentType: SelectedComponentType;
    @Input() component: FullComponentInstance | TopologyTemplate;
    @Input() input: {title: string};

    constructor(private store: Store,
                private workspaceService: WorkspaceService,
                private compositionService: CompositionService,
                private modalsHandler: ModalsHandler,
                private topologyTemplateService: TopologyTemplateService,
                private modalService: SdcUiServices.ModalService,
                private translateService: TranslateService,
                private groupByPipe: GroupByPipe) {
    }

    ngOnInit() {
        this.metadata = this.workspaceService.metadata;
        this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
        this.getComponentInstancesPropertiesAndAttributes();
    }

    public isPropertyOwner = (): boolean => {
        return this.component instanceof TopologyTemplate && this.component.isResource();
    }

    public updateProperty = (property: PropertyModel): void => {
        this.openEditPropertyModal(property);
    }

    public deleteProperty = (property: PropertyModel): void => {

        const onOk: Function = (): void => {
            this.store.dispatch(new TogglePanelLoadingAction({isLoading: true}));
            this.topologyTemplateService.deleteProperty(this.component.componentType, this.component.uniqueId, property.uniqueId)
                .subscribe((response) => {
                    this.store.dispatch(new TogglePanelLoadingAction({isLoading: false}));
                    this.component.properties = this.component.properties.filter((prop) => prop.uniqueId !==  property.uniqueId);
                    this.initComponentProperties();
                }, () => {
                    this.store.dispatch(new TogglePanelLoadingAction({isLoading: false}));
                });
        };

        const title: string = this.translateService.translate('PROPERTY_VIEW_DELETE_MODAL_TITLE');
        const message: string = this.translateService.translate('PROPERTY_VIEW_DELETE_MODAL_TEXT', {name: property.name});
        const okButton = {
            testId: 'OK',
            text: 'OK',
            type: SdcUiCommon.ButtonType.info,
            callback: onOk,
            closeModal: true} as SdcUiComponents.ModalButtonComponent;
        this.modalService.openInfoModal(title, message, 'delete-modal', [okButton]);
    }

    public groupNameByKey = (key: string): string => {
        switch (key) {
            case 'derived':
                return 'Derived';

            case this.metadata.uniqueId:
                return ResourceNamePipe.getDisplayName(this.metadata.name);

            default:
                return this.getComponentInstanceNameFromInstanceByKey(key);
        }
    }

    public getComponentInstanceNameFromInstanceByKey = (key: string): string => {
        let instanceName: string = '';
        const componentInstance = this.compositionService.getComponentInstances().find((item) => item.uniqueId === key);
        if (key !== undefined && componentInstance) {

            instanceName = ResourceNamePipe.getDisplayName(componentInstance.name);
        }
        return instanceName;
    }

    private getComponentInstancesPropertiesAndAttributes = () => {
        this.topologyTemplateService.getComponentInstanceAttributesAndProperties(
            this.workspaceService.metadata.uniqueId,
            this.workspaceService.metadata.componentType)
            .subscribe((genericResponse: ComponentGenericResponse) => {
                this.compositionService.componentInstancesAttributes = genericResponse.componentInstancesAttributes || new AttributesGroup();
                this.compositionService.componentInstancesProperties = genericResponse.componentInstancesProperties;
                this.initPropertiesAndAttributes();
            });
    }

    private initComponentProperties = (): void => {
        let result: PropertiesGroup = {};

        this.propertiesMessage = undefined;
        this.groupPropertiesByInstance = false;
        if (this.component instanceof FullComponentInstance) {
            result[this.component.uniqueId] = _.orderBy(this.compositionService.componentInstancesProperties[this.component.uniqueId], ['name']);
            if (this.component.originType === 'VF') {
                this.groupPropertiesByInstance = true;
                result[this.component.uniqueId] = Array.from(this.groupByPipe.transform(result[this.component.uniqueId], 'path'));
            }
        } else if (this.metadata.isService()) {
            // Temporally fix to hide properties for service (UI stack when there are many properties)
            result = this.compositionService.componentInstancesProperties;
            this.propertiesMessage = 'Note: properties for service are disabled';
        } else {
            const componentUid = this.component.uniqueId;
            result[componentUid] = Array<PropertyModel>();
            const derived = Array<PropertyModel>();
            _.forEach(this.component.properties, (property: PropertyModel) => {
                if (componentUid === property.parentUniqueId) {
                    result[componentUid].push(property);
                } else {
                    property.readonly = true;
                    derived.push(property);
                }
            });
            if (derived.length) {
                result['derived'] = derived;
            }
            this.objectKeys(result).forEach((key) => { result[key] =  _.orderBy(result[key], ['name']); });
        }
        this.properties = result;
    }

    private initComponentAttributes = (): void => {
        let result: AttributesGroup = {};

        if (this.component) {
            if (this.component instanceof FullComponentInstance) {
                result[this.component.uniqueId] = this.compositionService.componentInstancesAttributes[this.component.uniqueId] || [];
            } else if (this.metadata.isService()) {
                result = this.compositionService.componentInstancesAttributes;
            } else {
                result[this.component.uniqueId] = (this.component as TopologyTemplate).attributes;
            }
            this.attributes = result;
            this.objectKeys(this.attributes).forEach((key) => {
                this.attributes[key] =  _.orderBy(this.attributes[key], ['name']);
            });

        }
    }

    /**
     * This function is checking if the component is the value owner of the current property
     * in order to notify the edit property modal which fields to disable
     */
    private isPropertyValueOwner = (): boolean => {
        return this.metadata.isService() || !!this.component;
    }

    /**
     *  The function opens the edit property modal.
     *  It checks if the property is from the VF or from one of it's resource instances and sends the needed property list.
     *  For create property reasons an empty array is transferd
     *
     * @param property the wanted property to edit/create
     */
    private openEditPropertyModal = (property: PropertyModel): void => {
        this.modalsHandler.newOpenEditPropertyModal(property,
            (this.isPropertyOwner() ?
                this.properties[property.parentUniqueId] :
                this.properties[property.resourceInstanceUniqueId]) || [],
            this.isPropertyValueOwner(), 'component', property.resourceInstanceUniqueId).then((updatedProperty: PropertyModel) => {
                if (updatedProperty) {
                    const oldProp = _.find(this.properties[updatedProperty.resourceInstanceUniqueId],
                                 (prop: PropertyModel) => prop.uniqueId === updatedProperty.uniqueId);
                    oldProp.value = updatedProperty.value;
                }
        });
    }

    private initPropertiesAndAttributes = (): void => {
        this.initComponentProperties();
        this.initComponentAttributes();
    }
}