2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import * as _ from "lodash";
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";
39 export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
40 artifacts:Array<ArtifactModel>;
42 downloadFile:IFileDownload;
44 allowDeleteAndUpdateArtifactMap:Dictionary<string, boolean>;
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;
54 //isVFiArtifact(artifact:ArtifactModel):boolean;
57 export class ResourceArtifactsViewModel {
67 'EventListenerService'
70 constructor(private $scope:IArtifactsViewModelScope,
71 private $filter:ng.IFilterService,
73 private sdcConfig:IAppConfigurtaion,
74 private artifactsUtils:ArtifactsUtils,
75 private ModalsHandler:ModalsHandler,
76 private $q:ng.IQService,
77 private eventListenerService: EventListenerService) {
83 private initArtifactArr = (artifactType:string):void => {
84 let artifacts:Array<ArtifactModel> = [];
86 if (this.$scope.selectedComponent) {
87 if ('interface' == artifactType) {
88 let interfaces = this.$scope.currentComponent.interfaces;
89 if (interfaces && interfaces.standard && interfaces.standard.operations) {
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;
96 item.artifactDisplayName = interfaceName;
97 item.artifactLabel = interfaceName;
98 item.mandatory = false;
103 //init normal artifacts, deployment or api artifacts
104 let artifactsObj:ArtifactGroupModel;
105 switch (artifactType) {
107 artifactsObj = (<Service>this.$scope.currentComponent).serviceApiArtifacts;
110 if (!this.$scope.isComponentInstanceSelected()) {
111 artifactsObj = this.$scope.currentComponent.deploymentArtifacts;
113 artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
117 //artifactsObj = this.$scope.selectedComponent.artifacts;
118 if (!this.$scope.isComponentInstanceSelected()) {
119 artifactsObj = this.$scope.currentComponent.artifacts;
121 artifactsObj = this.$scope.currentComponent.selectedInstance.artifacts;
125 _.forEach(artifactsObj, (artifact:ArtifactModel, key) => {
126 artifacts.push(artifact);
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);
135 this.$scope.isLoading = false;
136 this.$scope.preventMoveTab(false);
140 private convertToArtifactUrl = (artifactType:string):string => {
142 switch (artifactType) {
146 return 'SERVICE_API';
148 return 'INFORMATIONAL';
153 private loadComponentArtifactIfNeeded = (forceLoad?: boolean) => {
155 let onGetComponentArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
156 switch (this.$scope.artifactType) {
158 this.$scope.currentComponent.deploymentArtifacts = artifacts;
161 (<Service>this.$scope.currentComponent).serviceApiArtifacts = artifacts;
164 this.$scope.currentComponent.artifacts = artifacts;
167 this.$scope.isLoading = false;
168 this.initArtifactArr(this.$scope.artifactType);
172 this.$scope.isLoading = false;
175 switch (this.$scope.artifactType) {
177 if(forceLoad || !this.$scope.currentComponent.deploymentArtifacts) {
178 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
180 this.initArtifactArr(this.$scope.artifactType);
185 if(!(<Service>this.$scope.currentComponent).serviceApiArtifacts) {
186 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
188 this.initArtifactArr(this.$scope.artifactType);
192 if(!this.$scope.currentComponent.artifacts) {
193 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
195 this.initArtifactArr(this.$scope.artifactType);
200 private loadArtifacts = (forceLoad?: boolean):void => {
202 let onGetInstanceArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
203 switch (this.$scope.artifactType) {
205 this.$scope.currentComponent.selectedInstance.deploymentArtifacts = artifacts;
208 this.$scope.currentComponent.selectedInstance.artifacts = artifacts;
211 this.initArtifactArr(this.$scope.artifactType);
215 this.$scope.isLoading = false;
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);
223 this.loadComponentArtifactIfNeeded(forceLoad);
227 private updateArtifactsIfNeeded = ():void => {
228 if (this.$scope.artifactType === "deployment") {
229 this.loadArtifacts(true);
231 this.initArtifactArr(this.$scope.artifactType);
235 private openEditArtifactModal = (artifact:ArtifactModel):void => {
236 this.ModalsHandler.openArtifactModal(artifact, this.$scope.currentComponent).then(():void => {
237 this.updateArtifactsIfNeeded();
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));
252 private initScope = ():void => {
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);
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);
265 // return this.$scope.currentComponent.selectedInstance && this.$scope.currentComponent.selectedInstance.deploymentArtifacts && this.$scope.currentComponent.selectedInstance.deploymentArtifacts[artifact.artifactLabel];
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);
275 this.$scope.delete = (artifact:ArtifactModel):void => {
277 let onOk = ():void => {
278 this.$scope.isLoading = true;
279 this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
281 let success = (responseArtifact:ArtifactModel):void => {
282 this.initArtifactArr(this.$scope.artifactType);
283 this.$scope.isLoading = false;
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;
291 if (this.$scope.isComponentInstanceSelected()) {
292 this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
294 this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
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);
303 this.$scope.getEnvArtifact = (heatArtifact:ArtifactModel):any=> {
304 return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
305 return item.generatedFromId === heatArtifact.uniqueId;
309 this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
310 let envArtifact = this.$scope.getEnvArtifact(artifact);
312 return envArtifact.artifactDisplayName;
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);
325 this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
326 this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.currentComponent).then(()=> {
327 this.updateArtifactsIfNeeded();
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';
341 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
342 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
344 this.$scope.$on('$destroy', () => {
346 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
347 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
350 this.loadArtifacts();