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