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