2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 import {ArtifactModel, IFileDownload, InstancesInputsPropertiesMap, InputModel, IValidate, RelationshipModel, PropertyModel, Component, ComponentInstance,
22 AttributeModel, IAppConfigurtaion, Resource, Module, DisplayModule, ArtifactGroupModel, InputsAndProperties} from "app/models";
23 import {ComponentInstanceFactory, CommonUtils} from "app/utils";
24 import {SharingService} from "../sharing-service";
25 import {ComponentMetadata} from "../../models/component-metadata";
27 export interface IComponentService {
29 getComponent(id:string);
30 updateComponent(component:Component):ng.IPromise<Component>;
31 changeLifecycleState(component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> ;
32 validateName(newName:string, subtype?:string):ng.IPromise<IValidate>;
33 createComponent(component:Component):ng.IPromise<Component>;
34 addOrUpdateArtifact(componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
35 deleteArtifact(componentId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
36 addProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
37 updateProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
38 addAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
39 updateAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
40 deleteProperty(componentId:string, propertyId:string):ng.IPromise<PropertyModel>;
41 deleteAttribute(componentId:string, attributeId:string):ng.IPromise<AttributeModel>;
42 changeResourceInstanceVersion(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance>;
43 updateInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
44 addInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
45 deleteInstanceArtifact(componentId:string, instanceId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
46 createComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
47 updateComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
48 updateMultipleComponentInstances(componentId:string, instances:Array<ComponentInstance>):ng.IPromise< Array<ComponentInstance>>;
49 downloadArtifact(componentId:string, artifactId:string):ng.IPromise<IFileDownload>;
50 uploadInstanceEnvFile(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
51 downloadInstanceArtifact(componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload>;
52 deleteComponentInstance(componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance>;
53 createRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
54 deleteRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
55 getRequirementsCapabilities(componentId:string):ng.IPromise<any>;
56 updateInstanceProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
57 updateInstanceAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
58 getComponentInstancesFilteredByInputsAndProperties(componentId:string, searchText:string):ng.IPromise<Array<ComponentInstance>>
59 getComponentInstanceInputs(componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>>;
60 getComponentInputs(componentId:string):ng.IPromise<Array<InputModel>>;
61 getComponentInstanceInputProperties(componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>>;
62 getComponentInstanceProperties(componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>>;
63 getModuleForDisplay(componentId:string, moduleId:string):ng.IPromise<DisplayModule>;
64 getComponentInstanceModule(componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule>;
65 updateGroupMetadata(componentId:string, group:Module):ng.IPromise<Module>;
66 getComponentInputInputsAndProperties(serviceId:string, input:string):ng.IPromise<InputsAndProperties>;
67 createInputsFromInstancesInputs(serviceId:string, instancesInputsMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>>;
68 createInputsFromInstancesInputsProperties(resourceId:string, instanceInputsPropertiesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>>;
69 deleteComponentInput(serviceId:string, inputId:string):ng.IPromise<InputModel>;
70 getArtifactByGroupType(componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
71 getComponentInstanceArtifactsByGroupType(componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
74 export class ComponentService implements IComponentService {
79 'Sdc.Services.SharingService',
84 constructor(protected restangular:restangular.IElement,
85 protected sdcConfig:IAppConfigurtaion,
86 protected sharingService:SharingService,
87 protected $q:ng.IQService,
91 this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
92 this.restangular.setRequestInterceptor(function (elem, operation) {
93 if (operation === "remove") {
98 // this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
101 //this function is override by each service, we need to change this method to abstract when updtaing typescript version
102 protected createComponentObject = (component:Component):Component => {
106 public getComponent = (id:string):ng.IPromise<Component> => {
107 let deferred = this.$q.defer();
108 this.restangular.one(id).get().then((response:Component) => {
109 let component:Component = this.createComponentObject(response);
110 //console.log("Component Loaded successfully : ", component);
111 deferred.resolve(component);
113 console.log("Failed to load component with ID: " + id);
114 deferred.reject(err);
116 return deferred.promise;
119 public updateComponent = (component:Component):ng.IPromise<Component> => {
120 // If this is resource
121 if (component instanceof Resource) {
122 let resource:Resource = <Resource>component;
123 if (resource.importedFile) {
124 // Update resource with payload data.
125 return this.updateResourceWithPayload(resource);
127 if (component.csarUUID) {
128 // Update resource without payload data.
129 return this.updateResource(component);
131 // Update resource without payload data (metadata).
132 return this.updateResourceMetadata(component);
136 return this.updateService(component);
140 private updateService = (component:Component):ng.IPromise<Component> => {
141 let deferred = this.$q.defer();
142 this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
143 let component:Component = this.createComponentObject(response);
144 deferred.resolve(component);
146 deferred.reject(err);
148 return deferred.promise;
151 private updateResource = (component:Component):ng.IPromise<Component> => {
152 let deferred = this.$q.defer();
153 this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
154 let component:Component = this.createComponentObject(response);
155 deferred.resolve(component);
157 deferred.reject(err);
159 return deferred.promise;
162 private updateResourceMetadata = (component:Component):ng.IPromise<Component> => {
163 let deferred = this.$q.defer();
164 this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response:Component) => {
165 let component:Component = this.createComponentObject(response);
166 deferred.resolve(component);
168 deferred.reject(err);
170 return deferred.promise;
174 * Only resource can be updated with payload data
176 * @returns {IPromise<T>}
178 private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
179 let deferred = this.$q.defer();
181 resource.payloadData = resource.importedFile.base64;
182 resource.payloadName = resource.importedFile.filename;
183 let headerObj = this.getHeaderMd5(resource);
185 this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
186 let componentResult:Component = this.createComponentObject(response);
187 deferred.resolve(componentResult);
189 deferred.reject(err);
192 return deferred.promise;
195 public createComponent = (component:Component):ng.IPromise<Component> => {
196 let deferred = this.$q.defer();
197 let headerObj = this.getHeaderMd5(component);
198 this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
199 let component:Component = this.createComponentObject(response);
200 deferred.resolve(component);
202 deferred.reject(err);
204 return deferred.promise;
207 public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
208 let deferred = this.$q.defer();
209 this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
210 deferred.resolve(response.plain());
212 deferred.reject(err);
214 return deferred.promise;
217 public changeLifecycleState = (component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> => {
218 let deferred = this.$q.defer();
219 this.restangular.one(component.uniqueId).one(state).customPOST(userRemarks).then((response:ComponentMetadata) => {
220 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
221 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
222 deferred.resolve(component);
224 deferred.reject(err);
226 return deferred.promise;
229 // ------------------------------------------------ Artifacts API --------------------------------------------------//
230 public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
231 let deferred = this.$q.defer();
233 if (artifact.payloadData) {
234 headerObj = this.getHeaderMd5(artifact);
236 this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
237 deferred.resolve(response.plain());
239 deferred.reject(err);
241 return deferred.promise;
244 public downloadArtifact = (componentId:string, artifactId:string):ng.IPromise<IFileDownload> => {
245 let deferred = this.$q.defer();
246 this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response:any) => {
247 deferred.resolve(response.plain());
249 deferred.reject(err);
251 return deferred.promise;
254 public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
255 let deferred = this.$q.defer();
256 this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
257 deferred.resolve(response);
259 deferred.reject(err);
261 return deferred.promise;
264 public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
265 let deferred = this.$q.defer();
266 this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
267 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
268 deferred.resolve(artifacts);
270 deferred.reject(err);
272 return deferred.promise;
275 public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
276 let deferred = this.$q.defer();
277 this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
278 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
279 deferred.resolve(artifacts);
281 deferred.reject(err);
283 return deferred.promise;
287 // ------------------------------------------------ Properties API --------------------------------------------------//
288 public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
289 let deferred = this.$q.defer();
290 this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
291 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
292 deferred.resolve(property);
294 deferred.reject(err);
296 return deferred.promise;
299 public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
300 let deferred = this.$q.defer();
301 this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
302 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
303 deferred.resolve(property);
305 deferred.reject(err);
307 return deferred.promise;
310 public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
311 let deferred = this.$q.defer();
312 this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
313 deferred.resolve(response);
315 deferred.reject(err);
317 return deferred.promise;
320 // ------------------------------------------------ Attributes API --------------------------------------------------//
321 public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
322 let deferred = this.$q.defer();
323 this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
324 let attribute:AttributeModel = new AttributeModel(response);
325 deferred.resolve(attribute);
327 deferred.reject(err);
329 return deferred.promise;
332 public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
333 let deferred = this.$q.defer();
334 this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
335 let attribute:AttributeModel = new AttributeModel(response);
336 deferred.resolve(attribute);
338 deferred.reject(err);
340 return deferred.promise;
343 public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
344 let deferred = this.$q.defer();
345 this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
346 deferred.resolve(response);
348 deferred.reject(err);
350 return deferred.promise;
353 // ------------------------------------------------ Component Instances API --------------------------------------------------//
355 public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
356 let deferred = this.$q.defer();
357 this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
358 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
359 console.log("Component Instance created", componentInstance);
360 deferred.resolve(componentInstance);
362 console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
363 deferred.reject(err);
365 return deferred.promise;
368 public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
369 let deferred = this.$q.defer();
370 this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
371 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
372 console.log("Component Instance was updated", componentInstance);
373 deferred.resolve(componentInstance);
375 console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
376 deferred.reject(err);
378 return deferred.promise;
381 public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
382 let deferred = this.$q.defer();
383 this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
384 console.log("Multiple Component Instances was updated", response);
385 let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
386 _.forEach(response, (componentInstance:ComponentInstance) => {
387 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
388 updateInstances.push(updatedComponentInstance);
390 deferred.resolve(updateInstances);
392 console.log("Failed to update Multiple componentInstance.");
393 deferred.reject(err);
395 return deferred.promise;
398 public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
399 let deferred = this.$q.defer();
400 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
401 console.log("Component Instance was deleted");
404 console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
405 deferred.reject(err);
407 return deferred.promise;
410 public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
411 let deferred = this.$q.defer();
412 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
413 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
414 deferred.resolve(componentInstance);
416 deferred.reject(err);
418 return deferred.promise;
421 public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
422 let deferred = this.$q.defer();
423 this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
424 deferred.resolve(response.plain());
426 deferred.reject(err);
428 return deferred.promise;
431 public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
432 let deferred = this.$q.defer();
434 if (artifact.payloadData) {
435 headerObj = this.getHeaderMd5(artifact);
437 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
438 let newArtifact = new ArtifactModel(response);
439 deferred.resolve(newArtifact);
441 deferred.reject(err);
443 return deferred.promise;
446 public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
447 let deferred = this.$q.defer();
449 if (artifact.payloadData) {
450 headerObj = this.getHeaderMd5(artifact);
452 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
453 let artifact:ArtifactModel = new ArtifactModel(response.plain());
454 deferred.resolve(artifact);
456 deferred.reject(err);
458 return deferred.promise;
461 public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
462 let deferred = this.$q.defer();
463 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
464 deferred.resolve(response);
466 deferred.reject(err);
468 return deferred.promise;
471 public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
472 let deferred = this.$q.defer();
474 if (artifact.payloadData) {
475 headerObj = this.getHeaderMd5(artifact);
477 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
478 let newArtifact = new ArtifactModel(response);
479 deferred.resolve(newArtifact);
481 deferred.reject(err);
483 return deferred.promise;
486 public updateInstanceProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
487 let deferred = this.$q.defer();
488 let instanceId = property.resourceInstanceUniqueId;
489 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("property").customPOST(JSON.stringify(property)).then((response:any) => {
490 let newProperty = new PropertyModel(response);
491 newProperty.readonly = true;
492 newProperty.resourceInstanceUniqueId = instanceId;
493 deferred.resolve(newProperty);
495 deferred.reject(err);
497 return deferred.promise;
500 public updateInstanceAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
501 let deferred = this.$q.defer();
502 let instanceId = attribute.resourceInstanceUniqueId;
503 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response:any) => {
504 let newAttribute = new AttributeModel(response);
505 newAttribute.readonly = true;
506 newAttribute.resourceInstanceUniqueId = instanceId;
507 deferred.resolve(newAttribute);
509 deferred.reject(err);
511 return deferred.promise;
514 public createRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
515 let deferred = this.$q.defer();
516 this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(link)).then((response:any) => {
517 let relation:RelationshipModel = new RelationshipModel(response.plain());
518 console.log("Link created successfully ", relation);
519 deferred.resolve(relation);
521 console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
522 deferred.reject(err);
524 return deferred.promise;
527 public deleteRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
528 let deferred = this.$q.defer();
529 this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(link)).then((response:any) => {
530 let relation:RelationshipModel = new RelationshipModel(response);
531 console.log("Link deleted successfully ", relation);
532 deferred.resolve(relation);
534 console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
535 deferred.reject(err);
537 return deferred.promise;
540 public getRequirementsCapabilities = (componentId:string):ng.IPromise<any> => {
541 let deferred = this.$q.defer();
542 this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response:any) => {
543 console.log("Component requirement capabilities recived: ", response);
544 deferred.resolve(response);
546 console.log("Failed to get requirements & capabilities");
547 deferred.reject(err);
549 return deferred.promise;
552 public getModuleForDisplay = (componentId:string, moduleId:string):ng.IPromise<DisplayModule> => {
553 let deferred = this.$q.defer();
554 this.restangular.one(componentId).one("groups").one(moduleId).get().then((response:any) => {
555 console.log("module loaded successfully: ", response);
556 let module:DisplayModule = new DisplayModule(response);
557 deferred.resolve(module);
559 console.log("Failed to get module with id: ", moduleId);
560 deferred.reject(err);
562 return deferred.promise;
565 public getComponentInstanceModule = (componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<Module> => {
566 let deferred = this.$q.defer();
567 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response:any) => {
568 console.log("module loaded successfully: ", response);
569 let module:DisplayModule = new DisplayModule(response);
570 deferred.resolve(module);
572 console.log("Failed to get module with id: ", moduleId);
573 deferred.reject(err);
575 return deferred.promise;
578 public getComponentInstancesFilteredByInputsAndProperties = (componentId:string, searchText?:string):ng.IPromise<Array<ComponentInstance>> => {
579 let deferred = this.$q.defer();
580 this.restangular.one(componentId).one("componentInstances").get({'searchText': searchText}).then((response:any) => {
581 console.log("component instances return successfully: ", response);
582 let componentInstances:Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
583 deferred.resolve(componentInstances);
585 console.log("Failed to get component instances of component with id: " + componentId);
586 deferred.reject(err);
589 return deferred.promise;
592 public getComponentInstanceInputs = (componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>> => {
594 let deferred = this.$q.defer();
595 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response:any) => {
596 console.log("component instance input return successfully: ", response);
597 let inputsArray:Array<InputModel> = new Array<InputModel>();
598 _.forEach(response, (inputObj:InputModel) => {
599 inputsArray.push(new InputModel(inputObj));
601 deferred.resolve(inputsArray);
603 console.log("Failed to get component instance input with id: " + instanceId);
604 deferred.reject(err);
607 return deferred.promise;
610 public getComponentInputs = (componentId:string):ng.IPromise<Array<InputModel>> => {
612 let deferred = this.$q.defer();
613 this.restangular.one(componentId).one("inputs").get().then((response:any) => {
614 console.log("component inputs return successfully: ", response);
615 let inputsArray:Array<InputModel> = new Array<InputModel>();
616 _.forEach(response, (inputObj:InputModel) => {
617 inputsArray.push(new InputModel(inputObj));
619 deferred.resolve(inputsArray);
621 console.log("Failed to get component inputs for component with id: " + componentId);
622 deferred.reject(err);
625 return deferred.promise;
628 public getComponentInstanceInputProperties = (componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>> => {
630 let deferred = this.$q.defer();
631 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response:any) => {
632 console.log("component instance input properties return successfully: ", response);
633 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
634 _.forEach(response, (propertyObj:PropertyModel) => {
635 propertiesArray.push(new PropertyModel(propertyObj));
637 deferred.resolve(propertiesArray);
639 console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
640 deferred.reject(err);
643 return deferred.promise;
647 public getComponentInstanceProperties = (componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>> => {
649 let deferred = this.$q.defer();
650 this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response:any) => {
651 console.log("component instance properties return successfully: ", response);
652 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
653 _.forEach(response, (propertyObj:PropertyModel) => {
654 propertiesArray.push(new PropertyModel(propertyObj));
656 deferred.resolve(propertiesArray);
658 console.log("Failed to get component instance properties with instanceId: " + instanceId);
659 deferred.reject(err);
662 return deferred.promise;
665 public updateGroupMetadata = (componentId:string, group:Module):ng.IPromise<Module> => {
667 let deferred = this.$q.defer();
668 this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response:Module) => {
669 console.log("group metadata updated successfully: ", response);
670 let updatedGroup:Module = new Module(response);
672 deferred.resolve(updatedGroup);
674 console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
675 deferred.reject(err);
678 return deferred.promise;
681 public getComponentInputInputsAndProperties = (serviceId:string, inputId:string):ng.IPromise<InputsAndProperties> => {
682 let defer = this.$q.defer<any>();
683 this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response:InputsAndProperties) => {
685 let inputsArray:Array<InputModel> = new Array<InputModel>();
686 _.forEach(response.inputs, (inputObj:InputModel) => {
687 inputsArray.push(new InputModel(inputObj));
690 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
691 _.forEach(response.properties, (property:PropertyModel) => {
692 propertiesArray.push(new PropertyModel(property));
695 defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
697 console.log("failed to get inputs of input : ", err);
700 return defer.promise;
703 createInputsFromInstancesInputsProperties = (resourceId:string, instancePropertyMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>> => {
704 let defer = this.$q.defer<any>();
705 this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response:any) => {
706 let inputsArray:Array<PropertyModel> = new Array<PropertyModel>();
707 _.forEach(response, (inputObj:PropertyModel) => {
708 inputsArray.push(new PropertyModel(inputObj));
710 defer.resolve(inputsArray);
712 console.log("failed to create service inputs from VF instances inputs : ", err);
715 return defer.promise;
718 createInputsFromInstancesInputs = (serviceId:string, instancesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>> => {
719 let defer = this.$q.defer<any>();
720 this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response:any) => {
721 let inputsArray:Array<InputModel> = new Array<InputModel>();
722 _.forEach(response, (inputObj:InputModel) => {
723 inputsArray.push(new InputModel(inputObj));
725 defer.resolve(inputsArray);
727 console.log("failed to create service inputs from VF instances inputs : ", err);
730 return defer.promise;
733 deleteComponentInput = (serviceId:string, inputId:string):ng.IPromise<InputModel> => {
734 let defer = this.$q.defer();
735 this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response:any) => {
736 let inputToDelete = new InputModel(response);
737 defer.resolve(inputToDelete);
739 console.log("failed to delete input from service: ", err);
742 return defer.promise;
745 private getHeaderMd5 = (object:any):any => {
747 // This is ugly workaround!!!
748 // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
749 JSON.stringify(object);
750 let componentString:string = JSON.stringify(object);
751 let md5Result = md5(componentString).toLowerCase();
752 headerObj = {'Content-MD5': this.$base64.encode(md5Result)};