f22562f439ccb737112ebf4244abf45122fac60a
[sdc.git] / catalog-ui / src / app / services / components / component-service.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 'use strict';
21 import * as _ from "lodash";
22 import {ArtifactModel, IFileDownload, InstancesInputsPropertiesMap, InputModel, IValidate, RelationshipModel, PropertyModel, Component, ComponentInstance,
23     AttributeModel, IAppConfigurtaion, Resource, Module, DisplayModule, ArtifactGroupModel, InputsAndProperties} from "app/models";
24 import {ComponentInstanceFactory, CommonUtils} from "app/utils";
25 import {SharingService} from "app/services-ng2";
26 import {ComponentMetadata} from "../../models/component-metadata";
27
28 export interface IComponentService {
29
30     getComponent(id:string);
31     updateComponent(component:Component):ng.IPromise<Component>;
32     changeLifecycleState(component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> ;
33     validateName(newName:string, subtype?:string):ng.IPromise<IValidate>;
34     createComponent(component:Component):ng.IPromise<Component>;
35     //importComponent
36     importComponent(component: Component): ng.IPromise<Component>;
37     addOrUpdateArtifact(componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
38     deleteArtifact(componentId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
39     addProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
40     updateProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
41     addAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
42     updateAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
43     deleteProperty(componentId:string, propertyId:string):ng.IPromise<PropertyModel>;
44     deleteAttribute(componentId:string, attributeId:string):ng.IPromise<AttributeModel>;
45     checkResourceInstanceVersionChange(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<any>;
46     changeResourceInstanceVersion(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance>;
47     updateInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
48     addInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
49     deleteInstanceArtifact(componentId:string, instanceId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
50     createComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
51     updateComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
52     updateMultipleComponentInstances(componentId:string, instances:Array<ComponentInstance>):ng.IPromise< Array<ComponentInstance>>;
53     downloadArtifact(componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload>;
54     uploadInstanceEnvFile(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
55     downloadInstanceArtifact(componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload>;
56     deleteComponentInstance(componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance>;
57     createRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
58     deleteRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
59     fetchRelation(componentId:string, linkId:string):ng.IPromise<RelationshipModel>;
60     getRequirementsCapabilities(componentId:string):ng.IPromise<any>;
61     updateInstanceProperties(componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]>;
62     updateInstanceAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
63     getComponentInstancesFilteredByInputsAndProperties(componentId:string, searchText:string):ng.IPromise<Array<ComponentInstance>>
64     getComponentInstanceInputs(componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>>;
65     getComponentInputs(componentId:string):ng.IPromise<Array<InputModel>>;
66     getComponentInstanceInputProperties(componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>>;
67     getComponentInstanceProperties(componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>>;
68     getModuleForDisplay(componentId:string, moduleId:string):ng.IPromise<DisplayModule>;
69     getComponentInstanceModule(componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule>;
70     updateGroupMetadata(componentId:string, group:Module):ng.IPromise<Module>;
71     getComponentInputInputsAndProperties(serviceId:string, input:string):ng.IPromise<InputsAndProperties>;
72     createInputsFromInstancesInputs(serviceId:string, instancesInputsMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>>;
73     createInputsFromInstancesInputsProperties(resourceId:string, instanceInputsPropertiesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>>;
74     deleteComponentInput(serviceId:string, inputId:string):ng.IPromise<InputModel>;
75     getArtifactByGroupType(componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
76     getComponentInstanceArtifactsByGroupType(componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
77 }
78
79 export class ComponentService implements IComponentService {
80
81     static '$inject' = [
82         'Restangular',
83         'sdcConfig',
84         'Sdc.Services.SharingService',
85         '$q',
86         '$base64'
87     ];
88
89     constructor(protected restangular:restangular.IElement,
90                 protected sdcConfig:IAppConfigurtaion,
91                 protected sharingService:SharingService,
92                 protected $q:ng.IQService,
93                 protected $base64:any
94                ) {
95
96         this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
97         this.restangular.setRequestInterceptor(function (elem, operation) {
98             if (operation === "remove") {
99                 return null;
100             }
101             return elem;
102         });
103         //    this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
104     }
105
106     //this function is override by each service, we need to change this method to abstract when updtaing typescript version
107     protected createComponentObject = (component:Component):Component => {
108         return component;
109     };
110
111     public getComponent = (id:string):ng.IPromise<Component> => {
112         let deferred = this.$q.defer<Component>();
113         this.restangular.one(id).get().then((response:Component) => {
114             let component:Component = this.createComponentObject(response);
115             //console.log("Component Loaded successfully : ", component);
116             deferred.resolve(component);
117         }, (err)=> {
118             console.log("Failed to load component with ID: " + id);
119             deferred.reject(err);
120         });
121         return deferred.promise;
122     };
123
124     public updateComponent = (component:Component):ng.IPromise<Component> => {
125         // If this is resource
126         if (component instanceof Resource) {
127             let resource:Resource = <Resource>component;
128             if (resource.importedFile) {
129                 // Update resource with payload data.
130                 return this.updateResourceWithPayload(resource);
131             } else {
132                 if (component.csarUUID) {
133                     // Update resource without payload data.
134                     return this.updateResource(component);
135                 } else {
136                     // Update resource without payload data (metadata).
137                     return this.updateResourceMetadata(component);
138                 }
139             }
140         } else {
141             return this.updateService(component);
142         }
143     };
144
145     private updateService = (component:Component):ng.IPromise<Component> => {
146         let deferred = this.$q.defer<Component>();
147         this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
148             let component:Component = this.createComponentObject(response);
149             deferred.resolve(component);
150         }, (err)=> {
151             deferred.reject(err);
152         });
153         return deferred.promise;
154     };
155
156     private updateResource = (component:Component):ng.IPromise<Component> => {
157         let deferred = this.$q.defer<Component>();
158         this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
159             let component:Component = this.createComponentObject(response);
160             deferred.resolve(component);
161         }, (err)=> {
162             deferred.reject(err);
163         });
164         return deferred.promise;
165     };
166
167     private updateResourceMetadata = (component:Component):ng.IPromise<Component> => {
168         let deferred = this.$q.defer<Component>();
169         this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response:Component) => {
170             let component:Component = this.createComponentObject(response);
171             deferred.resolve(component);
172         }, (err)=> {
173             deferred.reject(err);
174         });
175         return deferred.promise;
176     };
177
178     /**
179      * Only resource can be updated with payload data
180      * @param component
181      * @returns {IPromise<T>}
182      */
183     private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
184         let deferred = this.$q.defer<Component>();
185
186         resource.payloadData = resource.importedFile.base64;
187         resource.payloadName = resource.importedFile.filename;
188         let headerObj = this.getHeaderMd5(resource);
189
190         this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
191             let componentResult:Component = this.createComponentObject(response);
192             deferred.resolve(componentResult);
193         }, (err)=> {
194             deferred.reject(err);
195         });
196
197         return deferred.promise;
198     };
199
200     public createComponent = (component:Component):ng.IPromise<Component> => {
201         let deferred = this.$q.defer<Component>();
202         let headerObj = this.getHeaderMd5(component);
203         this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
204             let component:Component = this.createComponentObject(response);
205             deferred.resolve(component);
206         }, (err)=> {
207             deferred.reject(err);
208         });
209         return deferred.promise;
210     };
211
212     public importComponent = (component: Component): ng.IPromise<Component> => {
213         component.vendorName = "xfr";
214         component.vendorRelease = "xfr";
215         let deferred = this.$q.defer<Component>();
216         let headerObj = this.getHeaderMd5(component);
217         this.restangular.customPOST(JSON.stringify(component), 'importService', {}, headerObj).then((response: Component) => {
218             let component: Component = this.createComponentObject(response);
219             deferred.resolve(component);
220         }, (err) => {
221             deferred.reject(err);
222         });
223         return deferred.promise;
224     };
225
226     public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
227         let deferred = this.$q.defer<IValidate>();
228         this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
229             deferred.resolve(response.plain());
230         }, (err)=> {
231             deferred.reject(err);
232         });
233         return deferred.promise;
234     };
235
236     public changeLifecycleState = (component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> => {
237         let deferred = this.$q.defer<ComponentMetadata>();
238         this.restangular.one(component.uniqueId).one(state).customPOST(userRemarks).then((response:ComponentMetadata) => {
239             this.sharingService.addUuidValue(response.uniqueId, response.uuid);
240             let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
241             deferred.resolve(component);
242         }, (err)=> {
243             deferred.reject(err);
244         });
245         return deferred.promise;
246     };
247
248     // ------------------------------------------------ Artifacts API --------------------------------------------------//
249     public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
250         let deferred = this.$q.defer<ArtifactModel>();
251         let headerObj = {};
252         if (artifact.payloadData) {
253             headerObj = this.getHeaderMd5(artifact);
254         }
255         this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
256             deferred.resolve(response.plain());
257         }, (err)=> {
258             deferred.reject(err);
259         });
260         return deferred.promise;
261     };
262
263     public downloadArtifact = (componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload> => {
264         let deferred = this.$q.defer<IFileDownload>();
265         if(vendorName === 'IsService'){
266             this.restangular.one('importService').one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
267                 deferred.resolve(response.plain());
268             }, (err) => {
269                 deferred.reject(err);
270             });
271         }else{
272             this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
273                 deferred.resolve(response.plain());
274             }, (err) => {
275                 deferred.reject(err);
276             });
277         }
278         return deferred.promise;
279     };
280
281     public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
282         let deferred = this.$q.defer<ArtifactModel>();
283         this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
284             deferred.resolve(response);
285         }, (err)=> {
286             deferred.reject(err);
287         });
288         return deferred.promise;
289     };
290
291     public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
292         let deferred = this.$q.defer<ArtifactGroupModel>();
293         this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
294             var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
295             deferred.resolve(artifacts);
296         }, (err)=> {
297             deferred.reject(err);
298         });
299         return deferred.promise;
300     };
301
302     public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
303         let deferred = this.$q.defer<ArtifactGroupModel>();
304         this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
305             var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
306             deferred.resolve(artifacts);
307         }, (err)=> {
308             deferred.reject(err);
309         });
310         return deferred.promise;
311     };
312
313
314     // ------------------------------------------------ Properties API --------------------------------------------------//
315     public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
316         let deferred = this.$q.defer<PropertyModel>();
317         this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
318             let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
319             deferred.resolve(property);
320         }, (err)=> {
321             deferred.reject(err);
322         });
323         return deferred.promise;
324     };
325
326     public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
327         let deferred = this.$q.defer<PropertyModel>();
328         this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
329             let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
330             deferred.resolve(property);
331         }, (err)=> {
332             deferred.reject(err);
333         });
334         return deferred.promise;
335     };
336
337     public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
338         let deferred = this.$q.defer<PropertyModel>();
339         this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
340             deferred.resolve(response);
341         }, (err)=> {
342             deferred.reject(err);
343         });
344         return deferred.promise;
345     };
346
347     // ------------------------------------------------ Attributes API --------------------------------------------------//
348     public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
349         let deferred = this.$q.defer<AttributeModel>();
350         this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
351             let attribute:AttributeModel = new AttributeModel(response);
352             deferred.resolve(attribute);
353         }, (err)=> {
354             deferred.reject(err);
355         });
356         return deferred.promise;
357     };
358
359     public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
360         let deferred = this.$q.defer<AttributeModel>();
361         this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
362             let attribute:AttributeModel = new AttributeModel(response);
363             deferred.resolve(attribute);
364         }, (err)=> {
365             deferred.reject(err);
366         });
367         return deferred.promise;
368     };
369
370     public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
371         let deferred = this.$q.defer<AttributeModel>();
372         this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
373             deferred.resolve(response);
374         }, (err)=> {
375             deferred.reject(err);
376         });
377         return deferred.promise;
378     };
379
380     // ------------------------------------------------ Component Instances API --------------------------------------------------//
381
382     public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
383         let deferred = this.$q.defer<ComponentInstance>();
384         this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
385             let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
386             console.log("Component Instance created", componentInstance);
387             deferred.resolve(componentInstance);
388         }, (err)=> {
389             console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
390             deferred.reject(err);
391         });
392         return deferred.promise;
393     };
394
395     public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
396         let deferred = this.$q.defer<ComponentInstance>();
397         this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
398             let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
399             console.log("Component Instance was updated", componentInstance);
400             deferred.resolve(componentInstance);
401         }, (err)=> {
402             console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
403             deferred.reject(err);
404         });
405         return deferred.promise;
406     };
407
408     public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
409         let deferred = this.$q.defer<Array<ComponentInstance>>();
410         this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
411             console.log("Multiple Component Instances was updated", response);
412             let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
413             _.forEach(response, (componentInstance:ComponentInstance) => {
414                 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
415                 updateInstances.push(updatedComponentInstance);
416             });
417             deferred.resolve(updateInstances);
418         }, (err)=> {
419             console.log("Failed to update Multiple componentInstance.");
420             deferred.reject(err);
421         });
422         return deferred.promise;
423     };
424
425     public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
426         let deferred = this.$q.defer<ComponentInstance>();
427         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
428             console.log("Component Instance was deleted");
429             deferred.resolve();
430         }, (err)=> {
431             console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
432             deferred.reject(err);
433         });
434         return deferred.promise;
435     };
436
437     public checkResourceInstanceVersionChange = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
438         let deferred = this.$q.defer<ComponentInstance>();
439         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response:any) => {
440             deferred.resolve(response);
441         }, err => {
442             deferred.reject(err);
443         });
444         return deferred.promise;
445     };
446
447     public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
448         let deferred = this.$q.defer<ComponentInstance>();
449         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
450             let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
451             deferred.resolve(componentInstance);
452         }, (err)=> {
453             deferred.reject(err);
454         });
455         return deferred.promise;
456     };
457
458     public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
459         let deferred = this.$q.defer<IFileDownload>();
460         this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
461             deferred.resolve(response.plain());
462         }, (err)=> {
463             deferred.reject(err);
464         });
465         return deferred.promise;
466     };
467
468     public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
469         let deferred = this.$q.defer<ArtifactModel>();
470         let headerObj = {};
471         if (artifact.payloadData) {
472             headerObj = this.getHeaderMd5(artifact);
473         }
474         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
475             let newArtifact = new ArtifactModel(response);
476             deferred.resolve(newArtifact);
477         }, (err)=> {
478             deferred.reject(err);
479         });
480         return deferred.promise;
481     };
482
483     public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
484         let deferred = this.$q.defer<ArtifactModel>();
485         let headerObj = {};
486         if (artifact.payloadData) {
487             headerObj = this.getHeaderMd5(artifact);
488         }
489         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
490             let artifact:ArtifactModel = new ArtifactModel(response.plain());
491             deferred.resolve(artifact);
492         }, (err)=> {
493             deferred.reject(err);
494         });
495         return deferred.promise;
496     };
497
498     public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
499         let deferred = this.$q.defer<ArtifactModel>();
500         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
501             deferred.resolve(response);
502         }, (err)=> {
503             deferred.reject(err);
504         });
505         return deferred.promise;
506     };
507
508     public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
509         let deferred = this.$q.defer<ArtifactModel>();
510         let headerObj = {};
511         if (artifact.payloadData) {
512             headerObj = this.getHeaderMd5(artifact);
513         }
514         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
515             let newArtifact = new ArtifactModel(response);
516             deferred.resolve(newArtifact);
517         }, (err)=> {
518             deferred.reject(err);
519         });
520         return deferred.promise;
521     };
522
523     public updateInstanceProperties = (componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]> => {
524         let deferred = this.$q.defer<PropertyModel[]>();
525         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response:any) => {
526             const newProperties = response.map((res) => {
527                 const newProperty = new PropertyModel(res);
528                 newProperty.readonly = true;
529                 newProperty.resourceInstanceUniqueId = componentInstanceId;
530                 return newProperty;
531             });
532             deferred.resolve(newProperties);
533         }, (err)=> {
534             deferred.reject(err);
535         });
536         return deferred.promise;
537     };
538
539     public updateInstanceAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
540         let deferred = this.$q.defer<AttributeModel>();
541         let instanceId = attribute.resourceInstanceUniqueId;
542         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response:any) => {
543             let newAttribute = new AttributeModel(response);
544             newAttribute.readonly = true;
545             newAttribute.resourceInstanceUniqueId = instanceId;
546             deferred.resolve(newAttribute);
547         }, (err)=> {
548             deferred.reject(err);
549         });
550         return deferred.promise;
551     };
552
553     public createRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
554         let deferred = this.$q.defer<RelationshipModel>();
555         const linkPayload:RelationshipModel = new RelationshipModel(link);
556         linkPayload.relationships.forEach((rel) => {
557             delete rel.capability;
558             delete rel.requirement;
559         });
560         this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(linkPayload)).then((response:any) => {
561             let relation:RelationshipModel = new RelationshipModel(response.plain());
562             console.log("Link created successfully ", relation);
563             deferred.resolve(relation);
564         }, (err)=> {
565             console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
566             deferred.reject(err);
567         });
568         return deferred.promise;
569     };
570
571     public deleteRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
572         let deferred = this.$q.defer<RelationshipModel>();
573         const linkPayload:RelationshipModel = new RelationshipModel(link);
574         linkPayload.relationships.forEach((rel) => {
575             delete rel.capability;
576             delete rel.requirement;
577         });
578         this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(linkPayload)).then((response:any) => {
579             let relation:RelationshipModel = new RelationshipModel(response);
580             console.log("Link deleted successfully ", relation);
581             deferred.resolve(relation);
582         }, (err)=> {
583             console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
584             deferred.reject(err);
585         });
586         return deferred.promise;
587     };
588
589     public fetchRelation = (componentId:string, linkId:string):ng.IPromise<RelationshipModel> => {
590         let deferred = this.$q.defer<RelationshipModel>();
591         this.restangular.one(componentId).one("relationId").one(linkId).get().then((response:any) => {
592             let relation:RelationshipModel = new RelationshipModel(response);
593             console.log("Link fetched successfully ", relation);
594             deferred.resolve(relation);
595         }, (err)=> {
596             console.log("Failed to fetch Link Id: " + linkId);
597             deferred.reject(err);
598         });
599         return deferred.promise;
600     };
601
602     public getRequirementsCapabilities = (componentId:string):ng.IPromise<any> => {
603         let deferred = this.$q.defer();
604         this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response:any) => {
605             console.log("Component requirement capabilities recived: ", response);
606             deferred.resolve(response);
607         }, (err)=> {
608             console.log("Failed to get requirements & capabilities");
609             deferred.reject(err);
610         });
611         return deferred.promise;
612     };
613
614     public getModuleForDisplay = (componentId:string, moduleId:string):ng.IPromise<DisplayModule> => {
615         let deferred = this.$q.defer<DisplayModule>();
616         this.restangular.one(componentId).one("groups").one(moduleId).get().then((response:any) => {
617             console.log("module loaded successfully: ", response);
618             let module:DisplayModule = new DisplayModule(response);
619             deferred.resolve(module);
620         }, (err)=> {
621             console.log("Failed to get module with id: ", moduleId);
622             deferred.reject(err);
623         });
624         return deferred.promise;
625     };
626
627     public getComponentInstanceModule = (componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule> => {
628         let deferred = this.$q.defer<DisplayModule>();
629         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response:any) => {
630             console.log("module loaded successfully: ", response);
631             let module:DisplayModule = new DisplayModule(response);
632             deferred.resolve(module);
633         }, (err)=> {
634             console.log("Failed to get module with id: ", moduleId);
635             deferred.reject(err);
636         });
637         return deferred.promise;
638     };
639
640     public getComponentInstancesFilteredByInputsAndProperties = (componentId:string, searchText?:string):ng.IPromise<Array<ComponentInstance>> => {
641         let deferred = this.$q.defer<Array<ComponentInstance>>();
642         this.restangular.one(componentId).one("componentInstances").get({'searchText': searchText}).then((response:any) => {
643             console.log("component instances return successfully: ", response);
644             let componentInstances:Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
645             deferred.resolve(componentInstances);
646         }, (err) => {
647             console.log("Failed to get component instances of component with id: " + componentId);
648             deferred.reject(err);
649         });
650
651         return deferred.promise;
652     };
653
654     public getComponentInstanceInputs = (componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>> => {
655
656         let deferred = this.$q.defer<Array<InputModel>>();
657         this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response:any) => {
658             console.log("component instance input return successfully: ", response);
659             let inputsArray:Array<InputModel> = new Array<InputModel>();
660             _.forEach(response, (inputObj:InputModel) => {
661                 inputsArray.push(new InputModel(inputObj));
662             });
663             deferred.resolve(inputsArray);
664         }, (err) => {
665             console.log("Failed to get component instance input with id: " + instanceId);
666             deferred.reject(err);
667         });
668
669         return deferred.promise;
670     };
671
672     public getComponentInputs = (componentId:string):ng.IPromise<Array<InputModel>> => {
673
674         let deferred = this.$q.defer<Array<InputModel>>();
675         this.restangular.one(componentId).one("inputs").get().then((response:any) => {
676             console.log("component inputs return successfully: ", response);
677             let inputsArray:Array<InputModel> = new Array<InputModel>();
678             _.forEach(response, (inputObj:InputModel) => {
679                 inputsArray.push(new InputModel(inputObj));
680             });
681             deferred.resolve(inputsArray);
682         }, (err) => {
683             console.log("Failed to get component inputs for component with id: " + componentId);
684             deferred.reject(err);
685         });
686
687         return deferred.promise;
688     };
689
690     public getComponentInstanceInputProperties = (componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>> => {
691
692         let deferred = this.$q.defer<Array<PropertyModel>>();
693         this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response:any) => {
694             console.log("component instance input properties return successfully: ", response);
695             let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
696             _.forEach(response, (propertyObj:PropertyModel) => {
697                 propertiesArray.push(new PropertyModel(propertyObj));
698             });
699             deferred.resolve(propertiesArray);
700         }, (err) => {
701             console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
702             deferred.reject(err);
703         });
704
705         return deferred.promise;
706     };
707
708
709     public getComponentInstanceProperties = (componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>> => {
710         let deferred = this.$q.defer<Array<PropertyModel>>();
711         this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response:any) => {
712             console.log("component instance  properties return successfully: ", response);
713             let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
714             _.forEach(response, (propertyObj:PropertyModel) => {
715                 propertiesArray.push(new PropertyModel(propertyObj));
716             });
717             deferred.resolve(propertiesArray);
718         }, (err) => {
719             console.log("Failed to get component instance  properties with instanceId: " + instanceId);
720             deferred.reject(err);
721         });
722
723         return deferred.promise;
724     };
725
726     public updateGroupMetadata = (componentId:string, group:Module):ng.IPromise<Module> => {
727
728         let deferred = this.$q.defer<Module>();
729         this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response:Module) => {
730             console.log("group metadata updated successfully: ", response);
731             let updatedGroup:Module = new Module(response);
732
733             deferred.resolve(updatedGroup);
734         }, (err) => {
735             console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
736             deferred.reject(err);
737         });
738
739         return deferred.promise;
740     };
741
742     public getComponentInputInputsAndProperties = (serviceId:string, inputId:string):ng.IPromise<InputsAndProperties> => {
743         let defer = this.$q.defer<InputsAndProperties>();
744         this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response:InputsAndProperties) => {
745
746             let inputsArray:Array<InputModel> = new Array<InputModel>();
747             _.forEach(response.inputs, (inputObj:InputModel) => {
748                 inputsArray.push(new InputModel(inputObj));
749             });
750
751             let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
752             _.forEach(response.properties, (property:PropertyModel) => {
753                 propertiesArray.push(new PropertyModel(property));
754             });
755
756             defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
757         }, (err)=> {
758             console.log("failed to get inputs of input : ", err);
759             defer.reject(err);
760         });
761         return defer.promise;
762     };
763
764     createInputsFromInstancesInputsProperties = (resourceId:string, instancePropertyMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>> => {
765         let defer = this.$q.defer<Array<PropertyModel>>();
766         this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response:any) => {
767             let inputsArray:Array<PropertyModel> = new Array<PropertyModel>();
768             _.forEach(response, (inputObj:PropertyModel) => {
769                 inputsArray.push(new PropertyModel(inputObj));
770             });
771             defer.resolve(inputsArray);
772         }, (err)=> {
773             console.log("failed to create service inputs from VF instances inputs : ", err);
774             defer.reject(err);
775         });
776         return defer.promise;
777     };
778
779     createInputsFromInstancesInputs = (serviceId:string, instancesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>> => {
780         let defer = this.$q.defer<Array<InputModel>>();
781         this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response:any) => {
782             let inputsArray:Array<InputModel> = new Array<InputModel>();
783             _.forEach(response, (inputObj:InputModel) => {
784                 inputsArray.push(new InputModel(inputObj));
785             });
786             defer.resolve(inputsArray);
787         }, (err)=> {
788             console.log("failed to create service inputs from VF instances inputs : ", err);
789             defer.reject(err);
790         });
791         return defer.promise;
792     };
793
794     deleteComponentInput = (serviceId:string, inputId:string):ng.IPromise<InputModel> => {
795         let defer = this.$q.defer<InputModel>();
796         this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response:any) => {
797             let inputToDelete = new InputModel(response);
798             defer.resolve(inputToDelete);
799         }, (err)=> {
800             console.log("failed to delete input from service: ", err);
801             defer.reject(err);
802         });
803         return defer.promise;
804     };
805
806     private getHeaderMd5 = (object:any):any => {
807         let headerObj = {};
808         // This is ugly workaround!!!
809         // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
810         JSON.stringify(object);
811         let componentString:string = JSON.stringify(object);
812         let md5Result = md5(componentString).toLowerCase();
813         headerObj = {'Content-MD5': this.$base64.encode(md5Result)};
814         return headerObj;
815     };
816
817 }