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