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 * as _ from "lodash";
22 import {ArtifactModel, IFileDownload, InstancesInputsPropertiesMap, InputModel, IValidate, RelationshipModel, PropertyModel, Component, ComponentInstance,
23 AttributeModel, IAppConfigurtaion, Resource, Module, DisplayModule, ArtifactGroupModel, InputsAndProperties} from "app/models";
24 import {ComponentInstanceFactory, CommonUtils} from "app/utils";
25 import {SharingService} from "app/services-ng2";
26 import {ComponentMetadata} from "../../models/component-metadata";
28 export interface IComponentService {
30 getComponent(id:string);
31 updateComponent(component:Component):ng.IPromise<Component>;
32 changeLifecycleState(component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> ;
33 validateName(newName:string, subtype?:string):ng.IPromise<IValidate>;
34 createComponent(component:Component):ng.IPromise<Component>;
36 importComponent(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, vendorName?: 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>;
79 export class ComponentService implements IComponentService {
84 'Sdc.Services.SharingService',
89 constructor(protected restangular:restangular.IElement,
90 protected sdcConfig:IAppConfigurtaion,
91 protected sharingService:SharingService,
92 protected $q:ng.IQService,
96 this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
97 this.restangular.setRequestInterceptor(function (elem, operation) {
98 if (operation === "remove") {
103 // this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
106 //this function is override by each service, we need to change this method to abstract when updtaing typescript version
107 protected createComponentObject = (component:Component):Component => {
111 public getComponent = (id:string):ng.IPromise<Component> => {
112 let deferred = this.$q.defer<Component>();
113 this.restangular.one(id).get().then((response:Component) => {
114 let component:Component = this.createComponentObject(response);
115 //console.log("Component Loaded successfully : ", component);
116 deferred.resolve(component);
118 console.log("Failed to load component with ID: " + id);
119 deferred.reject(err);
121 return deferred.promise;
124 public updateComponent = (component:Component):ng.IPromise<Component> => {
125 // If this is resource
126 if (component instanceof Resource) {
127 let resource:Resource = <Resource>component;
128 if (resource.importedFile) {
129 // Update resource with payload data.
130 return this.updateResourceWithPayload(resource);
132 if (component.csarUUID) {
133 // Update resource without payload data.
134 return this.updateResource(component);
136 // Update resource without payload data (metadata).
137 return this.updateResourceMetadata(component);
141 return this.updateService(component);
145 private updateService = (component:Component):ng.IPromise<Component> => {
146 let deferred = this.$q.defer<Component>();
147 this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
148 let component:Component = this.createComponentObject(response);
149 deferred.resolve(component);
151 deferred.reject(err);
153 return deferred.promise;
156 private updateResource = (component:Component):ng.IPromise<Component> => {
157 let deferred = this.$q.defer<Component>();
158 this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
159 let component:Component = this.createComponentObject(response);
160 deferred.resolve(component);
162 deferred.reject(err);
164 return deferred.promise;
167 private updateResourceMetadata = (component:Component):ng.IPromise<Component> => {
168 let deferred = this.$q.defer<Component>();
169 this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response:Component) => {
170 let component:Component = this.createComponentObject(response);
171 deferred.resolve(component);
173 deferred.reject(err);
175 return deferred.promise;
179 * Only resource can be updated with payload data
181 * @returns {IPromise<T>}
183 private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
184 let deferred = this.$q.defer<Component>();
186 resource.payloadData = resource.importedFile.base64;
187 resource.payloadName = resource.importedFile.filename;
188 let headerObj = this.getHeaderMd5(resource);
190 this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
191 let componentResult:Component = this.createComponentObject(response);
192 deferred.resolve(componentResult);
194 deferred.reject(err);
197 return deferred.promise;
200 public createComponent = (component:Component):ng.IPromise<Component> => {
201 let deferred = this.$q.defer<Component>();
202 let headerObj = this.getHeaderMd5(component);
203 this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
204 let component:Component = this.createComponentObject(response);
205 deferred.resolve(component);
207 deferred.reject(err);
209 return deferred.promise;
212 public importComponent = (component: Component): ng.IPromise<Component> => {
213 component.vendorName = "xfr";
214 component.vendorRelease = "xfr";
215 let deferred = this.$q.defer<Component>();
216 let headerObj = this.getHeaderMd5(component);
217 this.restangular.customPOST(JSON.stringify(component), 'importService', {}, headerObj).then((response: Component) => {
218 let component: Component = this.createComponentObject(response);
219 deferred.resolve(component);
221 deferred.reject(err);
223 return deferred.promise;
226 public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
227 let deferred = this.$q.defer<IValidate>();
228 this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
229 deferred.resolve(response.plain());
231 deferred.reject(err);
233 return deferred.promise;
236 public changeLifecycleState = (component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> => {
237 let deferred = this.$q.defer<ComponentMetadata>();
238 this.restangular.one(component.uniqueId).one(state).customPOST(userRemarks).then((response:ComponentMetadata) => {
239 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
240 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
241 deferred.resolve(component);
243 deferred.reject(err);
245 return deferred.promise;
248 // ------------------------------------------------ Artifacts API --------------------------------------------------//
249 public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
250 let deferred = this.$q.defer<ArtifactModel>();
252 if (artifact.payloadData) {
253 headerObj = this.getHeaderMd5(artifact);
255 this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
256 deferred.resolve(response.plain());
258 deferred.reject(err);
260 return deferred.promise;
263 public downloadArtifact = (componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload> => {
264 let deferred = this.$q.defer<IFileDownload>();
265 if(vendorName === 'IsService'){
266 this.restangular.one('importService').one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
267 deferred.resolve(response.plain());
269 deferred.reject(err);
272 this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
273 deferred.resolve(response.plain());
275 deferred.reject(err);
278 return deferred.promise;
281 public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
282 let deferred = this.$q.defer<ArtifactModel>();
283 this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
284 deferred.resolve(response);
286 deferred.reject(err);
288 return deferred.promise;
291 public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
292 let deferred = this.$q.defer<ArtifactGroupModel>();
293 this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
294 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
295 deferred.resolve(artifacts);
297 deferred.reject(err);
299 return deferred.promise;
302 public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
303 let deferred = this.$q.defer<ArtifactGroupModel>();
304 this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
305 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
306 deferred.resolve(artifacts);
308 deferred.reject(err);
310 return deferred.promise;
314 // ------------------------------------------------ Properties API --------------------------------------------------//
315 public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
316 let deferred = this.$q.defer<PropertyModel>();
317 this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
318 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
319 deferred.resolve(property);
321 deferred.reject(err);
323 return deferred.promise;
326 public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
327 let deferred = this.$q.defer<PropertyModel>();
328 this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
329 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
330 deferred.resolve(property);
332 deferred.reject(err);
334 return deferred.promise;
337 public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
338 let deferred = this.$q.defer<PropertyModel>();
339 this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
340 deferred.resolve(response);
342 deferred.reject(err);
344 return deferred.promise;
347 // ------------------------------------------------ Attributes API --------------------------------------------------//
348 public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
349 let deferred = this.$q.defer<AttributeModel>();
350 this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
351 let attribute:AttributeModel = new AttributeModel(response);
352 deferred.resolve(attribute);
354 deferred.reject(err);
356 return deferred.promise;
359 public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
360 let deferred = this.$q.defer<AttributeModel>();
361 this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
362 let attribute:AttributeModel = new AttributeModel(response);
363 deferred.resolve(attribute);
365 deferred.reject(err);
367 return deferred.promise;
370 public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
371 let deferred = this.$q.defer<AttributeModel>();
372 this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
373 deferred.resolve(response);
375 deferred.reject(err);
377 return deferred.promise;
380 // ------------------------------------------------ Component Instances API --------------------------------------------------//
382 public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
383 let deferred = this.$q.defer<ComponentInstance>();
384 this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
385 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
386 console.log("Component Instance created", componentInstance);
387 deferred.resolve(componentInstance);
389 console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
390 deferred.reject(err);
392 return deferred.promise;
395 public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
396 let deferred = this.$q.defer<ComponentInstance>();
397 this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
398 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
399 console.log("Component Instance was updated", componentInstance);
400 deferred.resolve(componentInstance);
402 console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
403 deferred.reject(err);
405 return deferred.promise;
408 public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
409 let deferred = this.$q.defer<Array<ComponentInstance>>();
410 this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
411 console.log("Multiple Component Instances was updated", response);
412 let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
413 _.forEach(response, (componentInstance:ComponentInstance) => {
414 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
415 updateInstances.push(updatedComponentInstance);
417 deferred.resolve(updateInstances);
419 console.log("Failed to update Multiple componentInstance.");
420 deferred.reject(err);
422 return deferred.promise;
425 public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
426 let deferred = this.$q.defer<ComponentInstance>();
427 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
428 console.log("Component Instance was deleted");
431 console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
432 deferred.reject(err);
434 return deferred.promise;
437 public checkResourceInstanceVersionChange = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
438 let deferred = this.$q.defer<ComponentInstance>();
439 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response:any) => {
440 deferred.resolve(response);
442 deferred.reject(err);
444 return deferred.promise;
447 public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
448 let deferred = this.$q.defer<ComponentInstance>();
449 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
450 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
451 deferred.resolve(componentInstance);
453 deferred.reject(err);
455 return deferred.promise;
458 public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
459 let deferred = this.$q.defer<IFileDownload>();
460 this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
461 deferred.resolve(response.plain());
463 deferred.reject(err);
465 return deferred.promise;
468 public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
469 let deferred = this.$q.defer<ArtifactModel>();
471 if (artifact.payloadData) {
472 headerObj = this.getHeaderMd5(artifact);
474 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
475 let newArtifact = new ArtifactModel(response);
476 deferred.resolve(newArtifact);
478 deferred.reject(err);
480 return deferred.promise;
483 public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
484 let deferred = this.$q.defer<ArtifactModel>();
486 if (artifact.payloadData) {
487 headerObj = this.getHeaderMd5(artifact);
489 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
490 let artifact:ArtifactModel = new ArtifactModel(response.plain());
491 deferred.resolve(artifact);
493 deferred.reject(err);
495 return deferred.promise;
498 public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
499 let deferred = this.$q.defer<ArtifactModel>();
500 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
501 deferred.resolve(response);
503 deferred.reject(err);
505 return deferred.promise;
508 public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
509 let deferred = this.$q.defer<ArtifactModel>();
511 if (artifact.payloadData) {
512 headerObj = this.getHeaderMd5(artifact);
514 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
515 let newArtifact = new ArtifactModel(response);
516 deferred.resolve(newArtifact);
518 deferred.reject(err);
520 return deferred.promise;
523 public updateInstanceProperties = (componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]> => {
524 let deferred = this.$q.defer<PropertyModel[]>();
525 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response:any) => {
526 const newProperties = response.map((res) => {
527 const newProperty = new PropertyModel(res);
528 newProperty.readonly = true;
529 newProperty.resourceInstanceUniqueId = componentInstanceId;
532 deferred.resolve(newProperties);
534 deferred.reject(err);
536 return deferred.promise;
539 public updateInstanceAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
540 let deferred = this.$q.defer<AttributeModel>();
541 let instanceId = attribute.resourceInstanceUniqueId;
542 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response:any) => {
543 let newAttribute = new AttributeModel(response);
544 newAttribute.readonly = true;
545 newAttribute.resourceInstanceUniqueId = instanceId;
546 deferred.resolve(newAttribute);
548 deferred.reject(err);
550 return deferred.promise;
553 public createRelation = (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;
560 this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(linkPayload)).then((response:any) => {
561 let relation:RelationshipModel = new RelationshipModel(response.plain());
562 console.log("Link created successfully ", relation);
563 deferred.resolve(relation);
565 console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
566 deferred.reject(err);
568 return deferred.promise;
571 public deleteRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
572 let deferred = this.$q.defer<RelationshipModel>();
573 const linkPayload:RelationshipModel = new RelationshipModel(link);
574 linkPayload.relationships.forEach((rel) => {
575 delete rel.capability;
576 delete rel.requirement;
578 this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(linkPayload)).then((response:any) => {
579 let relation:RelationshipModel = new RelationshipModel(response);
580 console.log("Link deleted successfully ", relation);
581 deferred.resolve(relation);
583 console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
584 deferred.reject(err);
586 return deferred.promise;
589 public fetchRelation = (componentId:string, linkId:string):ng.IPromise<RelationshipModel> => {
590 let deferred = this.$q.defer<RelationshipModel>();
591 this.restangular.one(componentId).one("relationId").one(linkId).get().then((response:any) => {
592 let relation:RelationshipModel = new RelationshipModel(response);
593 console.log("Link fetched successfully ", relation);
594 deferred.resolve(relation);
596 console.log("Failed to fetch Link Id: " + linkId);
597 deferred.reject(err);
599 return deferred.promise;
602 public getRequirementsCapabilities = (componentId:string):ng.IPromise<any> => {
603 let deferred = this.$q.defer();
604 this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response:any) => {
605 console.log("Component requirement capabilities recived: ", response);
606 deferred.resolve(response);
608 console.log("Failed to get requirements & capabilities");
609 deferred.reject(err);
611 return deferred.promise;
614 public getModuleForDisplay = (componentId:string, moduleId:string):ng.IPromise<DisplayModule> => {
615 let deferred = this.$q.defer<DisplayModule>();
616 this.restangular.one(componentId).one("groups").one(moduleId).get().then((response:any) => {
617 console.log("module loaded successfully: ", response);
618 let module:DisplayModule = new DisplayModule(response);
619 deferred.resolve(module);
621 console.log("Failed to get module with id: ", moduleId);
622 deferred.reject(err);
624 return deferred.promise;
627 public getComponentInstanceModule = (componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule> => {
628 let deferred = this.$q.defer<DisplayModule>();
629 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response:any) => {
630 console.log("module loaded successfully: ", response);
631 let module:DisplayModule = new DisplayModule(response);
632 deferred.resolve(module);
634 console.log("Failed to get module with id: ", moduleId);
635 deferred.reject(err);
637 return deferred.promise;
640 public getComponentInstancesFilteredByInputsAndProperties = (componentId:string, searchText?:string):ng.IPromise<Array<ComponentInstance>> => {
641 let deferred = this.$q.defer<Array<ComponentInstance>>();
642 this.restangular.one(componentId).one("componentInstances").get({'searchText': searchText}).then((response:any) => {
643 console.log("component instances return successfully: ", response);
644 let componentInstances:Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
645 deferred.resolve(componentInstances);
647 console.log("Failed to get component instances of component with id: " + componentId);
648 deferred.reject(err);
651 return deferred.promise;
654 public getComponentInstanceInputs = (componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>> => {
656 let deferred = this.$q.defer<Array<InputModel>>();
657 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response:any) => {
658 console.log("component instance input return successfully: ", response);
659 let inputsArray:Array<InputModel> = new Array<InputModel>();
660 _.forEach(response, (inputObj:InputModel) => {
661 inputsArray.push(new InputModel(inputObj));
663 deferred.resolve(inputsArray);
665 console.log("Failed to get component instance input with id: " + instanceId);
666 deferred.reject(err);
669 return deferred.promise;
672 public getComponentInputs = (componentId:string):ng.IPromise<Array<InputModel>> => {
674 let deferred = this.$q.defer<Array<InputModel>>();
675 this.restangular.one(componentId).one("inputs").get().then((response:any) => {
676 console.log("component inputs return successfully: ", response);
677 let inputsArray:Array<InputModel> = new Array<InputModel>();
678 _.forEach(response, (inputObj:InputModel) => {
679 inputsArray.push(new InputModel(inputObj));
681 deferred.resolve(inputsArray);
683 console.log("Failed to get component inputs for component with id: " + componentId);
684 deferred.reject(err);
687 return deferred.promise;
690 public getComponentInstanceInputProperties = (componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>> => {
692 let deferred = this.$q.defer<Array<PropertyModel>>();
693 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response:any) => {
694 console.log("component instance input properties return successfully: ", response);
695 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
696 _.forEach(response, (propertyObj:PropertyModel) => {
697 propertiesArray.push(new PropertyModel(propertyObj));
699 deferred.resolve(propertiesArray);
701 console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
702 deferred.reject(err);
705 return deferred.promise;
709 public getComponentInstanceProperties = (componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>> => {
710 let deferred = this.$q.defer<Array<PropertyModel>>();
711 this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response:any) => {
712 console.log("component instance properties return successfully: ", response);
713 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
714 _.forEach(response, (propertyObj:PropertyModel) => {
715 propertiesArray.push(new PropertyModel(propertyObj));
717 deferred.resolve(propertiesArray);
719 console.log("Failed to get component instance properties with instanceId: " + instanceId);
720 deferred.reject(err);
723 return deferred.promise;
726 public updateGroupMetadata = (componentId:string, group:Module):ng.IPromise<Module> => {
728 let deferred = this.$q.defer<Module>();
729 this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response:Module) => {
730 console.log("group metadata updated successfully: ", response);
731 let updatedGroup:Module = new Module(response);
733 deferred.resolve(updatedGroup);
735 console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
736 deferred.reject(err);
739 return deferred.promise;
742 public getComponentInputInputsAndProperties = (serviceId:string, inputId:string):ng.IPromise<InputsAndProperties> => {
743 let defer = this.$q.defer<InputsAndProperties>();
744 this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response:InputsAndProperties) => {
746 let inputsArray:Array<InputModel> = new Array<InputModel>();
747 _.forEach(response.inputs, (inputObj:InputModel) => {
748 inputsArray.push(new InputModel(inputObj));
751 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
752 _.forEach(response.properties, (property:PropertyModel) => {
753 propertiesArray.push(new PropertyModel(property));
756 defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
758 console.log("failed to get inputs of input : ", err);
761 return defer.promise;
764 createInputsFromInstancesInputsProperties = (resourceId:string, instancePropertyMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>> => {
765 let defer = this.$q.defer<Array<PropertyModel>>();
766 this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response:any) => {
767 let inputsArray:Array<PropertyModel> = new Array<PropertyModel>();
768 _.forEach(response, (inputObj:PropertyModel) => {
769 inputsArray.push(new PropertyModel(inputObj));
771 defer.resolve(inputsArray);
773 console.log("failed to create service inputs from VF instances inputs : ", err);
776 return defer.promise;
779 createInputsFromInstancesInputs = (serviceId:string, instancesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>> => {
780 let defer = this.$q.defer<Array<InputModel>>();
781 this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response:any) => {
782 let inputsArray:Array<InputModel> = new Array<InputModel>();
783 _.forEach(response, (inputObj:InputModel) => {
784 inputsArray.push(new InputModel(inputObj));
786 defer.resolve(inputsArray);
788 console.log("failed to create service inputs from VF instances inputs : ", err);
791 return defer.promise;
794 deleteComponentInput = (serviceId:string, inputId:string):ng.IPromise<InputModel> => {
795 let defer = this.$q.defer<InputModel>();
796 this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response:any) => {
797 let inputToDelete = new InputModel(response);
798 defer.resolve(inputToDelete);
800 console.log("failed to delete input from service: ", err);
803 return defer.promise;
806 private getHeaderMd5 = (object:any):any => {
808 // This is ugly workaround!!!
809 // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
810 JSON.stringify(object);
811 let componentString:string = JSON.stringify(object);
812 let md5Result = md5(componentString).toLowerCase();
813 headerObj = {'Content-MD5': this.$base64.encode(md5Result)};