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