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