25203e77329923c0f7271ad7db383028490354f1
[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
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     checkResourceInstanceVersionChange(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<any>;
44     changeResourceInstanceVersion(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance>;
45     updateInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
46     addInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
47     deleteInstanceArtifact(componentId:string, instanceId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
48     createComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
49     updateComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
50     updateMultipleComponentInstances(componentId:string, instances:Array<ComponentInstance>):ng.IPromise< Array<ComponentInstance>>;
51     downloadArtifact(componentId:string, artifactId:string):ng.IPromise<IFileDownload>;
52     uploadInstanceEnvFile(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
53     downloadInstanceArtifact(componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload>;
54     deleteComponentInstance(componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance>;
55     createRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
56     deleteRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
57     fetchRelation(componentId:string, linkId:string):ng.IPromise<RelationshipModel>;
58     getRequirementsCapabilities(componentId:string):ng.IPromise<any>;
59     updateInstanceProperties(componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]>;
60     updateInstanceAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
61     getComponentInstancesFilteredByInputsAndProperties(componentId:string, searchText:string):ng.IPromise<Array<ComponentInstance>>
62     getComponentInstanceInputs(componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>>;
63     getComponentInputs(componentId:string):ng.IPromise<Array<InputModel>>;
64     getComponentInstanceInputProperties(componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>>;
65     getComponentInstanceProperties(componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>>;
66     getModuleForDisplay(componentId:string, moduleId:string):ng.IPromise<DisplayModule>;
67     getComponentInstanceModule(componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule>;
68     updateGroupMetadata(componentId:string, group:Module):ng.IPromise<Module>;
69     getComponentInputInputsAndProperties(serviceId:string, input:string):ng.IPromise<InputsAndProperties>;
70     createInputsFromInstancesInputs(serviceId:string, instancesInputsMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>>;
71     createInputsFromInstancesInputsProperties(resourceId:string, instanceInputsPropertiesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>>;
72     deleteComponentInput(serviceId:string, inputId:string):ng.IPromise<InputModel>;
73     getArtifactByGroupType(componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
74     getComponentInstanceArtifactsByGroupType(componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
75 }
76
77 export class ComponentService implements IComponentService {
78
79     static '$inject' = [
80         'Restangular',
81         'sdcConfig',
82         'Sdc.Services.SharingService',
83         '$q',
84         '$base64'
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                ) {
93
94         this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
95         this.restangular.setRequestInterceptor(function (elem, operation) {
96             if (operation === "remove") {
97                 return null;
98             }
99             return elem;
100         });
101         //    this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
102     }
103
104     //this function is override by each service, we need to change this method to abstract when updtaing typescript version
105     protected createComponentObject = (component:Component):Component => {
106         return component;
107     };
108
109     public getComponent = (id:string):ng.IPromise<Component> => {
110         let deferred = this.$q.defer<Component>();
111         this.restangular.one(id).get().then((response:Component) => {
112             let component:Component = this.createComponentObject(response);
113             //console.log("Component Loaded successfully : ", component);
114             deferred.resolve(component);
115         }, (err)=> {
116             console.log("Failed to load component with ID: " + id);
117             deferred.reject(err);
118         });
119         return deferred.promise;
120     };
121
122     public updateComponent = (component:Component):ng.IPromise<Component> => {
123         // If this is resource
124         if (component instanceof Resource) {
125             let resource:Resource = <Resource>component;
126             if (resource.importedFile) {
127                 // Update resource with payload data.
128                 return this.updateResourceWithPayload(resource);
129             } else {
130                 if (component.csarUUID) {
131                     // Update resource without payload data.
132                     return this.updateResource(component);
133                 } else {
134                     // Update resource without payload data (metadata).
135                     return this.updateResourceMetadata(component);
136                 }
137             }
138         } else {
139             return this.updateService(component);
140         }
141     };
142
143     private updateService = (component:Component):ng.IPromise<Component> => {
144         let deferred = this.$q.defer<Component>();
145         this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
146             let component:Component = this.createComponentObject(response);
147             deferred.resolve(component);
148         }, (err)=> {
149             deferred.reject(err);
150         });
151         return deferred.promise;
152     };
153
154     private updateResource = (component:Component):ng.IPromise<Component> => {
155         let deferred = this.$q.defer<Component>();
156         this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
157             let component:Component = this.createComponentObject(response);
158             deferred.resolve(component);
159         }, (err)=> {
160             deferred.reject(err);
161         });
162         return deferred.promise;
163     };
164
165     private updateResourceMetadata = (component:Component):ng.IPromise<Component> => {
166         let deferred = this.$q.defer<Component>();
167         this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response:Component) => {
168             let component:Component = this.createComponentObject(response);
169             deferred.resolve(component);
170         }, (err)=> {
171             deferred.reject(err);
172         });
173         return deferred.promise;
174     };
175
176     /**
177      * Only resource can be updated with payload data
178      * @param component
179      * @returns {IPromise<T>}
180      */
181     private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
182         let deferred = this.$q.defer<Component>();
183
184         resource.payloadData = resource.importedFile.base64;
185         resource.payloadName = resource.importedFile.filename;
186         let headerObj = this.getHeaderMd5(resource);
187
188         this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
189             let componentResult:Component = this.createComponentObject(response);
190             deferred.resolve(componentResult);
191         }, (err)=> {
192             deferred.reject(err);
193         });
194
195         return deferred.promise;
196     };
197
198     public createComponent = (component:Component):ng.IPromise<Component> => {
199         let deferred = this.$q.defer<Component>();
200         let headerObj = this.getHeaderMd5(component);
201         this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
202             let component:Component = this.createComponentObject(response);
203             deferred.resolve(component);
204         }, (err)=> {
205             deferred.reject(err);
206         });
207         return deferred.promise;
208     };
209
210     public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
211         let deferred = this.$q.defer<IValidate>();
212         this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
213             deferred.resolve(response.plain());
214         }, (err)=> {
215             deferred.reject(err);
216         });
217         return deferred.promise;
218     };
219
220     public changeLifecycleState = (component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> => {
221         let deferred = this.$q.defer<ComponentMetadata>();
222         this.restangular.one(component.uniqueId).one(state).customPOST(userRemarks).then((response:ComponentMetadata) => {
223             this.sharingService.addUuidValue(response.uniqueId, response.uuid);
224             let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
225             deferred.resolve(component);
226         }, (err)=> {
227             deferred.reject(err);
228         });
229         return deferred.promise;
230     };
231
232     // ------------------------------------------------ Artifacts API --------------------------------------------------//
233     public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
234         let deferred = this.$q.defer<ArtifactModel>();
235         let headerObj = {};
236         if (artifact.payloadData) {
237             headerObj = this.getHeaderMd5(artifact);
238         }
239         this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
240             deferred.resolve(response.plain());
241         }, (err)=> {
242             deferred.reject(err);
243         });
244         return deferred.promise;
245     };
246
247     public downloadArtifact = (componentId:string, artifactId:string):ng.IPromise<IFileDownload> => {
248         let deferred = this.$q.defer<IFileDownload>();
249         this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response:any) => {
250             deferred.resolve(response.plain());
251         }, (err)=> {
252             deferred.reject(err);
253         });
254         return deferred.promise;
255     };
256
257     public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
258         let deferred = this.$q.defer<ArtifactModel>();
259         this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
260             deferred.resolve(response);
261         }, (err)=> {
262             deferred.reject(err);
263         });
264         return deferred.promise;
265     };
266
267     public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
268         let deferred = this.$q.defer<ArtifactGroupModel>();
269         this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
270             var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
271             deferred.resolve(artifacts);
272         }, (err)=> {
273             deferred.reject(err);
274         });
275         return deferred.promise;
276     };
277
278     public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
279         let deferred = this.$q.defer<ArtifactGroupModel>();
280         this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
281             var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
282             deferred.resolve(artifacts);
283         }, (err)=> {
284             deferred.reject(err);
285         });
286         return deferred.promise;
287     };
288
289
290     // ------------------------------------------------ Properties API --------------------------------------------------//
291     public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
292         let deferred = this.$q.defer<PropertyModel>();
293         this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
294             let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
295             deferred.resolve(property);
296         }, (err)=> {
297             deferred.reject(err);
298         });
299         return deferred.promise;
300     };
301
302     public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
303         let deferred = this.$q.defer<PropertyModel>();
304         this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
305             let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
306             deferred.resolve(property);
307         }, (err)=> {
308             deferred.reject(err);
309         });
310         return deferred.promise;
311     };
312
313     public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
314         let deferred = this.$q.defer<PropertyModel>();
315         this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
316             deferred.resolve(response);
317         }, (err)=> {
318             deferred.reject(err);
319         });
320         return deferred.promise;
321     };
322
323     // ------------------------------------------------ Attributes API --------------------------------------------------//
324     public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
325         let deferred = this.$q.defer<AttributeModel>();
326         this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
327             let attribute:AttributeModel = new AttributeModel(response);
328             deferred.resolve(attribute);
329         }, (err)=> {
330             deferred.reject(err);
331         });
332         return deferred.promise;
333     };
334
335     public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
336         let deferred = this.$q.defer<AttributeModel>();
337         this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
338             let attribute:AttributeModel = new AttributeModel(response);
339             deferred.resolve(attribute);
340         }, (err)=> {
341             deferred.reject(err);
342         });
343         return deferred.promise;
344     };
345
346     public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
347         let deferred = this.$q.defer<AttributeModel>();
348         this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
349             deferred.resolve(response);
350         }, (err)=> {
351             deferred.reject(err);
352         });
353         return deferred.promise;
354     };
355
356     // ------------------------------------------------ Component Instances API --------------------------------------------------//
357
358     public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
359         let deferred = this.$q.defer<ComponentInstance>();
360         this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
361             let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
362             console.log("Component Instance created", componentInstance);
363             deferred.resolve(componentInstance);
364         }, (err)=> {
365             console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
366             deferred.reject(err);
367         });
368         return deferred.promise;
369     };
370
371     public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
372         let deferred = this.$q.defer<ComponentInstance>();
373         this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
374             let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
375             console.log("Component Instance was updated", componentInstance);
376             deferred.resolve(componentInstance);
377         }, (err)=> {
378             console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
379             deferred.reject(err);
380         });
381         return deferred.promise;
382     };
383
384     public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
385         let deferred = this.$q.defer<Array<ComponentInstance>>();
386         this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
387             console.log("Multiple Component Instances was updated", response);
388             let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
389             _.forEach(response, (componentInstance:ComponentInstance) => {
390                 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
391                 updateInstances.push(updatedComponentInstance);
392             });
393             deferred.resolve(updateInstances);
394         }, (err)=> {
395             console.log("Failed to update Multiple componentInstance.");
396             deferred.reject(err);
397         });
398         return deferred.promise;
399     };
400
401     public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
402         let deferred = this.$q.defer<ComponentInstance>();
403         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
404             console.log("Component Instance was deleted");
405             deferred.resolve();
406         }, (err)=> {
407             console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
408             deferred.reject(err);
409         });
410         return deferred.promise;
411     };
412
413     public checkResourceInstanceVersionChange = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
414         let deferred = this.$q.defer<ComponentInstance>();
415         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response:any) => {
416             deferred.resolve(response);
417         }, err => {
418             deferred.reject(err);
419         });
420         return deferred.promise;
421     };
422
423     public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
424         let deferred = this.$q.defer<ComponentInstance>();
425         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
426             let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
427             deferred.resolve(componentInstance);
428         }, (err)=> {
429             deferred.reject(err);
430         });
431         return deferred.promise;
432     };
433
434     public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
435         let deferred = this.$q.defer<IFileDownload>();
436         this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
437             deferred.resolve(response.plain());
438         }, (err)=> {
439             deferred.reject(err);
440         });
441         return deferred.promise;
442     };
443
444     public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
445         let deferred = this.$q.defer<ArtifactModel>();
446         let headerObj = {};
447         if (artifact.payloadData) {
448             headerObj = this.getHeaderMd5(artifact);
449         }
450         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
451             let newArtifact = new ArtifactModel(response);
452             deferred.resolve(newArtifact);
453         }, (err)=> {
454             deferred.reject(err);
455         });
456         return deferred.promise;
457     };
458
459     public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
460         let deferred = this.$q.defer<ArtifactModel>();
461         let headerObj = {};
462         if (artifact.payloadData) {
463             headerObj = this.getHeaderMd5(artifact);
464         }
465         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
466             let artifact:ArtifactModel = new ArtifactModel(response.plain());
467             deferred.resolve(artifact);
468         }, (err)=> {
469             deferred.reject(err);
470         });
471         return deferred.promise;
472     };
473
474     public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
475         let deferred = this.$q.defer<ArtifactModel>();
476         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
477             deferred.resolve(response);
478         }, (err)=> {
479             deferred.reject(err);
480         });
481         return deferred.promise;
482     };
483
484     public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
485         let deferred = this.$q.defer<ArtifactModel>();
486         let headerObj = {};
487         if (artifact.payloadData) {
488             headerObj = this.getHeaderMd5(artifact);
489         }
490         this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
491             let newArtifact = new ArtifactModel(response);
492             deferred.resolve(newArtifact);
493         }, (err)=> {
494             deferred.reject(err);
495         });
496         return deferred.promise;
497     };
498
499     public updateInstanceProperties = (componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]> => {
500         let deferred = this.$q.defer<PropertyModel[]>();
501         this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response:any) => {
502             const newProperties = response.map((res) => {
503                 const newProperty = new PropertyModel(res);
504                 newProperty.readonly = true;
505                 newProperty.resourceInstanceUniqueId = componentInstanceId;
506                 return newProperty;
507             });
508             deferred.resolve(newProperties);
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<AttributeModel>();
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<RelationshipModel>();
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<RelationshipModel>();
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<DisplayModule>();
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<DisplayModule> => {
604         let deferred = this.$q.defer<DisplayModule>();
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<Array<ComponentInstance>>();
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<Array<InputModel>>();
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<Array<InputModel>>();
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<Array<PropertyModel>>();
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<Array<PropertyModel>>();
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<Module>();
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<InputsAndProperties>();
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<Array<PropertyModel>>();
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<Array<InputModel>>();
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<InputModel>();
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 }