CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / tabs / artifacts / artifacts-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import {
24     ArtifactModel,
25     Service,
26     IAppConfigurtaion,
27     Resource,
28     Component,
29     ComponentInstance,
30     ArtifactGroupModel,
31     IFileDownload
32 } from "app/models";
33 import {ICompositionViewModelScope} from "../../composition-view-model";
34 import {ArtifactsUtils, ModalsHandler, ArtifactGroupType} from "app/utils";
35 import {GRAPH_EVENTS} from "app/utils/constants";
36 import {EventListenerService} from "app/services/event-listener-service";
37 import {Dictionary} from "../../../../../../utils/dictionary/dictionary";
38
39 export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
40     artifacts:Array<ArtifactModel>;
41     artifactType:string;
42     downloadFile:IFileDownload;
43     isLoading:boolean;
44     allowDeleteAndUpdateArtifactMap:Dictionary<string, boolean>;
45     getTitle():string;
46     addOrUpdate(artifact:ArtifactModel):void;
47     delete(artifact:ArtifactModel):void;
48     download(artifact:ArtifactModel):void;
49     openEditEnvParametersModal(artifact:ArtifactModel):void;
50     getEnvArtifact(heatArtifact:ArtifactModel):any;
51     getEnvArtifactName(artifact:ArtifactModel):string;
52     isLicenseArtifact(artifact:ArtifactModel):boolean;
53     isVfOrPnf():boolean;
54     //isVFiArtifact(artifact:ArtifactModel):boolean;
55 }
56
57 export class ResourceArtifactsViewModel {
58
59     static '$inject' = [
60         '$scope',
61         '$filter',
62         '$state',
63         'sdcConfig',
64         'ArtifactsUtils',
65         'ModalsHandler',
66         '$q',
67         'EventListenerService'
68     ];
69
70     constructor(private $scope:IArtifactsViewModelScope,
71                 private $filter:ng.IFilterService,
72                 private $state:any,
73                 private sdcConfig:IAppConfigurtaion,
74                 private artifactsUtils:ArtifactsUtils,
75                 private ModalsHandler:ModalsHandler,
76                 private $q:ng.IQService,
77                 private eventListenerService: EventListenerService) {
78
79         this.initScope();
80     }
81
82
83     private initArtifactArr = (artifactType:string):void => {
84         let artifacts:Array<ArtifactModel> = [];
85
86         if (this.$scope.selectedComponent) {
87             if ('interface' == artifactType) {
88                 let interfaces = this.$scope.currentComponent.interfaces;
89                 if (interfaces && interfaces.standard && interfaces.standard.operations) {
90
91                     angular.forEach(interfaces.standard.operations, (operation:any, interfaceName:string):void => {
92                         let item:ArtifactModel = <ArtifactModel>{};
93                         if (operation.implementation) {
94                             item = <ArtifactModel> operation.implementation;
95                         }
96                         item.artifactDisplayName = interfaceName;
97                         item.artifactLabel = interfaceName;
98                         item.mandatory = false;
99                         artifacts.push(item);
100                     });
101                 }
102             } else {
103                 //init normal artifacts, deployment or api artifacts
104                 let artifactsObj:ArtifactGroupModel;
105                 switch (artifactType) {
106                     case "api":
107                         artifactsObj = (<Service>this.$scope.currentComponent).serviceApiArtifacts;
108                         break;
109                     case "deployment":
110                         if (!this.$scope.isComponentInstanceSelected()) {
111                             artifactsObj = this.$scope.currentComponent.deploymentArtifacts;
112                         } else {
113                             artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
114                         }
115                         break;
116                     default:
117                         //artifactsObj = this.$scope.selectedComponent.artifacts;
118                         if (!this.$scope.isComponentInstanceSelected()) {
119                             artifactsObj = this.$scope.currentComponent.artifacts;
120                         } else {
121                             artifactsObj = this.$scope.currentComponent.selectedInstance.artifacts;
122                         }
123                         break;
124                 }
125                 _.forEach(artifactsObj, (artifact:ArtifactModel, key) => {
126                     artifacts.push(artifact);
127                 });
128             }
129         }
130         this.$scope.artifacts = artifacts;
131         this.$scope.allowDeleteAndUpdateArtifactMap = new Dictionary<string, boolean>();
132         _.forEach(this.$scope.artifacts, (artifact:ArtifactModel)=>{
133             this.$scope.allowDeleteAndUpdateArtifactMap[artifact.artifactLabel] = this.allowDeleteAndUpdateArtifact(artifact);
134         });
135         this.$scope.isLoading = false;
136         this.$scope.preventMoveTab(false);
137     };
138
139
140     private convertToArtifactUrl = (artifactType:string):string => {
141
142         switch (artifactType) {
143             case 'deployment':
144                 return 'DEPLOYMENT';
145             case 'api':
146                 return 'SERVICE_API';
147             default:
148                 return 'INFORMATIONAL';
149         }
150
151     }
152
153     private loadComponentArtifactIfNeeded = (forceLoad?: boolean) => {
154
155         let onGetComponentArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
156             switch (this.$scope.artifactType) {
157                 case 'deployment':
158                     this.$scope.currentComponent.deploymentArtifacts = artifacts;
159                     break;
160                 case 'api':
161                     (<Service>this.$scope.currentComponent).serviceApiArtifacts = artifacts;
162                     break;
163                 default:
164                     this.$scope.currentComponent.artifacts = artifacts;
165                     break;
166             }
167             this.$scope.isLoading = false;
168             this.initArtifactArr(this.$scope.artifactType);
169         }
170
171         let onError = ()=> {
172             this.$scope.isLoading = false;
173         };
174
175         switch (this.$scope.artifactType) {
176             case 'deployment':
177                 if(forceLoad || !this.$scope.currentComponent.deploymentArtifacts) {
178                     this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
179                 } else {
180                     this.initArtifactArr(this.$scope.artifactType);
181                 }
182
183                 break;
184             case 'api':
185                 if(!(<Service>this.$scope.currentComponent).serviceApiArtifacts) {
186                     this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
187                 } else {
188                     this.initArtifactArr(this.$scope.artifactType);
189                 }
190                 break;
191             default:
192                 if(!this.$scope.currentComponent.artifacts) {
193                     this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
194                 } else {
195                     this.initArtifactArr(this.$scope.artifactType);
196                 }
197                 break;
198         }
199     }
200     private loadArtifacts = (forceLoad?: boolean):void => {
201
202         let onGetInstanceArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
203             switch (this.$scope.artifactType) {
204                 case 'deployment':
205                     this.$scope.currentComponent.selectedInstance.deploymentArtifacts = artifacts;
206                     break;
207                 default:
208                     this.$scope.currentComponent.selectedInstance.artifacts = artifacts;
209                     break;
210             }
211             this.initArtifactArr(this.$scope.artifactType);
212         };
213
214         let onError = ()=> {
215             this.$scope.isLoading = false;
216         };
217
218         this.$scope.isLoading = true;
219         this.$scope.preventMoveTab(true);
220         if (this.$scope.isComponentInstanceSelected()) {
221             this.$scope.component.getComponentInstanceArtifactsByGroupType(this.$scope.component.selectedInstance.uniqueId, this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetInstanceArtifactsSuccess, onError);
222         } else {
223             this.loadComponentArtifactIfNeeded(forceLoad);
224         }
225     }
226
227     private updateArtifactsIfNeeded = ():void => {
228         if (this.$scope.artifactType === "deployment") {
229             this.loadArtifacts(true);
230         } else {
231             this.initArtifactArr(this.$scope.artifactType);
232         }
233     };
234
235     private openEditArtifactModal = (artifact:ArtifactModel):void => {
236         this.ModalsHandler.openArtifactModal(artifact, this.$scope.currentComponent).then(():void => {
237             this.updateArtifactsIfNeeded();
238         });
239     };
240
241     private allowDeleteAndUpdateArtifact = (artifact:ArtifactModel):boolean => {
242     if(!this.$scope.isViewMode()){
243         if(this.$scope.isComponentInstanceSelected()){//is artifact of instance
244             return !this.$scope.selectedComponent.deploymentArtifacts || !this.$scope.selectedComponent.deploymentArtifacts[artifact.artifactLabel];//if the artifact is not from instance parent
245         }else{//is artifact of main component
246             return (!artifact.isHEAT() && !artifact.isThirdParty() && !this.$scope.isLicenseArtifact(artifact));
247         }
248     }
249     return false;
250 };
251
252     private initScope = ():void => {
253
254         this.$scope.isLoading = false;
255         this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
256         this.$scope.getTitle = ():string => {
257             return this.artifactsUtils.getTitle(this.$scope.artifactType, this.$scope.currentComponent);
258         };
259
260         // Bug 310499 - user should be unable to delete RI artifact. (also talked to David and agreed this function isn't necessary)
261         // this.$scope.isVFiArtifact = (artifact:ArtifactModel):boolean=> {
262         //     if (artifact.artifactGroupType === ArtifactGroupType.INFORMATION) {//fix DE256847
263         //         return this.$scope.currentComponent.artifacts && (!this.$scope.currentComponent.artifacts[artifact.artifactLabel] || !this.$scope.currentComponent.artifacts[artifact.artifactLabel].artifactName);
264         //     }
265         //     return this.$scope.currentComponent.selectedInstance && this.$scope.currentComponent.selectedInstance.deploymentArtifacts && this.$scope.currentComponent.selectedInstance.deploymentArtifacts[artifact.artifactLabel];
266         // };
267
268         this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
269             this.artifactsUtils.setArtifactType(artifact, this.$scope.artifactType);
270             let artifactCopy = new ArtifactModel(artifact);
271             this.openEditArtifactModal(artifactCopy);
272         };
273
274
275         this.$scope.delete = (artifact:ArtifactModel):void => {
276
277             let onOk = ():void => {
278                 this.$scope.isLoading = true;
279                 this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
280
281                 let success = (responseArtifact:ArtifactModel):void => {
282                     this.initArtifactArr(this.$scope.artifactType);
283                     this.$scope.isLoading = false;
284                 };
285
286                 let error = (error:any):void => {
287                     console.log('Delete artifact returned error:', error);
288                     this.initArtifactArr(this.$scope.artifactType);
289                     this.$scope.isLoading = false;
290                 };
291                 if (this.$scope.isComponentInstanceSelected()) {
292                     this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
293                 } else {
294                     this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
295                 }
296             };
297             let title:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
298             let message:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
299             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
300         };
301
302
303         this.$scope.getEnvArtifact = (heatArtifact:ArtifactModel):any=> {
304             return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
305                 return item.generatedFromId === heatArtifact.uniqueId;
306             });
307         };
308
309         this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
310             let envArtifact = this.$scope.getEnvArtifact(artifact);
311             if (envArtifact) {
312                 return envArtifact.artifactDisplayName;
313             }
314         };
315
316         this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
317             let isLicense:boolean = false;
318             if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
319                 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
320             }
321
322             return isLicense;
323         };
324
325         this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
326             this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.currentComponent).then(()=> {
327                 this.updateArtifactsIfNeeded();
328             }, ()=> {
329                 // ERROR
330             });
331         };
332
333         this.$scope.isVfOrPnf = ():boolean => {
334             if (this.$scope.selectedComponent.isResource()) {
335                 let selectedResourceType = (<Resource>this.$scope.selectedComponent).resourceType;
336                 return selectedResourceType == 'VF' || selectedResourceType == 'PNF';
337             }
338             return false;
339         }
340
341         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
342         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
343
344         this.$scope.$on('$destroy', () => {
345
346             this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
347             this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
348         });
349
350         this.loadArtifacts();
351     }
352 }