Initial OpenECOMP SDC commit
[sdc.git] / catalog-ui / app / scripts / 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 /// <reference path="../../../../../../references"/>
21 module Sdc.ViewModels {
22     'use strict';
23     import Resource = Sdc.Models.Components.Resource;
24
25    export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
26         artifacts: Array<Models.ArtifactModel>;
27         artifactType: string;
28         downloadFile:Models.IFileDownload;
29         isLoading:boolean;
30
31         getTitle(): string;
32         addOrUpdate(artifact:Models.ArtifactModel): void;
33         delete(artifact:Models.ArtifactModel): void;
34         download(artifact:Models.ArtifactModel): void;
35         openEditEnvParametersModal(artifact:Models.ArtifactModel):void;
36         getEnvArtifact(heatArtifact:Models.ArtifactModel):any;
37         getEnvArtifactName(artifact:Models.ArtifactModel):string;
38         isLicenseArtifact(artifact:Models.ArtifactModel):boolean;
39         isVFiArtifact(artifact:Models.ArtifactModel):boolean;
40     }
41
42     export class ResourceArtifactsViewModel {
43
44         static '$inject' = [
45             '$scope',
46             '$filter',
47             '$modal',
48             '$templateCache',
49             '$state',
50             'sdcConfig',
51             'ArtifactsUtils',
52             'ModalsHandler',
53             'Sdc.Services.CacheService'
54         ];
55
56         constructor(private $scope:IArtifactsViewModelScope,
57                     private $filter:ng.IFilterService,
58                     private $modal:ng.ui.bootstrap.IModalService,
59                     private $templateCache:ng.ITemplateCacheService,
60                     private $state:any,
61                     private sdcConfig:Models.IAppConfigurtaion,
62                     private artifactsUtils:Sdc.Utils.ArtifactsUtils,
63                     private ModalsHandler: Utils.ModalsHandler,
64                     private cacheService:Services.CacheService) {
65
66             this.initScope();
67         }
68
69
70         private initArtifactArr = (artifactType:string):void => {
71             let artifacts:Array<Models.ArtifactModel> = [];
72
73             if (this.$scope.selectedComponent) {
74                 if ('interface' == artifactType) {
75                     let interfaces = this.$scope.selectedComponent.interfaces;
76                     if (interfaces && interfaces.standard && interfaces.standard.operations) {
77
78                         angular.forEach(interfaces.standard.operations, (operation:any, interfaceName:string):void => {
79                             let item:Sdc.Models.ArtifactModel = <Sdc.Models.ArtifactModel>{};
80                             if (operation.implementation) {
81                                 item = <Sdc.Models.ArtifactModel> operation.implementation;
82                             }
83                             item.artifactDisplayName = interfaceName;
84                             item.artifactLabel = interfaceName;
85                             item.mandatory = false;
86                             artifacts.push(item);
87                         });
88                     }
89                 }else {
90                     //init normal artifacts, deployment or api artifacts
91                     let artifactsObj:Models.ArtifactGroupModel;
92                     switch (artifactType) {
93                         case "api":
94                             artifactsObj = (<Models.Components.Service>this.$scope.selectedComponent).serviceApiArtifacts;
95                             break;
96                         case "deployment":
97                             if (!this.$scope.isComponentInstanceSelected()) {
98                                 artifactsObj = this.$scope.selectedComponent.deploymentArtifacts;
99                             } else {
100                                 artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
101                             }
102                             break;
103                         default:
104                             artifactsObj = this.$scope.selectedComponent.artifacts;
105                             break;
106                     }
107                     _.forEach(artifactsObj, (artifact:Models.ArtifactModel, key) => {
108                         artifacts.push(artifact);
109                     });
110                 }
111             }
112             this.$scope.artifacts = artifacts;
113         };
114
115         private openEditArtifactModal = (artifact:Models.ArtifactModel):void => {
116             let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
117
118             let modalOptions:ng.ui.bootstrap.IModalSettings = {
119                 template: this.$templateCache.get(viewModelsHtmlBasePath + 'forms/artifact-form/artifact-form-view.html'),
120                 controller: 'Sdc.ViewModels.ArtifactResourceFormViewModel',
121                 size: 'sdc-md',
122                 backdrop: 'static',
123                 keyboard: false,
124                 resolve: {
125                     artifact: ():Models.ArtifactModel => {
126                         return artifact;
127                     },
128                     component: (): Models.Components.Component => {
129                         return  this.$scope.currentComponent;
130                     }
131                 }
132             };
133
134             let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$modal.open(modalOptions);
135             modalInstance
136                 .result
137                 .then(():void => {
138                     this.initArtifactArr(this.$scope.artifactType);
139                 });
140         };
141
142         private initScope = ():void => {
143             let self = this;
144             this.$scope.isLoading= false;
145             this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
146             this.initArtifactArr(this.$scope.artifactType);
147
148             this.$scope.getTitle = ():string => {
149                 return this.artifactsUtils.getTitle(this.$scope.artifactType, this.$scope.selectedComponent);
150             };
151
152             let vfiArtifactTypes:any = this.cacheService.get('UIConfiguration').artifacts.deployment.resourceInstanceDeploymentArtifacts;
153
154             this.$scope.isVFiArtifact=(artifact:Models.ArtifactModel):boolean=>{
155                 return vfiArtifactTypes[artifact.artifactType];
156             }
157
158             this.$scope.$watch('selectedComponent', (newResource:Models.Components.Component):void => {
159                 if (newResource) {
160                     this.initArtifactArr(this.$scope.artifactType);
161                 }
162             });
163
164
165             this.$scope.$watch('currentComponent.selectedInstance', (newInstance:Models.ComponentsInstances.ComponentInstance):void => {
166                 if (newInstance) {
167                     this.initArtifactArr(this.$scope.artifactType);
168                 }
169             });
170
171             this.$scope.addOrUpdate = (artifact:Models.ArtifactModel):void => {
172                 this.artifactsUtils.setArtifactType(artifact, this.$scope.artifactType);
173                 let artifactCopy = new Models.ArtifactModel(artifact);
174                 this.openEditArtifactModal(artifactCopy);
175             };
176
177
178             this.$scope.delete = (artifact:Models.ArtifactModel):void => {
179
180                 let onOk = ():void => {
181                     this.$scope.isLoading= true;
182                     this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
183
184                     let success = (responseArtifact:Models.ArtifactModel):void => {
185                         this.initArtifactArr(this.$scope.artifactType);
186                         this.$scope.isLoading= false;
187                     };
188
189                     let error =(error:any):void =>{
190                         console.log('Delete artifact returned error:', error);
191                         this.initArtifactArr(this.$scope.artifactType);
192                         this.$scope.isLoading= false;
193                     };
194                     if(this.$scope.isComponentInstanceSelected()){
195                         this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
196                     }else{
197                         this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
198                     }
199                 };
200                 let title: string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
201                 let message: string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
202                 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
203             };
204
205
206             this.$scope.getEnvArtifact = (heatArtifact:Models.ArtifactModel):any=>{
207                 return _.find(this.$scope.artifacts, (item:Models.ArtifactModel)=>{
208                     return item.generatedFromId === heatArtifact.uniqueId;
209                 });
210             };
211
212             this.$scope.getEnvArtifactName = (artifact:Models.ArtifactModel):string =>{
213                 let envArtifact = this.$scope.getEnvArtifact(artifact);
214                 if(envArtifact){
215                     return envArtifact.artifactDisplayName;
216                 }
217             };
218
219             this.$scope.isLicenseArtifact = (artifact:Models.ArtifactModel) :boolean => {
220                 let isLicense:boolean = false;
221                 if(this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
222                     isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
223                 }
224
225                 return isLicense;
226             };
227
228             this.$scope.openEditEnvParametersModal = (artifact:Models.ArtifactModel):void => {
229
230                 let modalOptions:ng.ui.bootstrap.IModalSettings = {
231                     template: this.$templateCache.get('/app/scripts/view-models/forms/env-parameters-form/env-parameters-form.html'),
232                     controller: 'Sdc.ViewModels.EnvParametersFormViewModel',
233                     size: 'sdc-md',
234                     backdrop: 'static',
235                     resolve: {
236                         artifact: ():Models.ArtifactModel => {
237                             return artifact;
238                         },
239                         component: (): Models.Components.Component => {
240                             return  this.$scope.currentComponent;
241                         }
242                     }
243                 };
244
245                 let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$modal.open(modalOptions);
246                 modalInstance
247                     .result
248                     .then(():void => {
249                         this.initArtifactArr(this.$scope.artifactType);
250                     });
251             };
252
253         }
254     }
255 }