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