summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/panel/panel-tabs/artifacts-tab/artifacts-tab.component.ts
blob: 53a6c267e2a8fb891d0142fb099123d59a199358 (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
import { Component, Input } from '@angular/core';
import { Store } from '@ngxs/store';
import { ArtifactModel, Component as TopologyTemplate, FullComponentInstance, Resource } from 'app/models';
import { WorkspaceService } from 'app/ng2/pages/workspace/workspace.service';
import { ResourceNamePipe } from 'app/ng2/pipes/resource-name.pipe';
import { ComponentInstanceServiceNg2 } from 'app/ng2/services/component-instance-services/component-instance.service';
import { TopologyTemplateService } from 'app/ng2/services/component-services/topology-template.service';
import { ArtifactType } from 'app/utils';
import * as _ from 'lodash';
import { SdcUiServices } from 'onap-ui-angular';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';
import { ArtifactsService } from '../../../../../components/forms/artifacts-form/artifacts.service';
import { GetArtifactsByTypeAction } from '../../../../../store/actions/artifacts.action';
import { GetInstanceArtifactsByTypeAction } from '../../../../../store/actions/instance-artifacts.actions';
import { ArtifactsState } from '../../../../../store/states/artifacts.state';
import { InstanceArtifactsState } from '../../../../../store/states/instance-artifacts.state';
import { SelectedComponentType, TogglePanelLoadingAction } from '../../../common/store/graph.actions';
import { CompositionService } from '../../../composition.service';

@Component({
    selector: 'artifacts-tab',
    styleUrls: ['./artifacts-tab.component.less'],
    templateUrl: './artifacts-tab.component.html',
    providers: [SdcUiServices.ModalService]
})

export class ArtifactsTabComponent {

    @Input() component: FullComponentInstance | TopologyTemplate;
    @Input() isViewOnly: boolean;
    @Input() input: any;
    @Input() componentType: SelectedComponentType;

    public title: string;
    public type: string;
    public isComponentInstanceSelected: boolean;
    public artifacts$: Observable<ArtifactModel[]>;
    private topologyTemplateType: string;
    private topologyTemplateId: string;
    private heatToEnv: Map<string, ArtifactModel>;
    private resourceType: string;
    private isComplex: boolean;

    constructor(private store: Store,
                private compositionService: CompositionService,
                private workspaceService: WorkspaceService,
                private componentInstanceService: ComponentInstanceServiceNg2,
                private topologyTemplateService: TopologyTemplateService,
                private artifactService: ArtifactsService) {
        this.heatToEnv = new Map();
    }

    ngOnInit() {
        this.topologyTemplateType = this.workspaceService.metadata.componentType;
        this.topologyTemplateId = this.workspaceService.metadata.uniqueId;
        this.type = this.input.type;
        this.title = this.getTitle(this.type);
        this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
        this.resourceType = this.component['resourceType'];
        this.isComplex = this.component.isComplex();
        this.loadArtifacts();
    }

    public addOrUpdate = (artifact: ArtifactModel): void => {
        if (this.isComponentInstanceSelected) {
            this.artifactService.openArtifactModal(this.topologyTemplateId, this.topologyTemplateType, artifact, this.type, this.isViewOnly, this.component.uniqueId);
        } else {
            this.artifactService.openArtifactModal(this.topologyTemplateId, this.topologyTemplateType, artifact, this.type, this.isViewOnly);
        }
    }

    public updateEnvParams = (artifact: ArtifactModel) => {
        if (this.isComponentInstanceSelected) {
            this.artifactService.openUpdateEnvParams(this.topologyTemplateType, this.topologyTemplateId, this.heatToEnv.get(artifact.uniqueId), this.component.uniqueId);
        } else {
            this.artifactService.openUpdateEnvParams(this.topologyTemplateType, this.topologyTemplateId, artifact);
        }
    }

    public viewEnvParams = (artifact: ArtifactModel) => {
        if (this.isComponentInstanceSelected) {
            this.artifactService.openViewEnvParams(this.topologyTemplateType, this.topologyTemplateId, this.heatToEnv.get(artifact.uniqueId), this.component.uniqueId);
        } else {
            this.artifactService.openViewEnvParams(this.topologyTemplateType, this.topologyTemplateId, artifact);
        }
    }

    public getEnvArtifact = (heatArtifact: ArtifactModel, artifacts: ArtifactModel[]): ArtifactModel => {
        const envArtifact = _.find(artifacts, (item: ArtifactModel) => {
            return item.generatedFromId === heatArtifact.uniqueId;
        });
        if (envArtifact && heatArtifact) {
            envArtifact.artifactDisplayName = heatArtifact.artifactDisplayName;
            envArtifact.timeout = heatArtifact.timeout;
        }
        return envArtifact;
    }

    public delete = (artifact: ArtifactModel): void => {
        if (this.isComponentInstanceSelected) {
            this.artifactService.deleteArtifact(this.topologyTemplateType, this.topologyTemplateId, artifact, this.component.uniqueId);
        } else {
            this.artifactService.deleteArtifact(this.topologyTemplateType, this.topologyTemplateId, artifact);
        }
    }

    public isVfOrPnf(): boolean {
        if (this.component.isResource()){
            if (this.resourceType) {
                return this.resourceType === 'VF' || this.resourceType == 'PNF';
            }
            return false;
        }

        return false;
    }

    private envArtifactOf(artifact: ArtifactModel): ArtifactModel {
        return this.heatToEnv.get(artifact.uniqueId);
    }

    private isLicenseArtifact = (artifact: ArtifactModel): boolean => {
        let isLicense: boolean = false;
        if (this.component.isResource && (this.component as Resource).isCsarComponent) {
            if (ArtifactType.VENDOR_LICENSE === artifact.artifactType || ArtifactType.VF_LICENSE === artifact.artifactType) {
                isLicense = true;
            }
        }

        return isLicense;
    }

    private getTitle = (artifactType: string): string => {
        switch (artifactType) {
            case ArtifactType.SERVICE_API:
                return 'API Artifacts';
            case ArtifactType.DEPLOYMENT:
                return 'Deployment Artifacts';
            case ArtifactType.INFORMATION:
                return 'Informational Artifacts';
            default:
                return ResourceNamePipe.getDisplayName(artifactType) + ' Artifacts';
        }
    }

    private loadArtifacts = (forceLoad?: boolean): void => {

        this.store.dispatch(new TogglePanelLoadingAction({isLoading: true}));

        let action;
        if (this.isComponentInstanceSelected) {
            action = new GetInstanceArtifactsByTypeAction(({
                componentType: this.topologyTemplateType,
                componentId: this.topologyTemplateId,
                instanceId: this.component.uniqueId,
                artifactType: this.type
            }));
        } else {
            action = new GetArtifactsByTypeAction({
                componentType: this.topologyTemplateType,
                componentId: this.topologyTemplateId,
                artifactType: this.type
            });
        }
        this.store.dispatch(action).subscribe(() => {
            const stateSelector = this.isComponentInstanceSelected ? InstanceArtifactsState.getArtifactsByType : ArtifactsState.getArtifactsByType;
            this.artifacts$ = this.store.select(stateSelector).pipe(map((filterFn) => filterFn(this.type))).pipe(map((artifacts) => {
                _.forEach(artifacts, (artifact: ArtifactModel): void => {
                    const envArtifact = this.getEnvArtifact(artifact, artifacts); // Extract the env artifact (if exist) of the HEAT artifact
                    if (envArtifact) {
                        // Set a mapping between HEAT to HEAT_ENV
                        this.heatToEnv.set(artifact.uniqueId, envArtifact);
                    }
                });
                return _.orderBy(artifacts, ['mandatory', 'artifactDisplayName'], ['desc', 'asc']);
            }));

            this.artifacts$.subscribe((artifacts) => {
                _.forEach(artifacts, (artifact: ArtifactModel) => {
                    artifact.allowDeleteAndUpdate = this.allowDeleteAndUpdateArtifact(artifact);
                });
                if (this.component instanceof FullComponentInstance) {
                    this.compositionService.updateInstance(this.component);
                }
            });
            this.store.dispatch(new TogglePanelLoadingAction({isLoading: false}));
        }, () => {
            this.store.dispatch(new TogglePanelLoadingAction({isLoading: false}));
        });
    }

    private allowDeleteAndUpdateArtifact = (artifact: ArtifactModel): boolean => {
        if (!this.isViewOnly) {
            if (artifact.artifactGroupType === ArtifactType.DEPLOYMENT) {
                return !artifact.isFromCsar;
            } else {

                return (!artifact.isHEAT() && !artifact.isThirdParty() && !this.isLicenseArtifact(artifact));
            }
        }
        return false;
    }
}